4. if(co != 5){
g.setColor(pelota5.color);
}else{
g.setColor(Color.black);
}
g.fillOval(pelota5.xh,pelota5.yh, pelota5.tam,pelota5.tam);
}
}
public void start() {
if (hilo==null) {
hilo = new Thread(this);
hilo.start();
}
}
public void run() {
while (hilo!=null && hilo.isAlive())
{
try {
hilo.sleep(tiempoactual);
}
catch (InterruptedException e) {} // No hacemos nada
repaint(); // Llama al paint()
}
}
public void stop() {
hilo = null;
}
5. }
import java.awt.Color;
public class HiloMover extends Thread{
int sleepTime,xh,yh,vel,tam;
int banx=0, bany=0,cont=-1;
Color color;
public HiloMover (String id, int x,int y, int tiempo, int velocidad, Color gc, int tamaño)
{
super (id);
// dormir entre o y 5 segundos
sleepTime = tiempo; //(int)(Math.random()*5000);
xh=x;
yh=y;
vel = velocidad;
color= gc;
tam = tamaño;
}
// ejecutar hilo
public void run()
{
int x=1; // poner el hilo a dormir durante un intervalo aleatorio
while (x ==1) // while (hilo!=null && hilo.isAlive())
{
try{
sleep(sleepTime);
6. }
catch(InterruptedException exception){
System.err.println("Excepción: " + exception.toString());
}
if (banx ==0)
xh=xh+vel;
else
xh=xh-vel;
if (bany ==0)
yh=yh-vel;
else
yh=yh+vel;
if (xh >800){
banx=1;
if(cont != -1)
cont++;
}
if (xh <=0){
banx=0;
if(cont != -1)
cont++;
}
if (yh <=0){
bany=1;
if(cont != -1)
cont++;