Question: I have a problem with my traffic light applet. Now when I run the applet it shows the traffic light but when I press GO

I have a problem with my traffic light applet. Now when I run the applet it shows the traffic light but when I press GO it does not do anything. also how do I make a detailed traffic light useing shapes and make it a standard traffic light with the stem.

import java.applet.*; import java.awt.*; import java.awt.event.*;

public class Traffic extends Applet { Button stopButton = new Button ("Stop"); Button slowButton = new Button ("Slow"); Button goButton = new Button ("Go");

Color currentColor = Color.red;

Panel controls = new Panel (); DrawingArea drawingArea = new DrawingArea ();

public void init () { setLayout (new BorderLayout ()); add (BorderLayout.SOUTH, controls); controls.add (stopButton); controls.add (slowButton); controls.add (goButton);

add (BorderLayout.CENTER, drawingArea);

stopButton.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent event) { changeColor (Color.red); } });

slowButton.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent event) { changeColor (Color.yellow); } });

goButton.addActionListener (new ActionListener () { public void actionPerformed (ActionEvent event) { changeColor (Color.green); } });

repaint (); }

public void paint (Graphics g) { drawingArea.paint (drawingArea.getGraphics ()); }

void changeColor (Color newColor) { currentColor = newColor; repaint (); }

class DrawingArea extends Canvas { public void paint (Graphics g) { g.setColor (Color.darkGray); g.fillRect (70, 20, 50, 150); g.setColor (currentColor == Color.red ? Color.red : Color.gray); g.fillOval (80, 30, 30, 30); g.setColor (currentColor == Color.yellow ? Color.yellow : Color.gray); g.fillOval (80, 80, 30, 30); g.setColor (currentColor == Color.green ? Color.green : Color.gray); g.fillOval (80, 130, 30, 30); }

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!