import java.applet.*;
import java.awt.event.*;
public class semaforo extends Applet implements ActionListener {
TextArea tb = new TextArea("331v sistemas operativos\nQuiroz Zuñiga Victor", 10, 20);
Button b1 = new Button("Iniciar");
Button b2 = new Button("Pausar");
TextArea ta = new TextArea("O_0", 10, 20);
Conteo c1;
Conteo c2;
public void init() {
setLayout(new GridLayout(2, 3));
add(ta);
add(b1);
add(b2);
add(tb);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void empezar() {
c1 = new Conteo(1000, ta);
c2 = new Conteo(300, tb);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(b1)) {
empezar();
c1.start();
c2.start();
}
if (e.getSource().equals(b2)) {
c1.stop();
c2.stop();
}
}
class Conteo extends Thread {
long tiempo;
TextArea Area;
boolean contando = true;
public Conteo(long tiempo, TextArea Area) {
this.tiempo = tiempo;
this.Area = Area;
}
public void run() {
while (contando) {
Area.setBackground(Color.green);
try {
sleep(tiempo);
} catch (InterruptedException e) {
}
{
Area.setBackground(Color.yellow);
}
try {
sleep(tiempo);
} catch (InterruptedException e) {
}
Area.setBackground(Color.red);
try {
sleep(tiempo);
} catch (InterruptedException e) {
}
}
}
}
}