-
-
-
- import java.applet.*;
- import java.awt.*;
- import javax.swing.*;
-
- public class ball extends Applet implements Runnable {
- int x_pos = 10;
-
- int y_pos = 100;
-
- int radius = 20;
-
- private Image image;
-
- private Graphics graphic;
-
- public void init() {
- setBackground(Color.blue);
- }
-
- public void start() {
- Thread th = new Thread( this);
- th.start();
- }
-
- public void stop() {
- }
-
- public void destroy() {
- }
-
- public void run() {
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
- while ( true ) {
- x_pos++;
- repaint();
- try {
- Thread.sleep(20);
- }
- catch (InterruptedException ie) {
- }
- Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
- }
- }
-
- public void paint(Graphics g) {
- g.setColor(Color.red);
- g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
- }
-
- public void update(Graphics g) {
-
- if ( image == null ) {
- image = createImage(this.getSize().width, this.getSize().height);
- graphic = image.getGraphics();
- }
-
- graphic.setColor(getBackground());
- graphic.fillRect(0, 0, this.getSize().width, this.getSize().height);
-
- graphic.setColor(getForeground());
- paint(graphic);
-
- g.drawImage(image, 0, 0, this);
- }
- }