You are on page 1of 10

UNIVERSIDAD POLITCNICA DEL VALLE DE TOLUCA

NOMBRE DEL PROGRAMA EDUCATIVO:


INGENIERA INFORMTICA

NOMBRE DEL ASIGNATURA:


PROGRAMACION ORIENTADA A OBJETOS

NOMBRE DE LA PRCTICA:
PROYECTO
NOMBRE DE LA UNIDAD DE APRENDIZAJE:
UNIDAD 2: HILOS

FACILITADOR:
ING. LETICIA JAEL ROJAS ORTIZ
ALUMNO:
No
1
2

Matrcula
1417INI110
1314INI315

Nombre
DORIS SANCHEZ TORRES
ROSARIO GUADALUPE BANDERA MORAN
FEBRERO, 2016

CODIGO:

package Proyecto;
import java.awt.*;
import java.awt.event.*;
public class Restaurante extends Ven implements ActionListener
MiThread mt1,mt2,mt3,mt4,mt5, mt6, mt7;
//programa principal
public static void main (String args[]){

Restaurante carr= new Restaurante("La comida Feliz");

carr.setSize(450,400);
carr.setLocation(400,400);
carr.show();
}

//constructor

public Restaurante (String titulo){


super (titulo);
MiPanel mp= new MiPanel(); // se crea un objeto clase Mipanel
this.add(mp,"Center");
Button b= new Button("SERVIR");//se crea el boton que arrancar las threads
b.addActionListener(this);//se registra el event listener
Panel pan= new Panel();
pan.add(b);
this.add(pan,"South");// se aade el boton dentro del panel
//se crean los 3 threads
mt1=new MiThread("Cliente 1",mp);
mt2=new MiThread("Cliente 2",mp);
mt3=new MiThread("Cliente 3",mp);
mt4=new MiThread("Cliente 4",mp);
mt5=new MiThread("Cliente 5",mp);

mt6=new MiThread("Mesero 1",mp);


mt7=new MiThread("Mesero 2",mp);

public void actionPerformed(ActionEvent e){

mt1.start();

mt2.start();

mt3.start();
mt4.start();
mt5.start();
mt6.start();
mt7.start();

}
//

//clase mi panel que redefine el metodo paint()


class MiPanel extends Panel{
//variable de miembro

public int x1, x2, x3, x4,x5,x6,x7, finish;

//constructor
public MiPanel(){
super () ;
finish=300;

//redefinir el metodo paint


public void paint(Graphics g){
//primer cliente

g.setColor(Color.black);
g.fillOval(x1,10,15,15);
//segundo cliente
g.setColor(Color.black);
g.fillOval(x2,65,15,15);
// tercer cliente
g.setColor(Color.black);
g.fillOval(x3,105,15,15);

// cuarto cliente
g.setColor(Color.black);
g.fillOval(x4,155,15,15);
// quinto cliente
g.setColor(Color.black);
g.fillOval(x5,205,15,15);

// mesero 1

g.setColor(Color.red);
g.fillOval(x6,35,15,15);

//mesero 2
g.setColor(Color.red);
g.fillOval(x7,135,15,15);

//mesa 1

g.setColor(Color.blue);
g.fillRect(300,120,30,95);

// mesa 2
g.setColor(Color.blue);
g.fillRect(300,10,30,95);

g.drawString("Grupo de Clientes atendidos ", 5, 260);

}// fin de clase miPanel

//clase miThread que define el metodo run()

class MiThread extends Thread{


// variable miembro referencia miPanel para poder dar valor a x1, x2 y x3
MiPanel mp;
//constructor
public MiThread(String str, MiPanel mip){
super (str);
mp=mip;

//metodo run ()
public void run(){
boolean running =true;
//se obtiene la posicion de la meta del objeto mp
int finish=mp.finish;
//condicion para terminar el metodo run

while (running){
//se para el thread unos milisegundos
try{
sleep((int)(Math.random()*500));

}
catch(InterruptedException e){}
// se obtiene el nombre del thread
String threadname = this.getName();
meta

//se distingue el movil con que corresponde el thread y se comprueba si ha llegado a la


if (threadname.compareTo("Cliente 1")==0){
mp.x1+=5;

if (mp.x1>finish) running=false;

}
else if (threadname.compareTo("Cliente 2")==0){
mp.x2+=5;
if (mp.x2>finish) running=false;
}
else if (threadname.compareTo("Cliente 3")==0){
mp.x3+=5;
if (mp.x3>finish) running=false;

else if (threadname.compareTo("Cliente 4")==0){


mp.x4+=5;
if (mp.x4>finish) running=false;
}
else if (threadname.compareTo("Cliente 5")==0){
mp.x5+=5;
if (mp.x5>finish) running=false;
}

else if (threadname.compareTo("Mesero 1")==0){


mp.x6+=5;
if (mp.x6>finish) running=false;
}

else {
//azul
mp.x7+=5;
if (mp.x7>finish) running =false;

// habiendo cambiando un movil se mueve a dibujar


mp.repaint();
}

}// fin de la clase Mithread

package Proyecto;

import java.awt.*;
import java.awt.event.*;

public class Ven extends Frame {


//constructor
public Ven(String titulo){
super(titulo);
setSize(300,200);
setLocation(200,300);
// SE REGISTRA un objeto sin nombre de la clase winsowAdapter
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);

});
}
}

RESULTADO:

You might also like