Advertisement
luliu

EXAM 2 Traffic Lights V2

Apr 7th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. /*
  2. * Name: Lu Liu
  3. * Date: 4/7/2016
  4. * Course Number: CSC-112
  5. * Course Name: Intermediate Topics in Java Programming
  6. * Email: lliu0001@student.stcc.edu
  7. *
  8. * Assignment: Exam 2 V2
  9. * Programe Description:
  10. * Traffic Lights
  11. */
  12.  
  13. package trafficLigths;
  14.  
  15. import com.sun.javafx.geom.Rectangle;
  16. import javafx.application.Application;
  17. import javafx.stage.Stage;
  18. import javafx.event.ActionEvent;
  19. import javafx.event.EventHandler;
  20. import javafx.geometry.Insets;
  21. import javafx.geometry.Pos;
  22. import javafx.scene.Scene;
  23. import javafx.scene.control.Button;
  24. import javafx.scene.control.CheckBox;
  25. import javafx.scene.control.RadioButton;
  26. import javafx.scene.control.ToggleGroup;
  27. import javafx.scene.image.ImageView;
  28. import javafx.scene.layout.BorderPane;
  29. import javafx.scene.layout.HBox;
  30. import javafx.scene.layout.Pane;
  31. import javafx.scene.layout.VBox;
  32. import javafx.scene.paint.Color;
  33. import javafx.scene.shape.Circle;
  34. import javafx.scene.text.Font;
  35. import javafx.scene.text.FontPosture;
  36. import javafx.scene.text.FontWeight;
  37. import javafx.scene.text.Text;
  38.  
  39. public class TrafficLights extends Application {
  40.  
  41. @Override
  42. // Override the start method in the Application class
  43. public void start(Stage primaryStage) {
  44. // Create a scene and place it in the stage
  45. Scene scene = new Scene(getPane(), 300, 150);
  46. primaryStage.setTitle("Traffic Lights"); // Set the stage title
  47. primaryStage.setScene(scene); // Place the scene in the stage
  48. primaryStage.show(); // Display the stage
  49. }
  50.  
  51. /**
  52. * The main method is only needed for the IDE with limited JavaFX support.
  53. * Not needed for running from the command line.
  54. */
  55. public static void main(String[] args) {
  56. launch(args);
  57. }
  58.  
  59. public BorderPane getPane() {
  60.  
  61. // create a HBox for the buttons
  62. HBox paneForRadioButtons = new HBox(20);
  63. paneForRadioButtons.setPadding(new Insets(5, 5, 5, 5));
  64. paneForRadioButtons.setStyle("-fx-border-color: green");
  65. paneForRadioButtons.setStyle("-fx-border-width: 2px; -fx-border-color: green");
  66. RadioButton rbRed = new RadioButton("Red");
  67. RadioButton rbYellow = new RadioButton("Yellow");
  68. RadioButton rbGreen = new RadioButton("Green");
  69. paneForRadioButtons.getChildren().addAll(rbRed, rbYellow, rbGreen);
  70.  
  71. // create a VBox for the lights
  72. VBox paneForLights = new VBox();
  73. paneForLights.setPadding(new Insets(10, 5, 20, 130));
  74. Circle c1 = new Circle(70, 10, 10, Color.RED);
  75. c1.setStroke(Color.BLACK);
  76. Circle c2 = new Circle(10, 30, 10, Color.WHITE);
  77. c2.setStroke(Color.BLACK);
  78. Circle c3 = new Circle(10, 50, 10, Color.WHITE);
  79. c3.setStroke(Color.BLACK);
  80. VBox.setMargin(c1, new Insets(0, 0, 5, 0));
  81. VBox.setMargin(c2, new Insets(0, 0, 5, 0));
  82. VBox.setMargin(c3, new Insets(0, 0, 5, 0));
  83. paneForLights.getChildren().addAll(c1, c2, c3);
  84.  
  85. // create a BorderPane for the VBox and HBox
  86. BorderPane pane = new BorderPane();
  87. pane.setBottom(paneForRadioButtons);
  88. pane.setCenter(paneForLights);
  89.  
  90. ToggleGroup group = new ToggleGroup();
  91. rbRed.setToggleGroup(group);
  92. rbYellow.setToggleGroup(group);
  93. rbGreen.setToggleGroup(group);
  94.  
  95. rbRed.setOnAction(e -> {
  96. if (rbRed.isSelected()) {
  97. c1.setFill(Color.RED);
  98. c2.setFill(Color.WHITE);
  99. c3.setFill(Color.WHITE);
  100. }
  101. });
  102.  
  103. rbYellow.setOnAction(e -> {
  104. if (rbYellow.isSelected()) {
  105. c2.setFill(Color.YELLOW);
  106. c1.setFill(Color.WHITE);
  107. c3.setFill(Color.WHITE);
  108. }
  109. });
  110.  
  111. rbGreen.setOnAction(e -> {
  112. if (rbGreen.isSelected()) {
  113. c3.setFill(Color.GREEN);
  114. c1.setFill(Color.WHITE);
  115. c2.setFill(Color.WHITE);
  116. }
  117. });
  118.  
  119. return pane;
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement