
-----------------------------------
KeijiAurion
Mon Jan 15, 2007 10:13 am

Ready to Program IDE - Keyboard Help?
-----------------------------------
Hi, I'm basically computer illiterate, and I am stuck on how to make keyboard input move things across a GUI in Java. I'm using the Ready to Program Java IDE compiler, and I've run into a problem.

I'm guessing I have to create whatever I want to move in the paint area (For this example, I'm using a circle). Will I have to somehow classify this circle as an object?

I have found a tutorial on how to read keyboard input and determine the key code, if it is an action key, a modifier, and the location on the keyboard; but I'm unsure of what coding I'll need to modify this to move this circle around.

Oh, by the way, I already know that this program is junk. Please, spare me from any flames like that; if I could use a proper Java compiler, I would.

This is the tutorial I got:

import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;

public class KeyEventDemo extends JPanel 
                          implements KeyListener,
                                     ActionListener {
    JTextArea displayArea;
    JTextField typingArea;
    static final String newline = "\n";

    public KeyEventDemo() {
        super(new BorderLayout());

        JButton button = new JButton("Clear");
        button.addActionListener(this);

        typingArea = new JTextField(20);
        typingArea.addKeyListener(this);

        //Uncomment this if you wish to turn off focus
        //traversal.  The focus subsystem consumes
        //focus traversal keys, such as Tab and Shift Tab.
        //If you uncomment the following line of code, this
        //disables focus traversal and the Tab events will
        //become available to the key event listener.
        //typingArea.setFocusTraversalKeysEnabled(false);

        displayArea = new JTextArea();
        displayArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(displayArea);
        scrollPane.setPreferredSize(new Dimension(375, 125));

        add(typingArea, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
        add(button, BorderLayout.PAGE_END);
    }

    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
        displayInfo(e, "KEY TYPED: ");
    }

    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
        displayInfo(e, "KEY PRESSED: ");
    }

    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e) {
        displayInfo(e, "KEY RELEASED: ");
    }

    /** Handle the button click. */
    public void actionPerformed(ActionEvent e) {
        //Clear the text components.
        displayArea.setText("");
        typingArea.setText("");

        //Return the focus to the typing area.
        typingArea.requestFocusInWindow();
    }

    /*
     * We have to jump through some hoops to avoid
     * trying to print non-printing characters 
     * such as Shift.  (Not only do they not print, 
     * but if you put them in a String, the characters
     * afterward won't show up in the text area.)
     */
    protected void displayInfo(KeyEvent e, String s){
        String keyString, modString, tmpString,
               actionString, locationString;

        //You should only rely on the key char if the event
        //is a key typed event.
        int id = e.getID();
        if (id == KeyEvent.KEY_TYPED) {
            char c = e.getKeyChar();
            keyString = "key character = '" + c + "'";
        } else {
            int keyCode = e.getKeyCode();
            keyString = "key code = " + keyCode
                        + " ("
                        + KeyEvent.getKeyText(keyCode)
                        + ")";
        }

        int modifiers = e.getModifiersEx();
        modString = "modifiers = " + modifiers;
        tmpString = KeyEvent.getModifiersExText(modifiers);
        if (tmpString.length() > 0) {
            modString += " (" + tmpString + ")";
        } else {
            modString += " (no modifiers)";
        }

        actionString = "action key? ";
        if (e.isActionKey()) {
            actionString += "YES";
        } else {
            actionString += "NO";
        }

        locationString = "key location: ";
        int location = e.getKeyLocation();
        if (location == KeyEvent.KEY_LOCATION_STANDARD) {
            locationString += "standard";
        } else if (location == KeyEvent.KEY_LOCATION_LEFT) {
            locationString += "left";
        } else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
            locationString += "right";
        } else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
            locationString += "numpad";
        } else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
            locationString += "unknown";
        }

        displayArea.append(s + newline
                           + "    " + keyString + newline
                           + "    " + modString + newline
                           + "    " + actionString + newline
                           + "    " + locationString + newline);
        displayArea.setCaretPosition(displayArea.getDocument().getLength());
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("KeyEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new KeyEventDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

mod edit: wtd added code tags

-----------------------------------
KeijiAurion
Tue Jan 16, 2007 9:51 am

Re: Ready to Program IDE - Keyboard Help?
-----------------------------------
Okay, I've started to try and compile on my own; but it's just not working. *sighs* I think I understand KeyListeners and KeyEvents and such; but I'm getting a lot of events. This is my code:

// Programmed by: John Elasigue
// Date Started: January 10th, 2007
// This is (at least, planned to be) a remake of a Fire Emblem game.
// The "FireEmblem" class.
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class FireEmblem extends Applet
    implements KeyListener,
    ActionListener

{
    Object Player;

    int y = 0;
    int x = 0;
    int turn = 0;

    //Procedure for delays for drawing
    public static void delay (int de)

    {
        try
        {
            Thread.sleep (de);
        }

        catch (Exception e)
        {
            ;
        }
    }


    // This is to be used to handle the arrow keys being typed.
    public void keyTyped (KeyEvent e)
    {
        int id = e.getID ();
        if (id == KeyEvent.KEY_TYPED)
        {
            if (id == 39) // This represents the right arrow being pressed
            {
                Graphics g = g.getGraphics (); //Gets graphics from the paint class
                g.setColor (new Color (0, 100, 0));
                Player = g.fillOval (x, y, 32, 32);
                delay (5);
                g.setColor (new Color (0, 191, 255));
                Player = g.fillOval (x + 32, y, 32, 32); // Moves player's circle to the right.
            }
        }
    }


    public void init ()
    {
        // Place the body of the initialization method here
    } // init method


    public void paint (Graphics g)
    {
        // Draws the green background (the grass)
        g.setColor (new Color (0, 100, 0));
        g.fillRect (0, 0, 320, 320);

        // Draws the grid for movement.
        for (int i = 0 ; i 