-
-
Save bytecodeman/252f3d0642ffc5178763c1615d6dc003 to your computer and use it in GitHub Desktop.
CSC-112 HW6 Traffic Light Simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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