Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Trevon Alvarado
- import javafx.geometry.Pos;
- import javafx.application.Application;
- import javafx.scene.control.RadioButton;
- import javafx.scene.control.ToggleGroup;
- import javafx.scene.layout.*;
- import javafx.stage.Stage;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.scene.Scene;
- public class TrafficLights extends Application {
- @Override
- public void start(Stage primaryStage) {
- Lights lights = new Lights();
- LButtons lButtons = new LButtons(lights);
- BorderPane borderPane = new BorderPane();
- borderPane.setCenter(lights);
- borderPane.setBottom(lButtons);
- Scene scene = new Scene(borderPane, 500, 600);
- primaryStage.setTitle("Traffic Lights");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- public class Lights extends StackPane {
- ImageView imagViewRed;
- ImageView imagViewYellow;
- ImageView imagViewGreen;
- public Lights() {
- Pane pane = new Pane();
- Image image1 = new Image("Images/trafficred.png");
- Image image2 = new Image("Images/trafficyellow.png");
- Image image3 = new Image("Images/trafficgreen.png");
- imagViewRed = new ImageView(image1);
- imagViewYellow = new ImageView(image2);
- imagViewGreen = new ImageView(image3);
- getChildren().add(imagViewRed);
- }
- }
- public class LButtons extends HBox{
- public LButtons(Lights lights){
- this.setSpacing(5);
- this.setAlignment(Pos.CENTER);
- RadioButton red = new RadioButton("RED LIGHT");
- RadioButton yellow = new RadioButton("YELLOW LIGHT");
- RadioButton green = new RadioButton("GREEN LIGHT");
- ToggleGroup toggleGroup = new ToggleGroup();
- red.setToggleGroup(toggleGroup);
- yellow.setToggleGroup(toggleGroup);
- green.setToggleGroup(toggleGroup);
- red.setOnAction(e -> {
- lights.getChildren().clear();
- lights.getChildren().add(lights.imagViewRed);
- });
- yellow.setOnAction(e -> {
- lights.getChildren().clear();
- lights.getChildren().add(lights.imagViewYellow);
- });
- green.setOnAction(e -> {
- lights.getChildren().clear();
- lights.getChildren().add(lights.imagViewGreen);
- });
- this.getChildren().addAll(red, yellow, green);
- }
- }
- }
Add Comment
Please, Sign In to add comment