home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-11-05 | 3.4 KB | 120 lines |
- import java.awt.*;
-
- public class Bumper extends java.applet.Applet implements Runnable {
- Thread runner;
- Graphics offscreenG;
- Image offscreenImg;
- int[][] positions = new int[5][2];
- double[][] directions = new double[5][2];
- Color[] colours = new Color[5];
- boolean[] bounced = new boolean[5];
-
- public void init () {
- int i, j;
- boolean overlaps, cont;
-
- setBackground(Color.white);
- offscreenImg = createImage (size().width, size().height);
- offscreenG = offscreenImg.getGraphics();
- for (i = 0; i < 5; i++) {
- overlaps = false;
- cont = true;
- while (cont == true) {
- positions[i][0] = (int)(Math.random() * (size().width - 30)) + 5;
- positions[i][1] = (int)(Math.random() * (size().height - 30)) + 5;
- for (j = 0; j < i; j++) {
- if ((Math.abs(positions[i][0] - positions[j][0]) <= 20) && (Math.abs(positions[i][1] - positions[j][1]) <= 20)) {
- overlaps = true;
- }
- }
- if (overlaps == false) {
- cont = false;
- }
- }
- }
- for (i = 0; i < 5; i++) {
- directions[i][0] = (double)((Math.random() * 10) - 5);
- directions[i][1] = (double)((Math.random() * 10) - 5);
- }
- colours[0] = Color.blue;
- colours[1] = Color.red;
- colours[2] = Color.green;
- colours[3] = Color.yellow;
- colours[4] = Color.magenta;
- }
-
- public void start () {
- if (runner == null) {
- runner = new Thread(this);
- runner.start();
- }
- }
-
- public void stop () {
- if (runner != null) {
- runner.stop();
- runner = null;
- }
- }
-
- public void run () {
- int i, j;
- double a, b;
-
- while (true) {
- for (i = 0; i < 5; i++) {
- if ((positions[i][0] + directions[i][0] >= size().width - 20) || (positions[i][0] + directions[i][0] <= 0)) {
- directions[i][0] *= -1;
- }
- else {
- positions[i][0] += (int)(directions[i][0]);
- }
- if ((positions[i][1] + directions[i][1] >= size().height - 20) || (positions[i][1] + directions[i][1] <= 0)) {
- directions[i][1] *= -1;
- }
- else {
- positions[i][1] += (int)(directions[i][1]);
- }
- bounced[i] = false;
- }
- for (i = 0; i < 5; i++) {
- for (j = 0; j < i; j++) {
- 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)) {
- a = directions[i][0];
- b = directions[i][1];
- directions[i][0] = directions[j][0];
- directions[i][1] = directions[j][1];
- directions[j][0] = a;
- directions[j][1] = b;
- bounced[i] = bounced[j] = true;
- }
- }
- }
- try {
- runner.sleep(100);
- }
- catch (InterruptedException e) {
- }
- repaint();
- }
- }
-
- public void update(Graphics g) {
- g.clipRect(0, 0, size().width, size().height);
- paint(g);
- }
-
- public void paint(Graphics g) {
- int i;
-
- offscreenG.setColor(Color.white);
- offscreenG.fillRect(0, 0, size().width, size().height);
-
- for (i = 0; i < 5; i++) {
- offscreenG.setColor(colours[i]);
- offscreenG.fillOval(positions[i][0], positions[i][1], 20, 20);
- }
-
- g.drawImage(offscreenImg, 0, 0, this);
- }
- }