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();
        }           
    }
}
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

is there any way to do it without a timer?
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.
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
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.