Public paste
Undefined
By: Guest | Date: Jul 1 2010 09:39 | Format: None | Expires: never | Size: 2.29 KB | Hits: 800

  1. import java.applet.*;
  2. import java.awt.*;
  3.  
  4. public class BallApplet extends Applet implements Runnable {
  5.  
  6.         int ball_x_pos = 10;
  7.         int ball_y_pos = 100;
  8.         int ball_radius = 20;
  9.        
  10.         int ball_x_speed = 1;
  11.         int ball_y_speed = 1;
  12.        
  13.         int appletsize_x = 700;
  14.         int appletsize_y = 400;
  15.        
  16.         int mouse_x_pos;
  17.         int mouse_y_pos;
  18.        
  19.         boolean isStoped;
  20.  
  21.         AudioClip bounce;
  22.         Image backImage;
  23.  
  24.         private Image dbImage;
  25.         private Graphics dbg;
  26.  
  27.         public void init() {
  28.                 bounce = getAudioClip(getCodeBase(), "bounce.au");
  29.                 backImage = getImage(getCodeBase(), "bg.jpg");
  30.         }
  31.  
  32.         public void start() {
  33.                 Thread th = new Thread(this);
  34.                 th.start();
  35.         }
  36.  
  37.         public void stop() {
  38.         }
  39.  
  40.         public void destroy() {
  41.         }
  42.  
  43.         public void run() {
  44.  
  45.                 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  46.  
  47.                 while (true) {
  48.  
  49.                         ball_x_pos += ball_x_speed;
  50.                         ball_y_pos += ball_y_speed;
  51.                        
  52.                         if (ball_x_pos > appletsize_x - ball_radius) {
  53.                                 bounce.play();
  54.                                 ball_x_speed = -1;
  55.                         } else if (ball_x_pos < ball_radius) {
  56.                                 bounce.play();
  57.                                 ball_x_speed = +1;
  58.                         }
  59.                         if (ball_x_pos > appletsize_x + ball_radius) {
  60.                                 ball_x_pos = -20;
  61.                         }
  62.                        
  63.                         if (ball_y_pos > appletsize_y - ball_radius) {
  64.                                 bounce.play();
  65.                                 ball_y_speed = -1;
  66.                         } else if (ball_y_pos < ball_radius) {
  67.                                 bounce.play();
  68.                                 ball_y_speed = +1;
  69.                         }
  70.                         if (ball_x_pos > appletsize_x + ball_radius) {
  71.                                 ball_x_pos = -20;
  72.                         }
  73.  
  74.                         repaint();
  75.  
  76.                         try {Thread.sleep(20);} catch (InterruptedException ex) {}
  77.  
  78.                         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  79.                 }
  80.         }
  81.  
  82.         public void paint(Graphics g) {
  83.                 g.drawImage(backImage, 0, 0, this);
  84.  
  85.                 g.setColor(Color.black);
  86.                 g.fillRect(10, mouse_y_pos, 5, 100);
  87.                 g.setColor(Color.red);
  88.                 g.fillOval(ball_x_pos - ball_radius, ball_y_pos - ball_radius, 2 * ball_radius, 2 * ball_radius);
  89.         }
  90.  
  91.         public void update(Graphics g) {
  92.                 if (dbImage == null) {
  93.                         dbImage = createImage(this.getSize().width, this.getSize().height);
  94.                         dbg = dbImage.getGraphics();
  95.                 }
  96.                 dbg.setColor(getBackground());
  97.                 dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);
  98.  
  99.                 dbg.setColor(getForeground());
  100.                 paint(dbg);
  101.  
  102.                 g.drawImage(dbImage, 0, 0, this);
  103.         }
  104.         public boolean mouseMove(Event e, int x, int y) {
  105.                 mouse_x_pos = x;
  106.                 mouse_y_pos = y;
  107.                 return false;  
  108.         }
  109. }