Draw a square (100 pixels by 100 pixels) in the center of the screen and change its color every second.

Solution

/**
 * Draws a Mystery Square that changes color
 * -----------------------------------------
 * Really does what it says.
 */
public class MysterySquare extends GraphicsProgram {

	private static RandomGenerator rg = new RandomGenerator();
	
	public void run() {
		// Draw a square in the center of the screen.
		GRect rect = new GRect(100, 100);
		double x = (getWidth() - rect.getWidth()) / 2;
		double y = (getHeight() - rect.getHeight()) / 2;
		rect.setFilled(true);
		add(rect, x, y);
		
		// Change its color every second.
		while(true) {
			rect.setColor(rg.nextColor()); 
			pause(1000);
		}
	}
}