For a homework assignment I need to make a stoplight cycle from red to green to yellow back to red. I tried using a thread.sleep method when I press a button to cycle through but it only goes to the last sleep & setColor statement. I know this is probably really simple and basic but this has kept going for hours, so any tips of help would be much appreciated! I've just posted the class I'm working on since this project uses an external acm.jar and a few other classes. The sleep statements are in the Advance() class. It's supposed to cycle when you click an advance button.

public class Stoplight extends GCompound {

/* Public constants for the colors */
	public static final Color GREEN = Color.GREEN;
	public static final Color YELLOW = Color.YELLOW;
	public static final Color RED = Color.RED;

/** Creates a new Stoplight object, which is initially red */
	public Stoplight() {
		GRect frame = new GRect(STOPLIGHT_WIDTH, STOPLIGHT_HEIGHT);
		frame.setFilled(true);
		frame.setColor(Color.DARK_GRAY);
		add(frame, -STOPLIGHT_WIDTH / 2, -STOPLIGHT_HEIGHT / 2);
		redLamp = createLamp(0, -STOPLIGHT_HEIGHT / 4);
		yellowLamp = createLamp(0, 0);
		greenLamp = createLamp(0, STOPLIGHT_HEIGHT / 4);
		add(redLamp);
		add(yellowLamp);
		add(greenLamp);
		setState(RED);
	}

/** Changes the state of the stoplight to the indicated color */
	public void setState(Color color) {
		state = color;
		redLamp.setColor((state == RED) ? state : Color.GRAY);
		yellowLamp.setColor((state == YELLOW) ? state : Color.GRAY);
		greenLamp.setColor((state == GREEN) ? state : Color.GRAY);
	}

/** Returns the current state of the stoplight */
	public Color getState() {
		return state;
	}

/** Advances the stoplight to the next color 
 * @throws InterruptedException */

//  This is the class where I'm having trouble ...
	public void advance()  {
		try {
			Thread.currentThread();
			Thread.sleep(1000);
			setState(GREEN);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			Thread.currentThread();
			Thread.sleep(3000);
			setState(YELLOW);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		try {
			Thread.currentThread();
			Thread.sleep(1000);
			setState(RED);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

/* Creates a new GOval to represent one of the three lamps */
	private GOval createLamp(double x, double y) {
		GOval lamp = new GOval(x - LAMP_RADIUS, y - LAMP_RADIUS,
		                       2 * LAMP_RADIUS, 2 * LAMP_RADIUS);
		lamp.setFilled(true);
		return lamp;
	}

/* Private constants */
	private static final double STOPLIGHT_WIDTH = 50;
	private static final double STOPLIGHT_HEIGHT = 100;
	private static final double LAMP_RADIUS = 10;

/* Private instance variables */
	private Color state;
	private GOval redLamp;
	private GOval yellowLamp;
	private GOval greenLamp;
}

What thread is this running on? If it's the swing EDT then you won't see any screen updates while your method is running/sleeping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.