Methods Summary |
---|
public void | actionPerformed(java.awt.event.ActionEvent ae)
if(ae.getSource() == startBut)
{
LEDon = true;
LEDThread = new Thread(this,"LED"); //step 4
LEDThread.start();//step 5
}
if(ae.getSource() == stopBut)
{
LEDon = false;
LEDlight = false;
repaint();
}
|
public void | init()
startBut = new Button("Start");
stopBut = new Button("Stop");
startBut.addActionListener(this);
stopBut.addActionListener(this);
add(startBut);
add(stopBut);
//LEDThread = new Thread(this,"LED"); //step 4
//LEDThread.start(); //step 5
|
public void | paint(java.awt.Graphics g)//here the paint( ) method is reading variable LEDlight that is changed by our thread
//run toggles LEDlight every 1/2 sec; note that run explicitly calls repaint()
//which calls update()
if (LEDlight == true) //LEDon?
g.setColor(Color.red);
else //if not black
g.setColor(Color.black);
g.fillOval(100,100,50,50);
|
public void | run()
//for(;;)//loop forever REMOVED NOW
while (LEDon == true)
{
LEDlight = !LEDlight; //toggle the LED status
repaint(); //call paint so it draws the LED in its new state
try{//we'll talk about try..catch next week
LEDThread.sleep(500); //wait for 500 milliseconds (1/2 second)
}catch(InterruptedException e)
{
showStatus("Thread timed out");
}
showStatus("LED = "+LEDlight);
}//end while
//}//endfor REMOVED
|
public void | update(java.awt.Graphics g)
paint(g);
|