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


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;
        } 
    }
}

pls help =/

What’s happening is the OS only allows one key to fire the event at a time. When you hold down one key then press a second one, it stops calling the fire for the first one.
You need to keep track of which button is pressed, by setting a boolean. Then add the keyReleased to your KeyAdapter. Set the boolean to true on the key press and false on the key release. In your paint you will have to add to your movement. See this post a while back.
http://www.java-gaming.org/index.php/topic,23664.msg196970.html#msg196970

ok ty i’ll try this. Do you know how to solve problem with delayed painting?

Once you implement the boolean method this will fix the pausing when you first press the button.

I’d actually like to know why this would fix the delay? Is it because he’s not using the standard keyPressed/keyReleased instead of his method? I actually doubt that would be the problem, but I can’t see the reason as to why it would be delayed on his first key press.

He is using keyPressed just using it in a strange way, see line 28.
The reason it causes such odd behavior is because the keyPressed is only called at the computers typematic rate and also it’s called from a different thread, the boolean method described above fixes both of these.

On a side note I noticed an infinite loop bug on line 41, your missing an = or better yet remove the comparison because running is already a boolean.

For 2nd question, use help of dx and dy variabel and update your ball position through these variabels. They will control the movement direction and speed. If you make your game loop right, it should be moving diagonal.