Clase principal: Caballos
Clase secundaria, donde esta el constructor de cada caballo: Caballo
CABALLOS.CLASS:
Código:
import java.applet.*; import java.awt.*; public class Caballos extends Applet { Label l1,l2,l3,l4,l5, l6, l7,l8, l9; Color co; int x,y, metros, count, a = 20*count, dorsal; Panel p; String[] po; public void init() { l1 = new Label("Carrera de caballos"); l2 = new Label("Resultados"); l3 = new Label("Ganador:"); l4 = new Label("Segundo:"); l6 = new Label(); l7 = new Label(); l8 = new Label("Clasificaci�"); l9 = new Label(); p = new Panel(new BorderLayout()); p.setVisible(true); p.setBackground(Color.CYAN); p.add(l2); p.add(l3); p.add(l4); p.add(l6); p.add(l7); p.add(l8); p.add(l9); co = new Color(255,255,255); add(l1); add(p); setLayout(null); l1.setBounds(250,10,150,30); p.setBounds(300,100,250,500); l2.setBounds(50,5,150,30); l3.setBounds(10,50,90,30); l4.setBounds(10,100,90,30); l6.setBounds(100,50,100,30); l7.setBounds(100,100,100,30); l8.setBounds(50,120,200,30); l9.setBounds(50,120,200,a); setBackground(co); count = (int)(Math.random()*15+4); metros = (int)(Math.random()*300+1000); Caballo[] ca = new Caballo[count]; for(int i=0; i<count; i++) { ca[i] = new Caballo(i+1,metros,l6,l7); } for (int i=0; i<count; i++) { new Thread (ca[i]).start(); } } public void posicionar(String[] po1) { po = po1; System.out.println(po.length); for(int i=0; i<=po.length; i++) { l9.setText("Dorsal: " + po[i] + "\r"); } } }
CABALLO.CLASS:
Código:
import java.awt.*; import java.applet.*; public class Caballo extends Panel implements Runnable { int dorsal, y = 50, metros, avancealeatorio, pos; Label l6, l7; Canvas c; String[] cas; Caballos cab; public Caballo(int d, int m, Label g, Label s) { dorsal = d; metros = m; l6 = g; l7 = s; cab = (Caballos)l6.getParent(); setLayout(null); Label num = new Label("" + dorsal); num.setBounds(10,y+22*dorsal,30,20); add(num); c = new Canvas(); c.setBounds(50,y+22*dorsal,1,20); c.setBackground(Color.blue); add(c); } public void run() { pos = 0; while (pos < metros) { pos += (int)(Math.random()*0.02+5); if (pos > metros) pos = metros; c.setSize(pos*200/metros,20); } if(pos == metros) { cas = new String[dorsal]; cab.posicionar(cas); } if (l6.getText().equalsIgnoreCase("")) l6.setText("Dorsal: " + dorsal); else { if (l7.getText().equalsIgnoreCase("")) l7.setText("Dorsal: " + dorsal); } } }
bichomen