Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Created April 6, 2020 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/252f3d0642ffc5178763c1615d6dc003 to your computer and use it in GitHub Desktop.
Save bytecodeman/252f3d0642ffc5178763c1615d6dc003 to your computer and use it in GitHub Desktop.
CSC-112 HW6 Traffic Light Simulator
/*
* Antonio C. Silvestri
* Traffic Light Simulator Application
* Control Buttons Class
* Exam #2
* CSC-112 Intermediate Java
* 4/6/2020
*/
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
public class ControlButtonsPane extends HBox {
public ControlButtonsPane(TrafficLightPane tlp) {
this.setSpacing(10);
this.setAlignment(Pos.CENTER);
Button btnRed = new Button("Red");
btnRed.setOnAction(e -> tlp.turnRed());
Button btnYellow = new Button("Yellow");
btnYellow.setOnAction(e -> tlp.turnYellow());
Button btnGreen = new Button("Green");
btnGreen.setOnAction(e -> tlp.turnGreen());
this.getChildren().addAll(btnRed, btnYellow, btnGreen);
}
}
/*
* Antonio C. Silvestri
* Traffic Light Simulator Application
* Main App Class
* Exam #2
* CSC-112 Intermediate Java
* 4/6/2020
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class TrafficLightApp extends Application {
@Override
public void start(Stage primaryStage) {
TrafficLightPane tlp = new TrafficLightPane();
ControlButtonsPane cp = new ControlButtonsPane(tlp);
BorderPane bp = new BorderPane();
bp.setCenter(tlp);
bp.setBottom(cp);
Scene scene = new Scene(bp);
primaryStage.setTitle("Traffic Light App");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
/*
* Antonio C. Silvestri
* Traffic Light Simulator Application
* Traffic Light Class
* Exam #2
* CSC-112 Intermediate Java
* 4/6/2020
*/
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
public class TrafficLightPane extends Pane {
private static final int RED = 0;
private static final int YELLOW = 1;
private static final int GREEN = 2;
private static final int RADIUS = 30;
private int state = RED;
public TrafficLightPane() {
this.setPrefSize(300, 350);
this.paint();
}
private void paint() {
this.getChildren().clear();
this.setStyle("-fx-background: grey");
Rectangle rect = new Rectangle();
rect.setStroke(Color.BLACK);
rect.setFill(Color.WHITE);
rect.setX(110);
rect.setY(40);
rect.setHeight(220);
rect.setWidth(80);
Circle redCircle = new Circle(RADIUS);
redCircle.setCenterX(150);
redCircle.setCenterY(80);
redCircle.setStroke(Color.BLACK);
redCircle.setFill(this.state == RED ? Color.RED : Color.WHITE);
Circle yellowCircle = new Circle(RADIUS);
yellowCircle.setCenterX(150);
yellowCircle.setCenterY(150);
yellowCircle.setStroke(Color.BLACK);
yellowCircle.setFill(this.state == YELLOW ? Color.YELLOW : Color.WHITE);
Circle greenCircle = new Circle(RADIUS);
greenCircle.setCenterX(150);
greenCircle.setCenterY(220);
greenCircle.setStroke(Color.BLACK);
greenCircle.setFill(this.state == GREEN ? Color.GREEN : Color.WHITE);
getChildren().addAll(rect, redCircle, yellowCircle, greenCircle);
}
public void turnRed() {
this.state = RED;
paint();
}
public void turnYellow() {
this.state = YELLOW;
paint();
}
public void turnGreen() {
this.state = GREEN;
paint();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment