Results 1 to 8 of 8

Thread: Ball not moving

  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Ball not moving

    Works for me. You also forgot an import statement - import java.awt.event.KeyEvent;

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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!

  4. #4
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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!

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default 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()?

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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!

  7. #7
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default 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?

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Ball not moving

    Sounds about right. What happens when you run it outside the IDE, as in from the command line?

Similar Threads

  1. Cant create more than one ball
    By vettera in forum AWT / Swing
    Replies: 5
    Last Post: 09-17-2011, 05:26 PM
  2. Moving a ball with Arrow Keys
    By kekcklemen in forum Java Applets
    Replies: 5
    Last Post: 02-25-2011, 10:15 PM
  3. Ball program
    By codeStone in forum Advanced Java
    Replies: 10
    Last Post: 02-25-2011, 01:32 PM
  4. Need help with a third ball in game.
    By vlan in forum Java Applets
    Replies: 2
    Last Post: 05-30-2010, 03:37 PM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 09:08 PM

Posting Permissions

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