// SW13hint Verkeerslicht /** * @(#)schoolproject13_SW13.java * * * @author * @version 1.00 2011/3/28 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class schoolproject13_SW13 extends JFrame { public static void main( String args[] ) { JFrame frame = new schoolproject13_SW13(); frame.setSize( 450, 300 ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setTitle( "Verkeerslicht" ); frame.setContentPane( new Paneel() ); frame.setVisible( true ); } } class Paneel extends JPanel { JButton rKnop; Color lred,lorange,lgreen; String ro="r"; int pauze=2000; Handler naarRood = new Handler(); Timer timer= new Timer(pauze, naarRood); int pauze2=8000; Handler naarOranje =new Handler(); Timer timerTwo=new Timer(pauze2, naarOranje); public Paneel() { rKnop = new JButton( "ROOD" ); rKnop.addActionListener( new RKnopHandler() ); add(rKnop); lorange=new Color(200,200,100); lred= new Color(230,180,150); lgreen=new Color(143,188,143); setBackground( Color.YELLOW ); // gele achtergrond } public void paintComponent( Graphics g ) { super.paintComponent( g ); // Teken met zwart g.setColor( Color.BLACK ); if (ro=="r") { g.setColor( Color.RED ); g.fillOval( 140,40 , 30, 30 ); g.setColor( lorange ); g.fillOval( 140,80 , 30, 30 ); g.setColor(lgreen); g.fillOval(140,120,30,30); } if (ro=="o") { g.setColor( lred ); g.fillOval( 140,40 , 30, 30 ); g.setColor( Color.ORANGE ); g.fillOval( 140,80 , 30, 30 ); g.setColor(lgreen); g.fillOval(140,120,30,30); } if (ro=="g"){ g.setColor( lred ); g.fillOval( 140,40 , 30, 30 ); g.setColor( lorange ); g.fillOval( 140,80 , 30, 30 ); g.setColor(Color.GREEN); g.fillOval(140,120,30,30); } } // Inwendige klasse class RKnopHandler implements ActionListener { public void actionPerformed( ActionEvent e ) { ro="g"; timer.start(); repaint(); } } class Handlers implements ActionListener { public void actionPerformed( ActionEvent e ) { timerTwo.stop(); ro="o"; repaint(); } } class Handler implements ActionListener { public void actionPerformed( ActionEvent e ) { timer.stop(); ro="r"; repaint(); } } }