I need used one button to change the state of the light, and the light should have green, amber, and red lights and cycle through green -> amber -> red -> green each time the button is pressed.

My code

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

     public class TrafficLightPanel extends JPanel {
     /** Unique version of this panel. */
    private static final long serialVersionUID = -1694196162632289132L; 
    /** Holds the number of time the button is pressed. */
    private int count;
    /** Displays the number of times the button is pressed. */
    private JLabel label;
    private Component paintComponent;
    /**
    * Constructor: Sets up the GUI.
    */
	    public TrafficLightPanel() {
	        JButton push = new JButton("Light!");
	        push.addActionListener(new ButtonListener());

	        label = new JLabel("" + count);
	        
			
	        add(push);
	        add(label);

	        final int panelWidth = 500;
	        final int panelHeight = 300;
	        setPreferredSize(new Dimension(panelWidth, panelHeight));
	        setBackground(Color.cyan);
	    }
//////////////////////////////////////////////////////////////////////////////////
	    private boolean red;
	    private boolean yellow;
	    private boolean green;

	    public void myPanel() {
	        red = false;
	        yellow = false;
	        green = false;
	    }

	    public void turnred() {
	        red = true;
	        yellow = false;
	        green = false;
	        repaint();
	    }

	    public void turnyellow() {
	        yellow = true;
	        red = false;
	        green = false;
	        repaint();
	    }

	    public void turngreen() {
	        green = true;
	        red = false;
	        yellow = false;
	        repaint();
	    }

	    @Override
	    public void paintComponent(Graphics g) {
	        super.paintComponent(g);
	        if (red) {
	            g.drawRect(120, 10, 50, 140);
	            g.drawOval(125, 60, 40, 40);
	            g.drawOval(125, 105, 40, 40);
	            g.setColor(Color.red);
	            g.fillOval(125, 15, 40, 40);
	        } else if (yellow) {
	            g.drawRect(120, 10, 50, 140);
	            g.drawOval(125, 15, 40, 40);
	            g.drawOval(125, 105, 40, 40);
	            g.setColor(Color.yellow);
	            g.fillOval(125, 60, 40, 40);
	        } else if (green) {
	            g.drawRect(120, 10, 50, 140);
	            g.drawOval(125, 15, 40, 40);
	            g.drawOval(125, 60, 40, 40);
	            g.setColor(Color.green);
	            g.fillOval(125, 105, 40, 40);
	        } else {
	            g.drawRect(120, 10, 50, 140);
	            g.drawOval(125, 15, 40, 40);
	            g.drawOval(125, 60, 40, 40);
	            g.drawOval(125, 105, 40, 40);
	        }

	    }
////////////////////////////////////////////////////////////////////////////////////////////////
	    /**
	     * Represents a listener for button push (action) events.
	     */
	    private class ButtonListener implements ActionListener {
	        /**
	         * Updates the counter and label when the button is pushed.
	        * @param event The event produced by the button when it is pressed
	        */
	        public void actionPerformed(ActionEvent event) {
	            count++;
	            label.setText("Pushes: " + count);
	        }
	    }
}
import javax.swing.JFrame;

public class TrafficLight {
    /**
     * Creates the main program frame.
     * @param args unused
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame("Push Counter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(new TrafficLightPanel());

        frame.pack();
        frame.setVisible(true);
    }
}

Recommended Answers

All 2 Replies

and what exactly is your question?

You're almost almost there. You have 3 methods to change the colors, you have a method that's called when the button is pressed, which counts the number of presses, so all you need to do is call the appropriate color change method when the button is pressed (1st press, green, second press amber, 3rd red, 4th green... etc)

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.