Benemerita Universidad Autonoma de Puebla Facultad de Ciencias

Transcripción

Benemerita Universidad Autonoma de Puebla Facultad de Ciencias
Benemerita Universidad Autonoma de Puebla
Facultad de Ciencias de la Computacion
Materia: Programacion Concurrente y Paralela
Profesor: Ma. Del Carmen Ceron Garnica
Alumnos:
ROGELIO MENDOZA JUAREZ 200808730
PATRICIA DOMINGUEZ MIRON 200716025
IRASEMA VAZQUEZ PEREZ 200725469
Periodo: Primavera 2013
INTRODUCCION
Un grafo es un conjunto, no vacío, de objetos llamados vértices (o nodos) y una
selección de pares de vértices, llamados aristas (edges en inglés) que pueden
ser orientados o no. Típicamente, un grafo se representa mediante una serie de
puntos (los vértices) conectados por líneas (las aristas).
Un árbol es una estructura de datos ramificada (no lineal) que puede
representarse como un conjunto de nodos enlazados entre sí por medio de
ramas. La información contenida en un nodo puede ser de cualquier tipo
simple o estructura de datos.
PLANTEAMIENTO
Construir el Grafo Grafico del ejercicio 2 del cuestionario 1
DESARROLLO DE LA PRACTICA
CLASE FRAME
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.*;
public class Frame extends JFrame
{
private JButton aceptar=new JButton("Aceptar");
private JButton limpiar=new JButton("Limpiar");
Panel p=new Panel();
public Frame()
{
super("Grafo Grafico");
setSize(550,550);
setDefaultCloseOperation(EXIT_ON_CLOSE);
aceptar.setMnemonic('a');
aceptar.setToolTipText("Dibuja Grafo");
aceptar.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt)
{
accionBoton(evt);
}
});
limpiar.setMnemonic('l');
limpiar.setToolTipText("Limpia Pantalla");
limpiar.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt)
{
accionBoton(evt);
}
});
JPanel panel=new JPanel();
BoxLayout boxarriba=new BoxLayout(panel, BoxLayout.X_AXIS);
panel.setLayout(boxarriba);
panel.add(aceptar);
//panel.add(limpiar);
JPanel panel2=new JPanel();
BoxLayout boxabajo=new BoxLayout(panel2, BoxLayout.Y_AXIS);
panel2.setLayout(boxabajo);
panel2.add(p);
panel2.add(panel);
add(panel2);
setVisible(true);
}
public void accionBoton(java.awt.event.ActionEvent evt)
{
Object obj=evt.getSource();
if(obj==aceptar)
{
Thread S1=new Hilo(1000,"S1",p,200,50,200,50);
Thread S2=new Hilo(2000,"S2",p,150,150,200,50);
Thread S5=new Hilo(2020,"S5",p,250,150,200,50);
Thread S3=new Hilo(3000,"S3",p,100,250,150,150);
Thread S4=new Hilo(3020,"S4",p,200,250,150,150);
Thread S6=new Hilo(4000,"S6",p,250,380,250,150);
S1.start();
S2.start();
S3.start();
S4.start();
S5.start();
S6.start();
/////////////////////////////////////////////////////
/*Thread S1=new Hilo(1000,"S1",p,500,100,300,100);
Thread S2=new Hilo(2000,"S2",p,500,100,300,100);
Thread S5=new Hilo(2020,"S5",p,500,100,300,100);
Thread S3=new Hilo(3000,"S3",p,500,100,300,100);
Thread S4=new Hilo(3020,"S4",p,500,100,300,100);
Thread S6=new Hilo(4000,"S6",p,500,100,300,100);
S1.start();
S2.start();
S3.start();
S4.start();
S5.start();
S6.start();*/
}
}
public static void main(String[] args)
{
Frame F=new Frame();
}
}
CLASE HILO
public class Hilo extends Thread
{
int time;
String name;
Panel p;
int x, y;
int xo,yo;
public Hilo(int time, String name, Panel p,int x, int y,int xo,int yo)
{
this.time=time;
this.name=name;
this.p=p;
this.x=x;
this.y=y;
this.xo=xo;
this.yo=yo;
}
public void run()
{
try
{
sleep(time);
System.out.println(name);
p.Dibuja(x, y,xo,yo, name);
}catch(InterruptedException e){}
}
}
CLASE PANEL
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
public class Panel extends JPanel
{
int x, y, xo, yo;
String name;
public void Dibuja(int x, int y, int xo, int yo, String name)
{
this.x=x;
this.y=y;
this.name=name;
this.xo=xo;
this.yo=yo;
paintComponent(this.getGraphics());
}
public void paintComponent(Graphics g)
{
g.setColor(Color.lightGray);
g.fillOval(x,y,50,50);
g.setColor(Color.GREEN);
g.drawString(name, x+18, y+30);
if(name!="S1")
{
g.setColor(Color.BLACK);
g.drawLine(x+25, y, xo+25, yo+50);
}
if(name=="S6")
{
g.setColor(Color.BLACK);
g.drawLine(x+25, y, 100+25, 250+50);
g.drawLine(x+25, y, 200+25, 250+50);
}
}
}
CONCLUSION
Logramos apreciar de una mejor forma el comportamiento de los hilos con
clase extends utilizando una representacion grafica.

Documentos relacionados