Advertisement
rjsantiago0001

Traffic Light

Apr 7th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.60 KB | None | 0 0
  1. // Ricardo Santiago
  2. // 4/7/16
  3. // CSCI-112
  4. // Exam#2 - Traffic Light
  5. // This program will display a traffic light to the user, along with three radio buttons that allow the user to
  6. // select a light. If one light is selected, it will turn on and the other will dim
  7.  
  8.  
  9. import javafx.application.Application;
  10. import javafx.geometry.Insets;
  11. import javafx.geometry.Pos;
  12. import javafx.scene.Scene;
  13. import javafx.scene.control.RadioButton;
  14. import javafx.scene.control.ToggleGroup;
  15. import javafx.scene.layout.BorderPane;
  16. import javafx.scene.layout.HBox;
  17. import javafx.scene.paint.Color;
  18. import javafx.scene.shape.Circle;
  19. import javafx.scene.shape.Rectangle;
  20. import javafx.stage.Stage;
  21.  
  22. public class TrafficLight extends Application {
  23.  
  24.     Circle redLight, yellowLight, greenLight;
  25.     RadioButton rbRed, rbYellow, rbGreen;
  26.     double dim = .20, lit = 1.0;
  27.  
  28.     @Override
  29.     public void start(Stage primaryStage) throws Exception {
  30.        
  31.         BorderPane root = new BorderPane();
  32.        
  33.         Rectangle rectangle = new Rectangle(150, 75, 100, 250);
  34.         rectangle.setFill(Color.GRAY);
  35.         rectangle.setStroke(Color.BLACK);
  36.  
  37.         redLight = new Circle(200, 120, 30);
  38.         redLight.setFill(Color.RED);
  39.         redLight.setStroke(Color.BLACK);
  40.         redLight.setOpacity(lit);
  41.  
  42.         yellowLight = new Circle(200, 200, 30);
  43.         yellowLight.setFill(Color.YELLOW);
  44.         yellowLight.setStroke(Color.BLACK);
  45.         yellowLight.setOpacity(dim);
  46.  
  47.         greenLight = new Circle(200, 280, 30);
  48.         greenLight.setFill(Color.GREEN);
  49.         greenLight.setStroke(Color.BLACK);
  50.         greenLight.setOpacity(dim);
  51.  
  52.         root.getChildren().addAll(rectangle, redLight, yellowLight, greenLight);
  53.         root.setBottom(getBottomHBox());
  54.        
  55.         Scene scene = new Scene(root, 400, 400);
  56.  
  57.         primaryStage.setResizable(false);
  58.         primaryStage.setTitle("Traffic Light");
  59.         primaryStage.setScene(scene);
  60.         primaryStage.show();
  61.  
  62.         rbRed.setOnAction(e -> {
  63.             if (rbRed.isSelected()) {
  64.                 changeLight(0);
  65.             }
  66.         });
  67.  
  68.         rbYellow.setOnAction(e -> {
  69.             if (rbYellow.isSelected()) {
  70.                 changeLight(1);
  71.             }
  72.         });
  73.  
  74.         rbGreen.setOnAction(e -> {
  75.             if (rbGreen.isSelected()) {
  76.                 changeLight(2);
  77.             }
  78.         });
  79.     }
  80.    
  81.     public void changeLight(int lightNum){
  82.        
  83.         redLight.setOpacity(dim);
  84.         yellowLight.setOpacity(dim);
  85.         greenLight.setOpacity(dim);
  86.        
  87.         if (lightNum == 0){
  88.             redLight.setOpacity(lit);
  89.         }
  90.         if (lightNum == 1){
  91.             yellowLight.setOpacity(lit);
  92.         }
  93.         if (lightNum == 2){
  94.             greenLight.setOpacity(lit);
  95.         }
  96.        
  97.     }
  98.  
  99.     private HBox getBottomHBox() {
  100.  
  101.         HBox bottomHBox = new HBox(40);
  102.         bottomHBox.setPadding(new Insets(10, 10, 10, 10));
  103.         bottomHBox.setStyle("-fx-border-color: black");
  104.         bottomHBox.setAlignment(Pos.CENTER);
  105.         rbRed = new RadioButton("Red");
  106.         rbYellow = new RadioButton("Yellow");
  107.         rbGreen = new RadioButton("Green");
  108.  
  109.         bottomHBox.getChildren().addAll(rbRed, rbYellow, rbGreen);
  110.  
  111.         ToggleGroup group = new ToggleGroup();
  112.         rbRed.setToggleGroup(group);
  113.         rbYellow.setToggleGroup(group);
  114.         rbGreen.setToggleGroup(group);
  115.  
  116.         rbRed.setSelected(true);
  117.  
  118.         return bottomHBox;
  119.     }
  120.  
  121.     public static void main(String[] args) {
  122.         Application.launch(args);
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement