Results 1 to 8 of 8
Thread: Ball not moving
- 01-04-2012, 01:09 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Ball not moving
Hello,
My goal is to make the black ball move in the window when I press arrow keys. But it does not, it stays in one place no matter what key I press.
Here is the code that I have so far:
Java Code:package javagame; import javax.swing.JFrame; import java.awt.Graphics; import java.awt.event.KeyAdapter; public class JavaGame extends JFrame { int x, y; public class AL extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if (keyCode == e.VK_LEFT) { x--; } if (keyCode == e.VK_RIGHT) { x++; } if (keyCode == e.VK_UP) { y--; } if (keyCode == e.VK_DOWN) { y++; } } public void keyReleased(KeyEvent e) { } } public JavaGame() { addKeyListener(new AL()); setTitle("My First Java Game"); setSize(500, 500); setResizable(false); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); x = 150; y = 150; } public void paint(Graphics g) { g.fillOval(x, y, 15, 15); repaint(); } public static void main(String[] args) { JavaGame theGame = new JavaGame(); } }Last edited by Eleeist; 01-04-2012 at 01:13 PM.
- 01-04-2012, 03:38 PM #2
Re: Ball not moving
Works for me. You also forgot an import statement - import java.awt.event.KeyEvent;
- 01-04-2012, 03:41 PM #3
Re: Ball not moving
P.S. Putting repaint() in the paint method is pretty weird - that'll cause infinite repainting won't it?
Move that statement to the end of the keyPressed() method, so the screen repaints when the user hits a key!
- 01-04-2012, 05:47 PM #4
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Ball not moving
The ball is definitely not moving for me. Any ideas what that could be?
PS: I have applied the corrections that you suggested. Thanks!
- 01-04-2012, 06:10 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Ball not moving
Add a System.out.println() to print out the keycode you get in the keyPressed event, just to check you are getting what you think.
Have you moved the repaint()?
- 01-04-2012, 07:00 PM #6
Re: Ball not moving
Ball moves for me, and it leaves a painted 'trail' behind it (snake style) which it should (with the way you coded the logic). It could be possible it doesn't recognize your key presses? Put a System.out.println(e) in the keyPressed event method and verify that not only does it see the key press, but it is the correct key!
- 01-04-2012, 08:17 PM #7
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: Ball not moving
That is really strange. I switched to Eclipse from NetBeans and got it to work, but now I tried it again and no luck :(. I don't know what's happening. I added the println statements to print the keyCode. My keyCodes are:
Left: 37
Up: 38
Right: 39
Down: 40
Is that correct?
- 01-04-2012, 11:27 PM #8
Similar Threads
-
Cant create more than one ball
By vettera in forum AWT / SwingReplies: 5Last Post: 09-17-2011, 05:26 PM -
Moving a ball with Arrow Keys
By kekcklemen in forum Java AppletsReplies: 5Last Post: 02-25-2011, 10:15 PM -
Ball program
By codeStone in forum Advanced JavaReplies: 10Last Post: 02-25-2011, 01:32 PM -
Need help with a third ball in game.
By vlan in forum Java AppletsReplies: 2Last Post: 05-30-2010, 03:37 PM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks