//Title: Instructor //Version: 1.0 //Copyright: Copyright (c) Vairavan Vairavan //Author: Vairavan Vairavan //Company: UWM //Description: package colors; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Colors extends Applet implements Runnable,ActionListener{ boolean isStandalone = false; Thread counterThread; Label countValue; Button slow,fast; int count = 10; int sleeptime =1000; Color[] colorlist = { Color.white,Color.blue, Color.green, Color.yellow, Color.darkGray, Color.magenta,Color.pink,Color.red, Color.white, Color.cyan,Color.orange}; //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public Colors() { } //Initialize the applet public void init() { add(new Label("Count: ")); countValue = new Label(Integer.toString(count)); add(countValue); slow = new Button("Slow Down Counting"); add(slow); slow.addActionListener(this); fast = new Button("Speed up Counting"); add(fast); fast.addActionListener(this); } //Component initialization private void jbInit() throws Exception { } //Start the thread public void start() { if(counterThread == null) { counterThread = new Thread(this); counterThread.start(); } } //Stop the thread public void stop() { if(counterThread != null) { counterThread = null; } } public void run() { while(true) { try { counterThread.sleep(sleeptime); startCounting(); } catch(InterruptedException e) { } } } public void actionPerformed(ActionEvent e) { Button selectedbutton= (Button)e.getSource(); if (selectedbutton.equals(slow)) sleeptime= sleeptime + 1000; if (selectedbutton.equals(fast)) { sleeptime= sleeptime - 1000; if (sleeptime <= 0 ) sleeptime = 1000; } } public void startCounting() { count--; if(count == 0) count = 10; countValue.setText(Integer.toString(count)); setBackground(colorlist[count]); } }