Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created March 25, 2021 17:40
Show Gist options
  • Save jananpatel2002/8b97a6ef13640a2df38a538dcb9d261f to your computer and use it in GitHub Desktop.
Save jananpatel2002/8b97a6ef13640a2df38a538dcb9d261f to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 3/20/2021
* Course Number: CSC 112
* Course Name: Computer Science
* Problem Number: 7
* Email:jkpatel2001@student.stcc.edu
* Short Description of the Problem: Traffic lights with buttons
*/
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
public class Trafficlights extends Application {
public void start(Stage primaryStage) {
Rectangle rectangle = new Rectangle();
rectangle.setFill(Color.WHITE);
rectangle.setWidth(150);
rectangle.setHeight(250);
rectangle.setStroke(Color.BLACK);
rectangle.setStrokeWidth(2);
VBox circlePane = new VBox(10);
circlePane.setAlignment(Pos.CENTER);
Circle circle1 = new Circle(35);
circle1.setFill(Color.RED);
circle1.setStroke(Color.BLACK);
Circle circle2 = new Circle(35);
circle2.setFill(Color.WHITE);
circle2.setStroke(Color.BLACK);
Circle circle3 = new Circle(35);
circle3.setFill(Color.WHITE);
circle3.setStroke(Color.BLACK);
circlePane.getChildren().addAll(circle1, circle2, circle3);
// Creates a stack pane to stack these two together.
StackPane box = new StackPane();
box.getChildren().addAll(rectangle, circlePane);
Button buttonRed = new Button("Red");
Button buttonYellow = new Button("Yellow");
Button buttonGreen = new Button("Green");
HBox buttons = new HBox(10);
buttons.setAlignment(Pos.CENTER);
buttons.getChildren().addAll(buttonRed, buttonYellow, buttonGreen);
BorderPane pane = new BorderPane();
pane.setCenter(box);
pane.setBottom(buttons);
Scene scene = new Scene(pane, 250, 400);
primaryStage.setTitle("Traffic light");
primaryStage.setScene(scene);
primaryStage.show();
// actions
buttonRed.setOnAction(x -> {
circle1.setFill(Color.RED);
circle2.setFill(Color.WHITE);
circle3.setFill(Color.WHITE);
});
buttonYellow.setOnAction(x -> {
circle1.setFill(Color.WHITE);
circle2.setFill(Color.YELLOW);
circle3.setFill(Color.WHITE);
});
buttonGreen.setOnAction(x -> {
circle1.setFill(Color.WHITE);
circle2.setFill(Color.WHITE);
circle3.setFill(Color.GREEN);
});
}
// main method for the IDE to run the program
public static void main(String[] args) {
launch(args);
}
}
/*
* Name: Janan Patel
* Date: 3/20/2021
* Course Number: CSC 112
* Course Name: Computer Science
* Problem Number: 7
* Email:jkpatel2001@student.stcc.edu
* Short Description of the Problem: Traffic lights without buttons
*/
// CLICK THE CIRCLES TO CHANGE COLOR!
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
public class Trafficlightswithoutbtns extends Application {
public void start(Stage primaryStage) {
Rectangle rectangle = new Rectangle();
rectangle.setFill(Color.WHITE);
rectangle.setWidth(150);
rectangle.setHeight(250);
rectangle.setStroke(Color.BLACK);
rectangle.setStrokeWidth(2);
VBox circlePane = new VBox(10);
circlePane.setAlignment(Pos.CENTER);
Circle circle1 = new Circle(35);
circle1.setFill(Color.RED);
circle1.setStroke(Color.BLACK);
Circle circle2 = new Circle(35);
circle2.setFill(Color.WHITE);
circle2.setStroke(Color.BLACK);
Circle circle3 = new Circle(35);
circle3.setFill(Color.WHITE);
circle3.setStroke(Color.BLACK);
circlePane.getChildren().addAll(circle1, circle2, circle3);
// Creates a stack pane to stack these two together.
StackPane box = new StackPane();
box.getChildren().addAll(rectangle, circlePane);
BorderPane pane = new BorderPane();
pane.setCenter(box);
Scene scene = new Scene(pane, 250, 400);
primaryStage.setTitle("Traffic light without buttons");
primaryStage.setScene(scene);
primaryStage.show();
// actions
circle1.setOnMouseClicked(x -> {
circle1.setFill(Color.RED);
circle2.setFill(Color.WHITE);
circle3.setFill(Color.WHITE);
});
circle2.setOnMouseClicked(x -> {
circle1.setFill(Color.WHITE);
circle2.setFill(Color.YELLOW);
circle3.setFill(Color.WHITE);
});
circle3.setOnMouseClicked(x -> {
circle1.setFill(Color.WHITE);
circle2.setFill(Color.WHITE);
circle3.setFill(Color.GREEN);
});
}
// main method for the IDE to run the program
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment