Guest User

Untitled

a guest
Apr 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. //Trevon Alvarado
  2. import javafx.geometry.Pos;
  3. import javafx.application.Application;
  4. import javafx.scene.control.RadioButton;
  5. import javafx.scene.control.ToggleGroup;
  6. import javafx.scene.layout.*;
  7. import javafx.stage.Stage;
  8. import javafx.scene.image.Image;
  9. import javafx.scene.image.ImageView;
  10. import javafx.scene.Scene;
  11.  
  12. public class TrafficLights extends Application {
  13. @Override
  14. public void start(Stage primaryStage) {
  15.  
  16. Lights lights = new Lights();
  17. LButtons lButtons = new LButtons(lights);
  18.  
  19. BorderPane borderPane = new BorderPane();
  20. borderPane.setCenter(lights);
  21. borderPane.setBottom(lButtons);
  22.  
  23. Scene scene = new Scene(borderPane, 500, 600);
  24. primaryStage.setTitle("Traffic Lights");
  25. primaryStage.setScene(scene);
  26. primaryStage.show();
  27. }
  28.  
  29. public static void main(String[] args) {
  30. launch(args);
  31. }
  32.  
  33. public class Lights extends StackPane {
  34.  
  35. ImageView imagViewRed;
  36. ImageView imagViewYellow;
  37. ImageView imagViewGreen;
  38.  
  39. public Lights() {
  40. Pane pane = new Pane();
  41.  
  42. Image image1 = new Image("Images/trafficred.png");
  43. Image image2 = new Image("Images/trafficyellow.png");
  44. Image image3 = new Image("Images/trafficgreen.png");
  45.  
  46. imagViewRed = new ImageView(image1);
  47. imagViewYellow = new ImageView(image2);
  48. imagViewGreen = new ImageView(image3);
  49.  
  50. getChildren().add(imagViewRed);
  51.  
  52. }
  53. }
  54.  
  55. public class LButtons extends HBox{
  56.  
  57. public LButtons(Lights lights){
  58.  
  59. this.setSpacing(5);
  60. this.setAlignment(Pos.CENTER);
  61.  
  62. RadioButton red = new RadioButton("RED LIGHT");
  63. RadioButton yellow = new RadioButton("YELLOW LIGHT");
  64. RadioButton green = new RadioButton("GREEN LIGHT");
  65.  
  66. ToggleGroup toggleGroup = new ToggleGroup();
  67. red.setToggleGroup(toggleGroup);
  68. yellow.setToggleGroup(toggleGroup);
  69. green.setToggleGroup(toggleGroup);
  70.  
  71. red.setOnAction(e -> {
  72. lights.getChildren().clear();
  73. lights.getChildren().add(lights.imagViewRed);
  74.  
  75. });
  76.  
  77. yellow.setOnAction(e -> {
  78. lights.getChildren().clear();
  79. lights.getChildren().add(lights.imagViewYellow);
  80. });
  81.  
  82. green.setOnAction(e -> {
  83. lights.getChildren().clear();
  84. lights.getChildren().add(lights.imagViewGreen);
  85. });
  86.  
  87. this.getChildren().addAll(red, yellow, green);
  88.  
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment