home *** CD-ROM | disk | FTP | other *** search
/ Discovering Windows 98 / WinExpert9.iso / Tips / JAVA3.txt < prev    next >
Text File  |  1997-11-27  |  4KB  |  123 lines

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