Javafx Create a program that turns a circle ON (yellow) or OFF (blank). Do not u
ID: 3778757 • Letter: J
Question
Javafx
Create a program that turns a circle ON (yellow) or OFF (blank).
Do not use images in the buttons, just text.
This is what I came up with
CODE:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
public class ButtonDe extends Application {
protected Circle c = new Circle(50);
protected BorderPane getPane() {
HBox paneForButtons = new HBox(20);
Button btLeft = new Button("Left");
Button btRight = new Button("Right");
paneForButtons.getChildren().addAll(btLeft, btRight);
paneForButtons.setAlignment(Pos.CENTER);
paneForButtons.setStyle("-fx-border-color: green");
BorderPane pane = new BorderPane();
pane.setBottom(paneForButtons);
Pane paneForCircle = new Pane();
paneForCircle.getChildren().add(c);
pane.setCenter(paneForCircle);
btLeft.setOnAction(e -> c.setFill(Color.RED));
btRight.setOnAction(e -> c.setFill(Color.GREEN));
return pane;
}
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a scene and place it in the stage
Scene scene = new Scene(getPane(), 500, 500);
primaryStage.setTitle("ButtonDemo"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
I need to control and keep the Circle in the middel
Explanation / Answer
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; public class Main extends launchApplication { public static void main(String[] args) { launchApplication.launch(args); } public void start(Stage createaprimaryStage) { createaprimaryStage.setTitle("create a colored blinking cicle"); Group newroot = new Group(); Scene newscene = new Scene(root, 550, 250,Color.web("YELLOW")); Circle createcircle = new Circle(); createcircle.setCenterX(100.0f); createcircle.setCenterY(100.0f); createcircle.setRadius(50.0f); newroot.getChildren().add(circle); createaprimaryStage.setScene(scene); createaprimaryStage.show(); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.