PruebaMuchos.java
Categorías: CategoryJava | CategoryProgramacion |
1 /**
2 * pruebaMuchos.java
3 *
4 * @author Luis Alejandro Bernal Romero
5 *
6 * Prueba el motor de juegos con muchos objetos. En esta versión el motor ya tiene doble buffe.
7 */
8
9 package pruebaMuchos3;
10
11
12 import java.awt.Color;
13 import java.awt.Frame;
14 import java.awt.Graphics;
15
16 import motorJuegos3.*;
17
18 class Circulo extends ObjetoMovil implements Runnable{
19 private Color color;
20 private int ancho, alto;
21 private Frame marco;
22
23 public Circulo(int x, int y, int an, int al, Color c, Frame m) {
24 super(x, y);
25 color = c;
26 ancho = an;
27 alto = al;
28 marco = m;
29 }
30
31 public void paint(Graphics g) {
32 g.setColor(color);
33 g.fillOval(obtX(), obtY(), ancho, alto);
34 }
35
36 public void run() {
37 for(;;){
38 int x, y;
39 do{
40 x = obtX() + (int)(Math.random() * ancho / 2) * (((int)(Math.random() * 2) == 0)? 1 : -1);
41 }while(x > marco.getWidth() || x < 0);
42 modX(x);
43 do{
44 y = obtY() + (int)(Math.random() * alto / 2) * (((int)(Math.random() * 2) == 0)? 1 : -1);
45 }while(y > marco.getHeight() || y < 0);
46 modX(x);
47 modY(y);
48 //marco.repaint();
49 try{Thread.sleep(50);}catch (Exception e) {}
50 }
51 }
52
53 }
54
55 public class PruebaMuchos {
56 public static void main(String[] args) {
57 MotorJuegos motor = new MotorJuegos("Muchos objetos tres", Color.white);
58 Circulo[] cir = new Circulo[100];
59 Thread[] h = new Thread[100];
60 motor.setSize(1024, 800);
61 for(int i = 0; i < 100; i++){
62 cir[i] = new Circulo((int)(Math.random() * motor.getWidth()),
63 (int)(Math.random() * motor.getHeight()),
64 (int)(Math.random() * 100 + 10),
65 (int)(Math.random() * 100 + 20),
66 new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256)),
67 motor);
68 motor.add(cir[i]);
69 h[i] = new Thread(cir[i]);
70 h[i].start();
71 }
72 motor.setVisible(true);
73 motor.iniciar();
74 }
75
76 }
