//TrafficLight.java //needs WindowDestroyer.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TrafficLight extends JFrame implements ItemListener { private JRadioButton bRed, bYellow, bGreen; private ButtonGroup bGroup = new ButtonGroup(); private TLPanel tlpan = new TLPanel(); public static void main(String[] args) { TrafficLight lt = new TrafficLight(); lt.setSize(200, 400); lt.show(); }//end main public TrafficLight() { setTitle("Traffic Light"); addWindowListener(new WindowDestroyer()); JPanel lPanel = new JPanel(); lPanel.setSize(100, 300); lPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); lPanel.add(tlpan); JPanel toggleBP = new JPanel(); toggleBP.setLayout(new FlowLayout()); toggleBP.add(bRed = new JRadioButton("Red", false)); toggleBP.add(bYellow = new JRadioButton("Yellow", false) ); toggleBP.add(bGreen = new JRadioButton("Green", false)); bGroup.add(bRed); bGroup.add(bYellow); bGroup.add(bGreen); getContentPane().setLayout(new BorderLayout()); getContentPane().add(lPanel, BorderLayout.CENTER); getContentPane().add(toggleBP, BorderLayout.NORTH); bRed.addItemListener(this); bYellow.addItemListener(this); bGreen.addItemListener(this); }//end TrafficLight //Built-in for checking button states public void itemStateChanged(ItemEvent e) { if(bRed.isSelected()) tlpan.redOn(); if(bYellow.isSelected()) tlpan.yellowOn(); if(bGreen.isSelected()) tlpan.greenOn(); }//end intemStateChanged }//end class TrafficLight class TLPanel extends JPanel { private boolean rB; private boolean yB; private boolean gB; //Create custom colors for off settings Color redOff = new Color(90,0,0); Color yellowOff = new Color(200,90,0); Color greenOff = new Color(0,100,0); public TLPanel() { greenOn();//initialize light } //When one is on, stop the other two public void redOn() { rB = true; yB= false; gB = false; repaint(); }//redOn public void yellowOn() { yB = true; rB = false; gB = false; repaint(); }//yellowOn public void greenOn() { gB = true; rB = false; yB = false; repaint(); }//greenOn /********************** * Paints the lights in the * different states. Rather than * show the circles as black or blank * I've set them to darker colors when off * and bight color when on * ***********************/ public void paintComponent(Graphics g) { super.paintComponent(g); if(rB) { g.setColor(Color.black); g.drawRect(9, 5, 100, 320); g.setColor(Color.red); g.drawOval(10, 7, 95, 95); g.fillOval(10, 7, 95, 95); g.setColor(yellowOff); g.drawOval(10, 117, 95, 95); g.fillOval(10, 117, 95, 95); g.setColor(greenOff); g.drawOval(10, 227, 95, 95); g.fillOval(10, 227, 95, 95); } else if(yB) { g.setColor(Color.black); g.drawRect(9, 5, 100, 320); g.setColor(redOff); g.drawOval(10, 7, 95, 95); g.fillOval(10, 7, 95, 95); g.setColor(Color.yellow); g.drawOval(10, 117, 95, 95); g.fillOval(10, 117, 95, 95); g.setColor(greenOff); g.drawOval(10, 227, 95, 95); g.fillOval(10, 227, 95, 95); } else if(gB) { g.setColor(Color.black); g.drawRect(9, 5, 100, 320); g.setColor(redOff); g.drawOval(10, 7, 95, 95); g.fillOval(10, 7, 95, 95); g.setColor(yellowOff); g.drawOval(10, 117, 95, 95); g.fillOval(10, 117, 95, 95); g.setColor(Color.green); g.drawOval(10, 227, 95, 95); g.fillOval(10, 227, 95, 95); } }//end paintComponent public Dimension getPreferredSize() { return new Dimension(120, 330); }//end getPreferredSize() }//end TLPanel