Sei sulla pagina 1di 3

TOPICOS SELECTOS DE PROGRAMACIÓN

PRACTICA 5 – HILOS

En esta práctica vamos a crear hilos (Threads) en Java. Los hilos van a comunicarse a
través de un objeto que servirá para controlar la velocidad en que se mueven los objetos
gráficos de cada hilo así como para suspender o terminar la ejecución de los hilos.

El botón de disminuir, disminuye la velocidad con la que se mueven los puntos blancos,
el de Aumentar, aumenta la velocidad. El botón de Suspender, provoca que los puntos
se detengan y el botón de terminar provoca que los “Threads” de cada punto terminen.
El código que crea los hilos es el siguiente:

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

public class Practica5 extends Frame{


Button inicio;
Control c = new Control();

public Practica5(){
super("Ejemplo de Hilos");
setSize(420,420);
setLayout(new FlowLayout());
inicio = new Button("Iniciar");
add(inicio);
setBackground(Color.BLACK);
c.setLocation(50,50);
c.setVisible(true);
setLocation(500,50);
setVisible(true);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
inicio.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Graphics g = getGraphics();
Balon b = new Balon(g,c);
b.start();
}
});
}

public static void main(String[] args) {


Practica5 p = new Practica5();
}

private class Balon extends Thread{


int x, y, diametro;
boolean direccionX=true;
boolean direccionY=true;
Graphics g;
Control c;

public Balon(Graphics g, Control c){


x=(int)(Math.random()*300+1);
y=(int)(Math.random()*300+1);
diametro=10;
this.g = g;
this.c = c;
}

public void run(){


int incX,incY;
while(!c.salir){
g.setColor(Color.BLACK);
g.fillOval(x,y,diametro,diametro);
if (direccionX) incX = c.IncX;
else incX = -c.IncX;
if(x + incX <= 0 || x + incX >= 400)
direccionX = !direccionX;
if (direccionY) incY = c.IncY;
else incY = -c.IncY;
if(y + incY <= 30 || y + incY >= 400)
direccionY = !direccionY;
x+=incX;
y+=incY;
g.setColor(Color.WHITE);
g.fillOval(x,y,diametro,diametro);
try{
sleep(50);
} catch(InterruptedException e){
System.err.println("Error de excepcion");
}
}
}
}
}
El código para cada uno de los botones de la clase Control es el siguiente:

public class Control extends javax.swing.JFrame {


int IncX=1;
int IncY=1;
boolean salir = false;
/** Creates new form Control */
public Control() {

}

private void SuspenderActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
IncX=0;
IncY=0;
}

private void AumentarActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
IncX+=1;
IncY+=1;
if (IncX > 10) IncX = 10;
if (IncY > 10) IncY = 10;
}

private void DisminuirActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
IncX-=1;
IncY-=1;
if (IncX < -10) IncX = -10;
if (IncY < -10) IncY = -10;
}

private void TerminarActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
salir = true;
}


}

Usando Netbeans, complete el código de la clase Control para hacer funcionar el programa. Puede
utilizar la ayuda de programación visual de Netbeans.

Potrebbero piacerti anche