Results 1 to 3 of 3
  1. #1
    mij1_7 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default #key listener problem

    hello
    can anyone help me plz with this cheetah and rabbit game, i want the cheetah
    to move one step up, down, left and right when the keys are pressed.
    i managed to take one step up however it doesnt do it again when pressed,
    any hints.

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.*;

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;

    public class Platform implements KeyListener {

    private JFrame window = new JFrame("C&R");
    static int N;
    private JLabel[][] grid = new JLabel[N][N];

    Position posC;
    Position posR;

    public Platform() {

    // parses the string into integer
    N = Integer.parseInt(JOptionPane.showInputDialog(null,
    "enter dimensions"));
    grid = new JLabel[N][N];
    window.setSize(50 * N, 50 * N);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
    window.getContentPane().setLayout(new GridLayout(N, N));
    window.addKeyListener(this);

    for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {

    grid[i][j] = new JLabel(".");
    window.getContentPane().add(grid[i][j]);
    // create the i,j button
    // add the i,j button to the window

    }
    posC = new Position('C', 4, 4);
    posR = new Position('R', 3, 3);

    window.setVisible(true);
    Display();
    }

    public void Display() {
    for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {

    grid[i][j].setText(".");
    }
    grid[posC.getX()][posC.getY()].setText("C");
    // grid[posR.getX()][posR.getY()].setText("R");
    }

    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_UP) {
    posC.goUp();
    Display();
    }
    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {

    }

    public static void main(String[] args) {

    new Platform();
    }
    }
    thank you.

  2. #2
    mij1_7 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    hello
    go up is the method which reduces the y coordinate by 1.
    the initiual value is {code} posC = new Position('C', 4, 4); {code}

    Display shows the cheetah on the grid on position 4,4.
    However when i press up in run time it doesnt do anything.

    I will greatly appreciate any help given here. Thank you all in advance.
    {code}

    public void Display() {


    for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {

    grid[i][j].setText(".");

    }
    grid[posC.getX()][posC.getY()].setText("C");

    }

    {code}
    then the keypressed method is suppose to change the value of y and display it in the new position which is suppose to be (3,4).
    {code}
    public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_UP) {
    posC.goUp();
    System.out.println("hello");
    Display();
    }
    {code}

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default I can't find a keyListener problem here

    This code is 98% working.
    It displays the beginnings of a basic game board,
    the Cheeta responds to the arrow keys,
    and the display update is clean.

    The only problem I find is that the Up/Down keys
    send the Cheeta left and right, and the Left/Right keys
    send the Cheeta up and down.

Similar Threads

  1. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:14 AM
  2. Listener for print job
    By pjmorce in forum Advanced Java
    Replies: 1
    Last Post: 11-14-2008, 08:02 AM
  3. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM
  5. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •