home *** CD-ROM | disk | FTP | other *** search
/ Discovering Windows 98 / WinExpert9.iso / Tips / JavaSource.zip / Bumper.java < prev    next >
Text File  |  1997-11-05  |  3KB  |  120 lines

  1. import java.awt.*;
  2.  
  3. public class Bumper extends java.applet.Applet implements Runnable {
  4.     Thread runner;
  5.         Graphics offscreenG;
  6.         Image offscreenImg;
  7.     int[][] positions = new int[5][2];
  8.     double[][] directions = new double[5][2];
  9.     Color[] colours = new Color[5];
  10.     boolean[] bounced = new boolean[5];
  11.  
  12.     public void init () {
  13.         int i, j;
  14.         boolean overlaps, cont;
  15.  
  16.         setBackground(Color.white);
  17.         offscreenImg = createImage (size().width, size().height);
  18.                 offscreenG = offscreenImg.getGraphics();
  19.         for (i = 0; i < 5; i++) {
  20.             overlaps = false;
  21.             cont = true;
  22.             while (cont == true) {
  23.                 positions[i][0] = (int)(Math.random() * (size().width - 30)) + 5;
  24.                 positions[i][1] = (int)(Math.random() * (size().height - 30)) + 5;
  25.                 for (j = 0; j < i; j++) {
  26.                     if ((Math.abs(positions[i][0] - positions[j][0]) <= 20) && (Math.abs(positions[i][1] - positions[j][1]) <= 20)) {
  27.                         overlaps = true;
  28.                         }
  29.                     }
  30.                 if (overlaps == false) {
  31.                     cont = false;
  32.                     }
  33.                 }
  34.             }
  35.         for (i = 0; i < 5; i++) {
  36.             directions[i][0] = (double)((Math.random() * 10) - 5);
  37.             directions[i][1] = (double)((Math.random() * 10) - 5);
  38.             }
  39.         colours[0] = Color.blue;
  40.         colours[1] = Color.red;
  41.         colours[2] = Color.green;
  42.         colours[3] = Color.yellow;
  43.         colours[4] = Color.magenta;
  44.         }
  45.  
  46.     public void start () {
  47.                 if (runner == null) {
  48.                         runner = new Thread(this);
  49.                         runner.start();
  50.                         }
  51.                 }
  52.  
  53.         public void stop () {
  54.                 if (runner != null) {
  55.                         runner.stop();
  56.                         runner = null;
  57.                         }
  58.                 }
  59.  
  60.         public void run () {
  61.         int i, j;
  62.         double a, b;
  63.         
  64.                 while (true) {
  65.             for (i = 0; i < 5; i++) {
  66.                 if ((positions[i][0] + directions[i][0] >= size().width - 20) || (positions[i][0] + directions[i][0] <= 0)) {
  67.                     directions[i][0] *= -1;
  68.                     }
  69.                 else {
  70.                     positions[i][0] += (int)(directions[i][0]);
  71.                     }
  72.                 if ((positions[i][1] + directions[i][1] >= size().height - 20) || (positions[i][1] + directions[i][1] <= 0)) {
  73.                     directions[i][1] *= -1;
  74.                     }
  75.                 else {
  76.                     positions[i][1] += (int)(directions[i][1]);
  77.                     }
  78.                 bounced[i] = false;
  79.                 }
  80.             for (i = 0; i < 5; i++) {
  81.                 for (j = 0; j < i; j++) {
  82.                     if ((Math.abs(positions[i][0] - positions[j][0]) <= 20) && (Math.abs(positions[i][1] - positions[j][1]) <= 20) && (bounced[i] == false) && (bounced[j] == false)) {
  83.                         a = directions[i][0];
  84.                         b = directions[i][1];
  85.                         directions[i][0] = directions[j][0];
  86.                         directions[i][1] = directions[j][1];
  87.                         directions[j][0] = a;
  88.                         directions[j][1] = b;
  89.                         bounced[i] = bounced[j] = true;
  90.                         }
  91.                     }
  92.                 }
  93.             try {
  94.                 runner.sleep(100);
  95.                 }
  96.             catch (InterruptedException e) {
  97.                 }
  98.             repaint();
  99.                         }
  100.         }
  101.  
  102.     public void update(Graphics g) {
  103.                 g.clipRect(0, 0, size().width, size().height);
  104.                 paint(g);
  105.         }
  106.                         
  107.     public void paint(Graphics g) {
  108.         int i;
  109.  
  110.         offscreenG.setColor(Color.white);
  111.         offscreenG.fillRect(0, 0, size().width, size().height);
  112.  
  113.         for (i = 0; i < 5; i++) {
  114.             offscreenG.setColor(colours[i]);
  115.             offscreenG.fillOval(positions[i][0], positions[i][1], 20, 20);
  116.             }
  117.  
  118.                 g.drawImage(offscreenImg, 0, 0, this);
  119.         }
  120.     }