Project 6: Comments

Before you begin, create a folder in your Projects Folder called Firstname.LastName.project6. Do a File...Save as this html document into your Project5 folder.

1. Take the .java source code below(It is the same one you used in Project 4.) and paste it into a blank applet project in JCreator called Project6. Using comment /* comments here */ and/or // one line comments here markings, explain in as much detail as you can what each of the sections of code does. The more detail, the higher the project grade, but do not spend more than 30 minutes on it. Use my sample applet on page 3 of Object-Oriented Programming to help you in your explanations. Once you are done commenting, compile it in JCreator to make sure that it compiles and your commenting was ok.

2.

import java.awt.*;
import java.util.StringTokenizer;


public class Blink extends java.applet.Applet implements Runnable {
    Thread blinker;
    String lbl;
    Font font;
    int speed;

    public void init() {
	font = new java.awt.Font("TimesRoman", Font.PLAIN, 24);
	String att = getParameter("speed");
	speed = (att == null) ? 400 : (1000 / Integer.valueOf(att).intValue());
	att = getParameter("lbl");
	lbl = (att == null) ? "Blink" : att;
    }
    
    public void paint(Graphics g) {
	int x = 0, y = font.getSize(), space;
	int red = (int)(Math.random() * 50);
	int green = (int)(Math.random() * 50);
	int blue = (int)(Math.random() * 256);
	Dimension d = size();

	g.setColor(Color.black);
	g.setFont(font);
	FontMetrics fm = g.getFontMetrics();
	space = fm.stringWidth(" ");
	for (StringTokenizer t = new StringTokenizer(lbl) ; t.hasMoreTokens() ; ) {
	    String word = t.nextToken();
	    int w = fm.stringWidth(word) + space;
	    if (x + w > d.width) {
		x = 0;
		y += font.getSize();
	    }
	    if (Math.random() < 0.5) {
		g.setColor(new java.awt.Color((red + y * 30) % 256, (green + x / 3) % 256, blue));
	    } else {
		g.setColor(Color.lightGray);
	    }
	    g.drawString(word, x, y);
	    x += w;
	}
    }

    public void start() {
	blinker = new Thread(this);
	blinker.start();
    }
    public void stop() {
	blinker.stop();
    }
    public void run() {
	while (true) {
	try {Thread.currentThread().sleep(speed);} catch (InterruptedException e){}
	    repaint();
	}
    }
}

Save a copy of your entire folder into the Project 6 Folder on the server. You should have a project6.java file, a project6.class file and a project6.htm file. Make sure the folder has your Firstname.Lastname.project6 as a title. Open up your project on the server in a browser to make sure it appears the way your want it.