
-----------------------------------
Flikerator
Thu Aug 09, 2007 7:59 pm

Keyboard question
-----------------------------------
I just started learning Java (I'm not asking for a lecture on why I shouldn't use Java either), and I've been stuck on getting input from the keyboard to work for a few hours. Its probably not that difficult, but I can't get it to work at all. I've looked up a ton of examples. They each do it differently as well. In terms of Turing I want something that works relative to this;

var x, y : int := 50
var chars : array char of boolean
loop
    Input.Flush
    Input.KeyDown (chars)
    if chars ('w') then
        y += 1
    end if
    if chars ('s') then
        y -= 1
    end if
    if chars ('d') then
        x += 1
    end if
    if chars ('a') then
        x -= 1
    end if
    Draw.FillBox (x, y, x + 50, y + 50, red)
    Time.DelaySinceLast (10)
    cls
end loop

And what I have in Java (The latest most cutdown version);

import java.lang.Math;
import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.util.Random;

public class test extends JFrame{	
    public static void main(String

Don't worry too much about the imports. I know some of them aren't being used right now. The reason I have java.awt.* is because if I don't, then it claims it can't find the 'Event' methods, even when I had java.awp.Event.*.

I'm still learning how Java works, and I have the basic idea but not completely, obviously. Having the program continue without loops is still bugging me (The Woes of Turing).

With this good I'm basically grasping for straws. Experimenting until I find something that works, gain some understanding on how it all comes together as I go along.

Any assistance?

-----------------------------------
Flikerator
Fri Aug 10, 2007 2:11 am

Re: Keyboard question
-----------------------------------
I feel I'm so close, but I keep getting this error: events.Test is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListener



package events;

import java.lang.Math;
import java.awt.Graphics;
//import javax.swing.JFrame;
import java.awt.*;
//import java.awt.event.*;
//import javax.swing.*;
import java.awt.Color;
import java.util.Random;

//import java.awt.event.KeyListener;
//import java.awt.event.KeyEvent;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame
	implements KeyListener,
	ActionListener {
    public static void main(String

-----------------------------------
Aziz
Fri Aug 10, 2007 9:19 am

RE:Keyboard question
-----------------------------------
There are a few things:

1) Each class should be in a separate file, ClassName.java

2) test class should not extend JFrame. It's not actually a JFrame, is it? All it does is create an instance of mainFrame().

3) Classes should be named in FirstLetterCase, notLikeThis.

4) test should not implement KeyListener. Let me explain about EventListeners

all Swing components can add certain EventListeners. When something happens on that component (such as a key being typed, or a button being pressed), the internal mechanisms "fire" an Event. If that component has an appropriate listener added to it, it will call a specific method of that selected listener. All listeners inherit from EventListener, and each (almost) Listener has an associated Event.

For example,

A button being clicked. When you make a button, you do like so:

JButton button = new JButton("Click me");
frame.add(button);

When you click that button, it's internal code takes care of certain things, including drawing the clicking animation and "firing" and event. In this case, the button's click would fire an ActionEvent. But our code doesn't add an ActionListener to the button. Let's do that.

An ActionListener is another class. Or rather, it is an interface. It has a single method:

public void actionPerformed(ActionEvent e)

We can't instantiate an object of an interface. We have to create a class that implements ActionListener. We can have our main class do this:

public class MainFrame extends JFrame, implements ActionListener
{
    public MainFrame()
    {
        // we'll set up components here, later
    }
    
    // Overrides void actionPerformed(ActionEvent) in ActionListener
    public void actionPerformed(ActionEvent e)
    {
        // do something
    }
}

Now, in the constructor, when we set up components, we will make a JButton, and use the JButton's addActionListener(ActionListener) method to add the current MainClass object ("this") as an ActionListener (because MainClass implements ActionListener, it "is an" ActionListener, and you could pass it to any method that calls for an ActionListener). When an ActionEvent is fire by a JButton (like clicking it), it will call the associated ActionListener's actionPerformed() method, and pass an ActionEvent object (which can tell you certain information, such as the source of the event). Watch:

JButton button = new JButton("Click me");
button.addActionListener(this); // 