Advertisement
luliu

EXAM 2 Traffic Lights

Apr 7th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package trafficLigths;
  2.  
  3. /*
  4. * Name: Lu Liu
  5. * Date: 4/7/2016
  6. * Course Number: CSC-112
  7. * Course Name: Intermediate Topics in Java Programming
  8. * Email: lliu0001@student.stcc.edu
  9. *
  10. * Assignment: Exam 2
  11. * Programe Description:
  12. * Traffic Lights
  13. */
  14. import com.sun.javafx.geom.Rectangle;
  15.  
  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, 50, 130));
  74. Circle c1 = new Circle(10, 70, 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. paneForLights.getChildren().addAll(c1, c2, c3);
  81.  
  82. // create a BorderPane for the VBox and HBox
  83. BorderPane pane = new BorderPane();
  84. pane.setBottom(paneForRadioButtons);
  85. pane.setCenter(paneForLights);
  86.  
  87. ToggleGroup group = new ToggleGroup();
  88. rbRed.setToggleGroup(group);
  89. rbYellow.setToggleGroup(group);
  90. rbGreen.setToggleGroup(group);
  91.  
  92. rbRed.setOnAction(e -> {
  93. if (rbRed.isSelected()) {
  94. c1.setFill(Color.RED);
  95. c2.setFill(Color.WHITE);
  96. c3.setFill(Color.WHITE);
  97. }
  98. });
  99.  
  100. rbYellow.setOnAction(e -> {
  101. if (rbYellow.isSelected()) {
  102. c2.setFill(Color.YELLOW);
  103. c1.setFill(Color.WHITE);
  104. c3.setFill(Color.WHITE);
  105. }
  106. });
  107.  
  108. rbGreen.setOnAction(e -> {
  109. if (rbGreen.isSelected()) {
  110. c3.setFill(Color.GREEN);
  111. c1.setFill(Color.WHITE);
  112. c2.setFill(Color.WHITE);
  113. }
  114. });
  115.  
  116. return pane;
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement