Here is my code:
public class Main extends Applet implements Runnable, KeyListener,
java.awt.event.MouseListener {
int x_pos = 300;
int y_pos = 200;
int radius = 20;
int appletsize_x = 600;
int appletsize_y = 400;
double x_speed = 0;
double y_speed = 0;
private Image dbImage;
private Graphics dbg;
public void init() {
this.setSize(600, 400);
}
public void start() {
this.addKeyListener(this);
this.addMouseListener(this);
Thread th = new Thread(this);
th.start();
}
public void stop() {
}
public void destroy() {
}
public void run() {
// lower ThreadPriority
this.requestFocusInWindow();
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (true) {
repaint();
x_pos += x_speed;
y_pos += y_speed;
// Hitting (right)
if (x_pos > this.getSize().width - radius) {
//x_speed = -x_speed;
x_speed = 0;
}
// Hitting (left)
if (x_pos < 0 + radius) {
//x_speed = -x_speed;
x_speed = 0;
}
// Hitting top
if (y_pos < 0 + radius) {
//y_speed = -y_speed;
y_speed = 0;
}
// Hitting bottom
if (y_pos > this.getSize().height - radius) {
//y_speed = -y_speed;
y_speed = 0;
}
try {
// Stop thread for 1 milliseconds
Thread.sleep(20);
} catch (InterruptedException ex) {
// do nothing
}
// set ThreadPriority to maximum value
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
}
public void paint(Graphics g) {
// set colour
g.setColor(Color.red);
// paint a filled coloured circle
g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
}
public void update(Graphics g) {
if (dbImage == null) {
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
dbg.setColor(getBackground());
dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (x_speed > 0) {
x_speed = +x_speed;
y_speed = 0;
}
if (x_speed == 0) {
x_speed = -4;
y_speed = 0;
}
if (x_speed < 0) {
x_speed = +x_speed;
y_speed = 0;
}
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
if (x_speed < 0) {
x_speed = -x_speed;
y_speed = 0;
if (x_speed > 0) {
x_speed = -x_speed;
y_speed = 0;
}
}
if (x_speed == 0) {
x_speed = 4;
y_speed = 0;
}
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
if (y_speed > 0) {
y_speed = -y_speed;
x_speed = 0;
}
if (y_speed < 0) {
y_speed = +y_speed;
x_speed = 0;
}
if (y_speed == 0) {
y_speed = -4;
x_speed = 0;
}
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
if (y_speed > 0) {
y_speed = +y_speed;
x_speed = 0;
}
if (y_speed < 0) {
y_speed = -y_speed;
x_speed = 0;
}
if (y_speed == 0) {
y_speed = +4;
}
x_speed = 0;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("HIT!");
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
How will I make the ball stop moving when no buttons are being pressed?
Under your keyReleased method, you should be able to do the opposite of your keyPressed method. That is, if you press the right arrow key, add 1 to x_speed, and when you release it, subtract 1 from x_speed, using similar logic for the other keys.