Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 0.98 KB | Hits: 36 | Expires: Never
Copy text to clipboard
  1. package BallApplet;
  2. import java.applet.Applet;
  3. import java.awt.*;
  4.  
  5. public class BallApplet extends Applet implements Runnable {
  6.         private static final long serialVersionUID = 1L;
  7. // Some member variables
  8.  private int ballx, bally; // current position of balls
  9.  private int size, speed;
  10.  private int dx, dy;       // current speed in x & y axes
  11.  // Called automatically when applet starts-up
  12.         public void init() {
  13.                 setBackground(Color.blue);
  14.                 ballx = 0;
  15.                 bally = 0;
  16.                 speed = 4;
  17.                 dx = speed;
  18.                 dy = speed;
  19.                 size = 50;
  20.         }
  21.  
  22.  // Called after init, and after any pause
  23.         public void start() {
  24.                 Thread th = new Thread(this);
  25.                 th.start();
  26.         }
  27.  
  28.         public void stop(){}
  29.  
  30.         public void destroy(){}
  31.  
  32.         public void paint(Graphics g ) {
  33.                 ballx += dx; bally += dy;
  34.                 g.setColor(Color.RED);
  35.                 g.fillOval(ballx, bally, size, size);
  36.         }
  37.  
  38.         public void run() {
  39.                 while(true){
  40.                         repaint();
  41.                         try {
  42.                                 Thread.sleep(40);
  43.                         }
  44.                         catch(InterruptedException ex) {}
  45.                 }
  46.         }
  47. }