import java.awt.*; import java.util.StringTokenizer; /********************************************************** * Grading: * 2 two points for determining that the state change operations * had to go into either start or stop * 2 points for getting the color to change on each visit * 2 points for getting the text to change on each visit * 2 points for getting the font face to change on each visit * 2 points for getting the speed to change on each visit **********************************************************/ public class BlinkHW extends java.applet.Applet implements Runnable { Thread blinkThread; String currentBlinkingText; // to store the three messages String [] possibleTexts = new String[3]; // the RGB components of the color int red, green, blue; // flags to keep track of the state boolean isPlain; boolean isFast; boolean isFirstStart; // the current text that we are looking at int currentTextIndex; Font font; int speed; public void init() { // initial font font = new Font("TimesRoman", Font.PLAIN, 24); isPlain = true; // initial speed speed = 1000; isFast = false; // initialize the possible messages possibleTexts[0] = "Hello world!!"; possibleTexts[1] = "SE450 is great."; possibleTexts[2] = "This is a simple blinking text applet for demonstration in SE 450"; // initialize the current text currentBlinkingText = possibleTexts[0]; currentTextIndex = 0; // initialize the starting colors red = (int)(Math.random() * 256); green = (int)(Math.random() * 256); blue = (int)(Math.random() * 256); // flag to tell you this is the first start isFirstStart = true; } public void paint(Graphics g) { int x = 0, y = font.getSize(), space; Dimension d = getSize(); g.setColor(Color.black); g.setFont(font); FontMetrics fm = g.getFontMetrics(); space = fm.stringWidth(" "); StringTokenizer t = new StringTokenizer(currentBlinkingText); while (t.hasMoreTokens()) { String word = t.nextToken(); int w = fm.stringWidth(word) + space; if (x + w > d.width) { x = 0; y += font.getSize(); } // this gives you the missing words if (Math.random() < 0.5) { g.setColor(new Color(red, green, blue)); } else { g.setColor(getBackground()); } g.drawString(word, x, y); x += w; } } public void start() { // do this on subsequent starts other than the first one if (! isFirstStart){ // change the color of the text red = (int)(Math.random() * 256); green = (int)(Math.random() * 256); blue = (int)(Math.random() * 256); //System.out.println("Red =" + red); //System.out.println("Green =" + green); //System.out.println("Blue =" + blue); // change the speed if (isFast){ // slow the speed down speed = (int)(speed - 1000 * Math.random()); } else{ // speed it up speed = (int)(speed + 1000 * Math.random()); } isFast = !isFast; //System.out.println("Speed = " + speed); // change the font face if (isPlain){ // make it italic font = new Font("TimesRoman", Font.ITALIC, 24); } else{ // make it plain font = new Font("TimesRoman", Font.PLAIN, 24); } isPlain = !isPlain; // change the message if (currentTextIndex == (possibleTexts.length - 1)){ // you need to go back to 0 because there is no index 3. currentTextIndex = 0; } else{ // increment the index currentTextIndex++; } currentBlinkingText = possibleTexts[currentTextIndex]; } else{ // do this on the first start isFirstStart = false; } // start up the thread blinkThread = new Thread(this); blinkThread.start(); } public void stop() { // kill the thread blinkThread = null; } public void run() { while (Thread.currentThread() == blinkThread) { repaint(); try { Thread.currentThread().sleep(speed); } catch (InterruptedException e){} } } }