1

I am trying to make a stoplight that performs certain tasks after a button is clicked. What this stoplight is supposed to do is change from green to yellow after 50 secs, from yellow to red after 10 secs, and from red to green after 60 secs (this part I have working fine), and if the button is pressed when it is green it should change to yellow, this should only work after 10 secs have at least passed while green. What I have a problem is how do I check if 10 secs have passed or not?

public class Stoplight extends Applet
{
    Button cross;

    public void init(){
       cross = new Button("Cross");
       add(cross);

       StoplightCanvas stoplightCanvas = new StoplightCanvas(cross);
       add(stoplightCanvas);

       new StoplightThread(stoplightCanvas).start();
    }
}

class StoplightCanvas extends Canvas implements ActionListener
{  
    int Xpos;
    int Ypos;
    int diameter;
    Button cross;
    int x = 1;

    StoplightCanvas(Button cross)
    {
        this.cross = cross;
        cross.addActionListener(this);
        setSize(300, 600);
    }

    public void paint(Graphics g)
    {
        diameter = 70;
        Xpos = 70;
        Ypos = 50; 

        g.setColor(Color.BLUE);
        g.fillRect(70, 50, 74, 220); 

        g.setColor(Color.WHITE);

        if (x == 1) 
           g.setColor(Color.RED);
        drawCircles(g, Xpos, Ypos);

        g.setColor(Color.WHITE);
        if (x == 2) 
            g.setColor(Color.YELLOW);
        drawCircles(g, Xpos, Ypos + diameter);

        g.setColor(Color.WHITE);
        if (x == 3)
        g.setColor(Color.GREEN);
        drawCircles(g, Xpos, Ypos + diameter * 2);    
    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == cross) {                   
        }  

        repaint();
    }

    void drawCircles(Graphics g, int x, int y)
    {   
        g.fillOval(x, y, diameter, diameter);     
    }

    public void toggleColor() {
        if (x == 1) 
            x = 3;
        else if (x == 2)
            x = 1;
        else if (x == 3) 
            x = 2;
    }
}

class StoplightThread extends Thread
{
    StoplightCanvas stoplightCanvas;

    StoplightThread(StoplightCanvas stoplightCanvas) {
        this.stoplightCanvas = stoplightCanvas;
    }

    public void run() 
    {
        while (true) {
           try {
               if (stoplightCanvas.x == 3){
                   Thread.sleep(50000);
               } else if (stoplightCanvas.x == 2) {
                   Thread.sleep(10000);    
               } else if (stoplightCanvas.x == 1) {
                   Thread.sleep(60000);
               }
           } catch (InterruptedException e){}

           stoplightCanvas.toggleColor();
           stoplightCanvas.repaint();
        }           
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
FJam
  • 736
  • 3
  • 13
  • 32
  • 1) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson May 06 '13 at 15:13

1 Answers1

1

You can set a timer when they press the button for 10 seconds. When that time expires, then change the color to yellow via the callback. It is much better than dealing with exceptions, because they should be for exceptional circumstances.

See this thread on how to set a timer for later.

Edit

The poster wishes to not use timers. One way would be to store the time when the button is pressed in a variable, then access that variable and compare against the current time within the while loop of the run method.

Community
  • 1
  • 1
Colonel Panic
  • 1,604
  • 2
  • 20
  • 31
  • is there any way to do it without a timer? – FJam May 06 '13 at 01:41
  • Is there a specific reason for not wanting to use timers? If you don't use timers, then you will end up sleeping on the thread, which makes the GUI unresponsive. Timers are the classical way of handling this problem. – Colonel Panic May 06 '13 at 01:42
  • this is for homework and we have not learned timers yet, that is the only reason. If I do not find any other way, ill just have to use a timer – FJam May 06 '13 at 01:44
  • I guess the current way you have it will suffice for a basic homework assignment. Your problem is that you need to set `stoplightCanvas.x` whenever you press the button. Your `actionPerformed` isn't doing much and I suspect that's where the problem is. I also updated the answer. – Colonel Panic May 06 '13 at 01:49
  • so when I put `x = 2` inside the `actionPerformed` all it does is when I press the button it changes color, then whatever the `.sleep` time is left gets added to the yellow sleep time. – FJam May 07 '13 at 01:57