import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Lights extends JPanel
{
public JRadioButton btnR;
public JRadioButton btnY;
public JRadioButton btnG;
public JRadioButton btnO;
public Color rColor;
public Color yColor;
public Color gColor;
public Color general;
public void paintComponent ( Graphics g)
{
super.paintComponent ( g );
general= getBackground();
// Set foreground color
g.setColor (Color.black);
// get the size of the drawing area
Dimension size = this.getSize();
// Bounding rectangle for the traffic lights
int rectWidth = (int) (0.4 * size.width);
int rectHeight = (int) (0.8 * size.height);
// Location where the rectangle is to be drawn
int xint = (int) (0.3 * size.width);
int yint = (int) (0.1 * size.height);
// Draw bounding rectangle
g.drawRect (xint, yint, rectWidth, rectHeight);
// Draw three circles inside the rectangle
int circleHeight = (int) (0.9 * rectHeight / 3);
int circleWidth = (int) (0.8 * rectWidth);
int circleDia = (circleHeight < circleWidth)? circleHeight : circleWidth;
int xOffset = (rectWidth - circleDia) / 2;
int yOffset = (rectHeight - 3 * circleDia) / 4;
xint = xint + xOffset;
yint = yint + yOffset;
// Draw the red light
g.setColor(getBackground());
g.setColor(rColor);
g.fillOval (xint, yint, circleDia, circleDia);
g.setColor(Color.black);
g.drawOval (xint, yint, circleDia, circleDia);
g.setColor(getBackground());
rColor= general;
// Draw the yellow light
yint = yint + circleDia + yOffset;
g.setColor(yColor);
g.fillOval (xint, yint, circleDia, circleDia);
g.setColor(Color.black);
g.drawOval (xint, yint, circleDia, circleDia);
g.setColor(getBackground());
yColor= general;
// Draw the green light
yint = yint + circleDia + yOffset;
g.setColor(gColor);
g.fillOval (xint, yint, circleDia, circleDia);
g.setColor(Color.black);
g.drawOval (xint, yint, circleDia, circleDia);
g.setColor(getBackground());
gColor= general;
}
}
public class TrafficLights
{
public static void main (String[] args)
{
Lights lightPanel = new Lights();
lightPanel.setBorder ( BorderFactory.createEmptyBorder (10, 20, 10, 20 ));
// Create buttons to simulate switches
lightPanel.btnR = new JRadioButton ("Red");
lightPanel.btnY = new JRadioButton ("Yellow");
lightPanel.btnG = new JRadioButton ("Green");
lightPanel.btnO = new JRadioButton ("Off");
ButtonGroup switches = new ButtonGroup ();
switches.add (lightPanel.btnR);
switches.add (lightPanel.btnY);
switches.add (lightPanel.btnG);
switches.add (lightPanel.btnO);
JPanel switchPanel = new JPanel();
switchPanel.setBorder ( BorderFactory.createEmptyBorder (10, 20, 10, 20 ));
switchPanel.setLayout (new GridLayout (1, 4));
switchPanel.add (lightPanel.btnR);
switchPanel.add (lightPanel.btnY);
switchPanel.add (lightPanel.btnG);
switchPanel.add (lightPanel.btnO);
JFrame frame = new JFrame ("Traffic Lights");
frame.getContentPane().add( lightPanel, BorderLayout.CENTER);
frame.getContentPane().add( switchPanel, BorderLayout.SOUTH);
frame.setSize (400, 600);
frame.setVisible (true);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
class LightButtonListener implements ActionListener
{
private Lights theLight;
//public Color c;
public LightButtonListener ( Lights aLight )
{
theLight = aLight;
//create listener and add buttons to it
theLight.btnR.addActionListener ( this );
theLight.btnY.addActionListener ( this );
theLight.btnG.addActionListener ( this );
theLight.btnO.addActionListener ( this );
}
public void actionPerformed ( ActionEvent evt )
{
if ( evt.getSource() == theLight.btnR)
{
theLight.rColor= Color.red;
theLight.repaint();
}
else if (evt.getSource() == theLight.btnY)
{
theLight.yColor= Color.yellow;
theLight.repaint();
}
else if (evt.getSource() == theLight.btnG)
{
theLight.rColor= Color.green;
theLight.repaint();
}
else if (evt.getSource() == theLight.btnO)
{
theLight.repaint();
}
}
}