import java.awt.*; import javax.swing.*; import java.awt.event.*; public class StopLight extends JApplet implements MouseListener { // instance variables - replace the example below with your own private int flag; public void init() { JRootPane rootPane = this.getRootPane(); rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); flag = 0; addMouseListener(this); } /** * Paint method for applet. * * @param g the Graphics object for this applet */ public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; // simple text displayed on applet g2.setColor(Color.black); g2.fillRect(0, 0, 400, 800); g2.setColor(Color.red); g2.drawOval(100, 5, 200, 200); g2.setColor(Color.yellow); g2.drawOval(100, 215, 200, 200); g2.setColor(Color.green); g2.drawOval(100, 425, 200, 200); System.out.println(flag); if(flag%3 == 0) { g2.setColor(Color.red); g2.fillOval(100, 5, 200, 200); } else if(flag%3 == 1) { g2.setColor(Color.green); g2.fillOval(100, 425, 200, 200); flag++; try{ Thread.sleep(1000); }catch(Throwable t){} repaint(); } else { g2.setColor(Color.yellow); g2.fillOval(100, 215, 200, 200); flag++; try{ Thread.sleep(1000); }catch(Throwable t){} repaint(); } } public void mouseClicked(MouseEvent e) { flag = ++flag %3; repaint(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e){} }