luliu

EXAM 2 Traffic Lights V2

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