Question
Somebody tried, but it is wrong way please this is with Java and Applet before you help me make sure is right way to do
Somebody tried, but it is wrong way please this is with Java and Applet before you help me make sure is right way to do it Thank and please help me with both how to run with html and java code too.
Task #1 Create an Applet TrafficApplet.java
2. This class currently has all the constants you will need to be able to create your traffic signal. It doesnt have anything else . You will need to change the class heading so that it extends JApplet.
Task #2 The Timer Object
1. An applet does not have a constructor or a main method. Instead, it has a method named init that performs the same operations as a constructor. The init method accepts no arguments and has a void return type. Write an init method.
2. Inside the init method, create a Timer object passing in the TIME_DELAY constant and a new TimerListener (We will be creating the listener class next).
3. Call the start method with the Timer object to generate action events.
Task #3 The TimerListener Class
1. Write a private inner class called TimerListener which implements ActionListener.
2. Inside this class, write an actionPerformed method.
This method will check the status variable to see whether it is currently red, orange, or green.
Since we want the lights to cycle as a traffic signal, we need to cycle in the order: green, orange, red, green, orange, red, and so on. Once the status is determined, the status should then be set to the next color in the cycle.
3. Redisplay the graphics components (to be created next) by calling the repaint method.
Task #4 Drawing Graphics
1. draw the traffic signal by overriding the paint method. For all graphics, use the named constants included in the class.
2. Call the method that is being overridden in the parent class.
3. Create a yellow rectangle (solid color) for the traffic signal. The constants X_TRAFFICLIGHT, Y_TRAFFICLIGHT, TRAFFICLIGHT_WIDTH, and TRAFFICLIGHT_HEIGHT have already been defined for your use.
4. Create round lights of red, orange, and green for the signals.
These should be outlines of these colors. The constants X_LIGHTS, Y_REDLIGHT, Y_GREENLIGHT, Y_ORANGELIGHT, and LIGHT_DIAMETER, have already been defined for your use.
Only one light will be filled in at a time, when the status indicates that one has been chosen.
You will need to check the st determine which light to fill in. Remember, the status is changed only in the actionPerformed method (already defined) where the repaint method is also called.
5. Put the shade hoods above the lights by drawing black arcs above each light. The constants HOOD_START_ANGLE and HOOD_ANGLE_SWEPT have already been defined for your use.
6. Try out your applet. If time permits, create a web page on which you can display your applet
/// This is the Code to help me and fix it with Java code and to run Applet with html
import java.awt.*; import java.awt.event.*; import javax.swing.*;
/** This applet displays a timed traffic signal. */
public class TrafficApplet { // Constants for the traffic signal public final int WIDTH = 300; public final int HEIGHT = 400; public final int X_TRAFFICLIGHT = WIDTH / 3; public final int Y_TRAFFICLIGHT = HEIGHT / 7; public final int TRAFFICLIGHT_WIDTH = WIDTH / 2; public final int TRAFFICLIGHT_HEIGHT = HEIGHT * 3 / 5; public final int LIGHT_DIAMETER = TRAFFICLIGHT_HEIGHT / 5; public final int HOOD_START_ANGLE = 20; public final int HOOD_ANGLE_SWEPT = 140; public final int X_LIGHTS = TRAFFICLIGHT_WIDTH / 3 + X_TRAFFICLIGHT; public final int Y_REDLIGHT = TRAFFICLIGHT_HEIGHT / 10 + Y_TRAFFICLIGHT; public final int Y_ORANGELIGHT = TRAFFICLIGHT_HEIGHT * 4 / 10 + Y_TRAFFICLIGHT; public final int Y_GREENLIGHT = TRAFFICLIGHT_HEIGHT * 7 / 10 + Y_TRAFFICLIGHT; public final int TIME_DELAY = 1000;
private String status = "green"; // Signal status private Timer timer; // Timer object }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started