Write a class called TrafficLight that extends the Pane class to represent the d
ID: 3894846 • Letter: W
Question
Write a class called TrafficLight that extends the Pane class to represent the drawing of a traffic light. The TrafficLight class has three Circle object variables representing the three lights red, yellow, and green of a traffic light. Also, the class includes the definition of an enumerated type, called State, that contains the following identifiers: STOP, CAUTION, and GO. The TrafficLight class constructor creates the geometric structure of the traffic light using three circles within a rectangle. Assume the circle fill color for an OFF light is Grey. Assume the initial state of the traffic light is STOP. Accordingly, the fill colors for the red, yellow, and green circles are set to RED, GREY, and GREY, respectively. The TrafficLight class includes a method, called SwitchState, that accepts a State argument which is used to determine the fill color of the three circles representing the traffic lights. Write a JavaFX application, called TrafficLightDisplayer, that has four fields: A TrafficLight object variable and three Button object variables representing STOP, CAUTION, and GO states. The TrafficLightDisplayer allows the user to control the state of a displayed traffic light by pushing one of the corresponding buttons.
Explanation / Answer
Hi . Below is your code. Please let me know if you need screenshot or any other help in comments. I'll help for sure.
Please do rate the answer positive. Really need it.
// TrafficLight.java
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
public class TrafficLight extends Pane {
//necessary attributes
Circle RED_LIGHT;
Circle YELLOW_LIGHT;
Circle GREEN_LIGHT;
static double RADIUS = 100;//radius of circle
static double HEIGHT = 600;//height of the container
public TrafficLight() {
//initializing 3 circles
RED_LIGHT = new Circle(RADIUS);
YELLOW_LIGHT = new Circle(RADIUS);
GREEN_LIGHT = new Circle(RADIUS);
//creating the container
Rectangle rectangle = new Rectangle(RADIUS * 2, HEIGHT, Color.BLACK);
//using a stack pane to arrange the container and a vbox containing
//circles
StackPane pane = new StackPane();
pane.getChildren().add(rectangle);
VBox vBox = new VBox(RED_LIGHT, YELLOW_LIGHT, GREEN_LIGHT);
pane.getChildren().add(vBox);
//adding all to the main pane
this.getChildren().add(pane);
//calling default state (STOP)
switchState(State.STOP);
}
//method to switch state
public void switchState(State state) {
if (state == State.STOP) {
//displaying red circle only
RED_LIGHT.setFill(Color.RED);
YELLOW_LIGHT.setFill(Color.GRAY);
GREEN_LIGHT.setFill(Color.GRAY);
} else if (state == State.GO) {
//displaying green circle only
RED_LIGHT.setFill(Color.GRAY);
YELLOW_LIGHT.setFill(Color.GRAY);
GREEN_LIGHT.setFill(Color.GREEN);
} else {
//displaying yellow circle only
RED_LIGHT.setFill(Color.GRAY);
YELLOW_LIGHT.setFill(Color.YELLOW);
GREEN_LIGHT.setFill(Color.GRAY);
}
}
}
//enumeration to represent a state
enum State {
STOP,
CAUTION,
GO
}
// TrafficLightDisplayer.java
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TrafficLightDisplayer extends Application {
TrafficLight trafficLight;
Button stop, caution, go;
//method to work as an action listener for the buttons
public void update(State state) {
trafficLight.switchState(state);
}
@Override
public void start(Stage primaryStage) {
//initializing all fields
trafficLight = new TrafficLight();
stop = new Button("STOP");
caution = new Button("CAUTION");
go = new Button("GO");
//adding buttons to an hbox
HBox hBox = new HBox(stop, caution, go);
hBox.setAlignment(Pos.CENTER);
//creating a Vbox to arrange traffic lights and buttons
VBox vBox = new VBox(trafficLight, hBox);
//creating and displaying the scene
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.show();
//adding action listener to the buttons, using a simple lambda expression
stop.setOnAction(e -> update(State.STOP));
caution.setOnAction(e -> update(State.CAUTION));
go.setOnAction(e -> update(State.GO));
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.