Untitled
By: a guest | Feb 9th, 2010 | Syntax:
None | Size: 0.98 KB | Hits: 36 | Expires: Never
package BallApplet;
import java.applet.Applet;
import java.awt.*;
public class BallApplet extends Applet implements Runnable {
private static final long serialVersionUID = 1L;
// Some member variables
private int ballx, bally; // current position of balls
private int size, speed;
private int dx, dy; // current speed in x & y axes
// Called automatically when applet starts-up
public void init() {
setBackground(Color.blue);
ballx = 0;
bally = 0;
speed = 4;
dx = speed;
dy = speed;
size = 50;
}
// Called after init, and after any pause
public void start() {
Thread th = new Thread(this);
th.start();
}
public void stop(){}
public void destroy(){}
public void paint(Graphics g ) {
ballx += dx; bally += dy;
g.setColor(Color.RED);
g.fillOval(ballx, bally, size, size);
}
public void run() {
while(true){
repaint();
try {
Thread.sleep(40);
}
catch(InterruptedException ex) {}
}
}
}