Results 1 to 3 of 3
Thread: #key listener problem
- 02-11-2009, 10:22 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
#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.
thank you.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();
}
}
- 02-12-2009, 12:17 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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}
- 02-14-2009, 09:02 PM #3
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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
-
action listener on jcombobox
By chkm8 in forum New To JavaReplies: 2Last Post: 02-05-2009, 10:14 AM -
Listener for print job
By pjmorce in forum Advanced JavaReplies: 1Last Post: 11-14-2008, 08:02 AM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-20-2008, 11:07 PM -
Regarding Listener
By adeeb in forum AWT / SwingReplies: 2Last Post: 06-10-2008, 02:00 AM -
Listener for SWT event
By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks