FileDocCategorySizeDatePackage
LED.javaAPI DocExample1413Tue Mar 27 11:19:16 BST 2001None

LED

public class LED extends Applet implements Runnable

Fields Summary
Thread
LEDThread
boolean
LEDon
boolean
LEDlight
Constructors Summary
Methods Summary
public voidinit()

	  
	
		LEDThread = new Thread(this,"LED"); //step 4
		LEDThread.start();	//step 5

	
public voidpaint(java.awt.Graphics g)

//here the paint method is reading a variable that is changed by our thread
		//run changes LEDlight every 1/2 second, notice that run explicitly calls repaint()
		//which calls paint()
		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
		{
			while (LEDon == true)
			{
				LEDlight = !LEDlight; //flip the LED status
				repaint();	//call paint so it draws the LED in its new state
				try{//we'll talk about try..catch next week (they are exceptions)
					LEDThread.sleep(500); //wait for 500 milliseconds (1/2 second)
				}catch(InterruptedException e) 
				{
					showStatus("Thread timed out");
				}
				showStatus("LED = "+LEDlight);
			}//end while
		}//endfor