import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TrafficLightPanel extends JPanel
{
private int state;
private JButton status;
private JLabel label;
//--------------------------------------------
//Constructor:Creates three circle objects
//------------------------------------------
public TrafficLightPanel()
{
//----------------------------------------------
//Constructor: Sets up the GUI
//----------------------------------------------
status = new JButton ("State");
status.addActionListener (new ButtonListener());
label = new JLabel ();
add(status);
add (label);
setPreferredSize (new Dimension (300,200));
setBackground (Color.black);
}
//-------------------------------------------------------
//Represents a listener for button status (action) events
//--------------------------------------------------------
private class ButtonListener implements ActionListener
{
//---------------------------------------------------------------
//Updates the state of the traffic light when the button is pushed
//----------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
state++;
red=1;
yellow=2;
green=3;
status = new JButton("State");
repaint();
}
}
//-------------------------------------------------------------
//Draws this panel by requesting that each circle draw itself
//-------------------------------------------------------------
public void paintComponent (Graphics page)
{
super.paintComponent(page);
page.setColor(Color.red);
page.fillOval(112, 60, 60,60);
page.setColor(Color.yellow);
page.fillOval(112, 60, 60,60);
page.setColor(Color.green);
page.fillOval(112, 60, 60,60);
page.setColor(Color.white);
page.drawString("Ready...Set...GO!!!",100,180);
}
}