import java.awt.*;
import java.applet.Applet;
public class mOVERH extends Applet implements Runnable{
Thread hilo= null;
int tiempoactual=200,co=1,ct;
HiloMover pelota1,pelota2,pelota3,pelota4,pelota5;
public void init(){ // x y tmp vel color
pelota1 = new HiloMover ("1",100,500,200,15,Color.GREEN,35);
pelota2 = new HiloMover ("2",200,200,200,10,Color.BLUE,70);
pelota3 = new HiloMover ("3",500,100,200,10,Color.RED,50);
pelota4 = new HiloMover ("4",50,600,200,10,Color.yellow,50);
pelota5 = new HiloMover ("5",350,10,200,10,Color.CYAN,50);
pelota1.cont=0;
pelota1.start();
pelota2.start();
pelota3.start();
pelota4.start();
pelota5.start();
}
public boolean action ( Event evt, Object obj)
{
return true;
}
public void paint(Graphics g)
{
this.setSize(850, 600);
//pelota 1
if(co != 5){
g.setColor(pelota1.color);
}else{
g.setColor(Color.BLACK);
}
g.fillOval(pelota1.xh,pelota1.yh, pelota1.tam,pelota1.tam);
//pelota 2
if(pelota1.cont>=3){
if(pelota2.cont==-1){
pelota2.cont=0;
co++;
}
if(co != 5){
g.setColor(pelota2.color);
}else{
g.setColor(Color.BLACK);
}
g.fillOval(pelota2.xh,pelota2.yh, pelota2.tam,pelota2.tam);
}
//pelota 3
if(pelota1.cont+pelota2.cont>=6){
if(pelota3.cont==-1){
pelota3.cont=0;
co++;
}
if(co != 5){
g.setColor(pelota3.color);
}else{
g.setColor(Color.BLACK);
}
g.fillOval(pelota3.xh,pelota3.yh, pelota3.tam,pelota3.tam);
}
//pelota 4
if(pelota1.cont+pelota2.cont+pelota3.cont >=9){
if(pelota4.cont == -1){
pelota4.cont = 0;
co++;
}
if(co != 5){
g.setColor(pelota4.color);
}else{
g.setColor(Color.BLACK);
}
g.fillOval(pelota4.xh,pelota4.yh, pelota4.tam,pelota4.tam);
}
//pelota 5
if(pelota1.cont+pelota2.cont+pelota3.cont+pelota4.cont >=12){
if(pelota5.cont == -1){
pelota5.cont = 0;
co++;
}
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;
}
}
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);
}
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++;
}
if (yh >550){
bany=0;
if(cont != -1)
cont++;
}
}
}
}
programa pelotas java
programa pelotas java
programa pelotas java
programa pelotas java

programa pelotas java

  • 1.
    import java.awt.*; import java.applet.Applet; publicclass mOVERH extends Applet implements Runnable{ Thread hilo= null; int tiempoactual=200,co=1,ct; HiloMover pelota1,pelota2,pelota3,pelota4,pelota5; public void init(){ // x y tmp vel color pelota1 = new HiloMover ("1",100,500,200,15,Color.GREEN,35); pelota2 = new HiloMover ("2",200,200,200,10,Color.BLUE,70); pelota3 = new HiloMover ("3",500,100,200,10,Color.RED,50); pelota4 = new HiloMover ("4",50,600,200,10,Color.yellow,50); pelota5 = new HiloMover ("5",350,10,200,10,Color.CYAN,50); pelota1.cont=0; pelota1.start(); pelota2.start(); pelota3.start(); pelota4.start(); pelota5.start(); } public boolean action ( Event evt, Object obj) { return true; }
  • 2.
    public void paint(Graphicsg) { this.setSize(850, 600); //pelota 1 if(co != 5){ g.setColor(pelota1.color); }else{ g.setColor(Color.BLACK); } g.fillOval(pelota1.xh,pelota1.yh, pelota1.tam,pelota1.tam); //pelota 2 if(pelota1.cont>=3){ if(pelota2.cont==-1){ pelota2.cont=0; co++; } if(co != 5){ g.setColor(pelota2.color); }else{ g.setColor(Color.BLACK); } g.fillOval(pelota2.xh,pelota2.yh, pelota2.tam,pelota2.tam); } //pelota 3 if(pelota1.cont+pelota2.cont>=6){ if(pelota3.cont==-1){
  • 3.
    pelota3.cont=0; co++; } if(co != 5){ g.setColor(pelota3.color); }else{ g.setColor(Color.BLACK); } g.fillOval(pelota3.xh,pelota3.yh,pelota3.tam,pelota3.tam); } //pelota 4 if(pelota1.cont+pelota2.cont+pelota3.cont >=9){ if(pelota4.cont == -1){ pelota4.cont = 0; co++; } if(co != 5){ g.setColor(pelota4.color); }else{ g.setColor(Color.BLACK); } g.fillOval(pelota4.xh,pelota4.yh, pelota4.tam,pelota4.tam); } //pelota 5 if(pelota1.cont+pelota2.cont+pelota3.cont+pelota4.cont >=12){ if(pelota5.cont == -1){ pelota5.cont = 0; co++; }
  • 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 classHiloMover 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++;
  • 7.
    } if (yh >550){ bany=0; if(cont!= -1) cont++; } } } }