Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trafficLigths;
- /*
- * Name: Lu Liu
- * Date: 4/7/2016
- * Course Number: CSC-112
- * Course Name: Intermediate Topics in Java Programming
- * Email: lliu0001@student.stcc.edu
- *
- * Assignment: Exam 2
- * Programe Description:
- * Traffic Lights
- */
- import com.sun.javafx.geom.Rectangle;
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.CheckBox;
- import javafx.scene.control.RadioButton;
- import javafx.scene.control.ToggleGroup;
- import javafx.scene.image.ImageView;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.text.Font;
- import javafx.scene.text.FontPosture;
- import javafx.scene.text.FontWeight;
- import javafx.scene.text.Text;
- public class TrafficLights extends Application {
- @Override
- // Override the start method in the Application class
- public void start(Stage primaryStage) {
- // Create a scene and place it in the stage
- Scene scene = new Scene(getPane(), 300, 150);
- primaryStage.setTitle("Traffic Lights"); // Set the stage title
- primaryStage.setScene(scene); // Place the scene in the stage
- primaryStage.show(); // Display the stage
- }
- /**
- * The main method is only needed for the IDE with limited JavaFX support.
- * Not needed for running from the command line.
- */
- public static void main(String[] args) {
- launch(args);
- }
- public BorderPane getPane() {
- // create a HBox for the buttons
- HBox paneForRadioButtons = new HBox(20);
- paneForRadioButtons.setPadding(new Insets(5, 5, 5, 5));
- paneForRadioButtons.setStyle("-fx-border-color: green");
- paneForRadioButtons.setStyle("-fx-border-width: 2px; -fx-border-color: green");
- RadioButton rbRed = new RadioButton("Red");
- RadioButton rbYellow = new RadioButton("Yellow");
- RadioButton rbGreen = new RadioButton("Green");
- paneForRadioButtons.getChildren().addAll(rbRed, rbYellow, rbGreen);
- // create a VBox for the lights
- VBox paneForLights = new VBox();
- paneForLights.setPadding(new Insets(10, 5, 50, 130));
- Circle c1 = new Circle(10, 70, 10, Color.RED);
- c1.setStroke(Color.BLACK);
- Circle c2 = new Circle(10, 30, 10, Color.WHITE);
- c2.setStroke(Color.BLACK);
- Circle c3 = new Circle(10, 50, 10, Color.WHITE);
- c3.setStroke(Color.BLACK);
- paneForLights.getChildren().addAll(c1, c2, c3);
- // create a BorderPane for the VBox and HBox
- BorderPane pane = new BorderPane();
- pane.setBottom(paneForRadioButtons);
- pane.setCenter(paneForLights);
- ToggleGroup group = new ToggleGroup();
- rbRed.setToggleGroup(group);
- rbYellow.setToggleGroup(group);
- rbGreen.setToggleGroup(group);
- rbRed.setOnAction(e -> {
- if (rbRed.isSelected()) {
- c1.setFill(Color.RED);
- c2.setFill(Color.WHITE);
- c3.setFill(Color.WHITE);
- }
- });
- rbYellow.setOnAction(e -> {
- if (rbYellow.isSelected()) {
- c2.setFill(Color.YELLOW);
- c1.setFill(Color.WHITE);
- c3.setFill(Color.WHITE);
- }
- });
- rbGreen.setOnAction(e -> {
- if (rbGreen.isSelected()) {
- c3.setFill(Color.GREEN);
- c1.setFill(Color.WHITE);
- c2.setFill(Color.WHITE);
- }
- });
- return pane;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement