FileDocCategorySizeDatePackage
blinkinLED1.javaAPI DocExample2208Wed Mar 28 15:17:54 BST 2001duncan

blinkinLED1

public class blinkinLED1 extends Applet implements ActionListener, Runnable

Fields Summary
Button
startBut
Button
stopBut
Thread
LEDThread
boolean
LEDon
boolean
LEDlight
Constructors Summary
Methods Summary
public voidactionPerformed(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 voidinit()


	  
	
    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 voidpaint(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 voidrun()

		//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 voidupdate(java.awt.Graphics g)

     paint(g);