You are on page 1of 4

INGENIERIA DE SISTEMAS

PROGRAMACION II (GRUPO 2)

FRAMES
THREADS (Multihilos)
Los threads (hilos) permiten realizar procesos paralelos en un programa en java.
El cdigo que realiza el verdadero trabajo de un hilo se coloca un su mtodo run(), El mtodo run puede supeditarse
en una subclase de Thread o bien en un objeto Runnable (ejecutable): Runnable es una interface importante de java.
Un programa inicia la ejecucin de un hilo invocando al mtodo start() de ese hilo; a su vez el mtodo start() invoca al
mtodo run().
El mtodo static sleep (dormir) se invoca con un argumento que especifica durante cuanto tiempo el hilo que se est
ejecutando debe dormir (en segundos).
Ejemplo 1.
import java.awt.*;
class frame3 extends Frame implements Runnable
{
Thread hilo1=new Thread(this);
boolean activo=true;
Button b1=new Button("ok");
Label l=new Label();
int y=100;
int x=100;
public void run()
{
while(activo)
{
x=x+5;
b1.setLocation(x,y);
try
{
Thread.sleep(500);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public frame3()
{
setSize(400,400);
setLocation(100,100);
add(b1);
b1.setSize(40,40);b1.setLocation(x,y);
add(l);
hilo1.start();
show();
}
public static void main(String args[])
{
new frame2();

}
}

Ejemplo 2 (mejorado)
import java.awt.*;
class frame2 extends Frame implements Runnable
{
Thread hilo1=new Thread(this);
boolean arriba=false,abajo=false,der=false,izq=false;
boolean activo=true;
Button b1=new Button("ok");
Label l=new Label();
int y=100;
int x=100;
public void run()
{
while(activo)
{
if(arriba==true)
y=y-5;
if(abajo==true)
y=y+5;
if(der==true)
x=x+5;
if(izq==true)
x=x-5;
b1.setLocation(x,y);
try
{
Thread.sleep(50); //tiempo que duerme
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public frame2()
{
setSize(400,400);
setLocation(100,100);
add(b1);
b1.setSize(40,40);b1.setLocation(x,y);
add(l);
hilo1.start();
show();
}
public boolean keyDown(Event e,int key)

{
if(key==1004) //tecla arriba
{
arriba=true;abajo=false;der=false;izq=false;
}
if(key==1005) //tecla abajo
{
arriba=false;abajo=true;der=false;izq=false;
}
if(key==1006) //tecla izquierda
{
arriba=false;abajo=false;der=false;izq=true;
}
if(key==1007) //tecla derecha
{
arriba=false;abajo=false;der=true;izq=false;
}
return true;
}
public static void main(String args[])
{
new frame2();
}
}
EJEMPLO 3
/* Programa que utiliza una clase miboton el cual hereda de la clase Button, el programa genera 2 botones en
posiciones diferentes los cuales van cayendo gracias a la utilizacin de un hilo (Thread). */
import java.awt.*;
class ejemplo extends Frame implements Runnable
{
Thread t=new Thread(this);
miboton b[]=new miboton[2];
Label l=new Label();
int x,y;
int cant=0;
public ejemplo()
{
for(int i=0;i<2;i++)
{
b[i]=new miboton();
int px=b[i].getPosX();
int py=b[i].getPosY();
b[i].setSize(20,20);b[i].setLocation(px,py);
add(b[i]);
}
add(l);
t.start();
// inicia el hilo
setSize(300,300);
setLocation(100,100);

show();
}
public void run()
{
while(1==1)
{
for(int i=0;i<2;i++)
{
b[i].caer();
int px=b[i].getPosX();
int py=b[i].getPosY();
b[i].setLocation(px,py);
}
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void main(String args[])
{
new ejemplo();
}
}
class miboton extends Button
{
int x=(int)(Math.random()*250);
int y=30;
public void caer()
{
y=y+5;
}
public int getPosX()
{
return x;
}
public int getPosY()
{
return y;
}
}

You might also like