Results 1 to 6 of 6
  1. #1
    kekcklemen is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Question Moving a ball with Arrow Keys

    Ok guys. I'm making a game. For now I made a program in which you can move ball with arrow keys. UP, DOWN, RIGHT and LEFT.

    My first question is:
    I clicked one button... Ball moves... but not instantly!! I just hate this. Like it's somehow delayed or something. At key click it moves only for 5 sources than it moves normally. But I want that ball moves constantly... Try it out with my code. How to make ball moving nice and without dalaying?

    My second question is:
    I can move left and right, up and down. :D how to move up and left at the same time? when I click Up and Left keys I want that ball moves up and left. not just left or just up. how to do that?

    here is the code:


    Java Code:
    import java.awt.Canvas;
    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.image.BufferStrategy;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class HandlingEvents implements Runnable {
    
        JFrame frame;
        int myX = 400;
        int myY = 400;
        Canvas canvas;
        BufferStrategy bufferStrategy;
        boolean running = true;
    
        public HandlingEvents() {
            frame = new JFrame("Basic Game");
            JPanel panel = (JPanel) frame.getContentPane();
            panel.setPreferredSize(new Dimension(500, 500));
            panel.setLayout(null);
            canvas = new Canvas();
            canvas.setBounds(0, 0, 500, 500);
            canvas.setIgnoreRepaint(true);
            panel.add(canvas);
            canvas.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent evt) {
                    moveIt(evt);
                }
            });
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setResizable(false);
            frame.setVisible(true);
            canvas.createBufferStrategy(2);
            bufferStrategy = canvas.getBufferStrategy();
            canvas.requestFocus();
        }
        public void run() {
            while (running = true) {
                Paint();
                try {
                    Thread.sleep(25);
                } catch (InterruptedException e) {
                }
            }
        }
        public static void main(String[] args) {
            HandlingEvents ex = new HandlingEvents();
            new Thread(ex).start();
        }
        public void Paint() {
            Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
            g.clearRect(0, 0, 500, 500);
            Paint(g);
            bufferStrategy.show();
        }
    
        protected void Paint(Graphics2D g) {
            g.fillOval(myX, myY, 30, 30);
        }
        public void moveIt(KeyEvent evt) {
         switch (evt.getKeyCode()) {
                case KeyEvent.VK_DOWN:
                    myY += 5;
                    break;
                case KeyEvent.VK_UP:
                    myY -= 5;
                    break;
                case KeyEvent.VK_LEFT:
                    myX -= 5;
                    break;
                case KeyEvent.VK_RIGHT:
                    myX += 5;
                    break;
            }
            
          
            
        }
    }

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    I just compiled and ran your code as-is, and I see no delay in moving the ball after I've clicked. I also see it moving diagonally when I press two adjacent arrow keys at once.

  3. #3
    kekcklemen is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by NRitH View Post
    I just compiled and ran your code as-is, and I see no delay in moving the ball after I've clicked. I also see it moving diagonally when I press two adjacent arrow keys at once.
    =/ lol
    I'm using netbeans.. Which program do you use?

  4. #4
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    While I'm at it, here are some tips:

    1. Get rid of all that BufferStrategy nonsense. It works, but it's overkill.
    2. Remove the "implements Runnable" and the new Thread crap in main().
    3. Make your class extend Canvas.
    3. Override your Canvas's paint() method to do the clearing and drawing that are in your miscapitalized Paint() methods.
    4. Remove the call to setIgnoreRepaint().
    5. You don't need a Graphics2D object; just use a java.awt.Graphics one.
    6. At the end of your keyPressed() method, call repaint().

    The result? Much, much clearer code. To wit:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    
    public class HandlingEvents extends Canvas {
    
        int myX = 400;
        int myY = 400;
    
        public HandlingEvents() {
            setSize(new Dimension(500, 500));
            addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent evt) {
                    moveIt(evt);
                }
            });
        }
    
        public void paint(Graphics g) {
            g.fillOval(myX, myY, 30, 30);
        }
    
        public void moveIt(KeyEvent evt) {
         switch (evt.getKeyCode()) {
                case KeyEvent.VK_DOWN:
                    myY += 5;
                    break;
                case KeyEvent.VK_UP:
                    myY -= 5;
                    break;
                case KeyEvent.VK_LEFT:
                    myX -= 5;
                    break;
                case KeyEvent.VK_RIGHT:
                    myX += 5;
                    break;
            }
    
            repaint();
        }
    
        public static void main(String[] args) {
            JFrame frame = new JFrame("Basic Game");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            HandlingEvents ex = new HandlingEvents();
            frame.getContentPane().add(ex);
            frame.pack();
            frame.setResizable(false);
            frame.setVisible(true);
            ex.requestFocus();
        }
    }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by NRitH View Post
    While I'm at it, here are some tips:
    ...
    3. Make your class extend Canvas.
    3. Override your Canvas's paint() method to do the clearing and drawing that are in your miscapitalized Paint() methods.
    ....
    Since this is a Swing program, I would recommend that the drawing panel extend JPanel and that the drawing occur in the paintComponent method.

  6. #6
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Since this is a Swing program, I would recommend that the drawing panel extend JPanel and that the drawing occur in the paintComponent method.
    Damn, that's an excellent point.

Similar Threads

  1. Help in bow and arrow game
    By sahildave1991 in forum AWT / Swing
    Replies: 6
    Last Post: 10-18-2010, 03:34 PM
  2. Arrow Button Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:44 PM
  3. Replies: 0
    Last Post: 07-11-2008, 04:07 PM
  4. Draw an arrow
    By Albert in forum SWT / JFace
    Replies: 3
    Last Post: 02-01-2008, 09:11 AM
  5. Problem deleting ball from bouncing ball app
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 10: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
  •