In: Computer Science
Create a JavaFX program in java that does the following items:
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//DrawingProgram.java
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
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;
import javafx.stage.Stage;
public class DrawingProgram extends Application {
//declaring important components
private RadioButton redBtn, blueBtn, greenBtn;
private Pane drawingPane;
private Color selectedColor;
@Override
public void start(Stage primaryStage) {
//creating a BorderPane
BorderPane root = new BorderPane();
selectedColor = Color.RED; //default color
//initializing a 500x400 drawing pane
drawingPane = new Pane();
drawingPane.setPrefSize(500, 400);
//using black background
drawingPane.setStyle("-fx-background-color: black;");
//initializing radio buttons
redBtn = new RadioButton("RED");
redBtn.setSelected(true); //default selection
greenBtn = new RadioButton("GREEN");
blueBtn = new RadioButton("BLUE");
//creating a ToggleGroup so that only one button can be selected at a time
ToggleGroup group = new ToggleGroup();
redBtn.setToggleGroup(group);
greenBtn.setToggleGroup(group);
blueBtn.setToggleGroup(group);
//adding buttons to an HBox to arrange horizontally
HBox btns = new HBox(redBtn, greenBtn, blueBtn);
btns.setAlignment(Pos.CENTER);
btns.setPadding(new Insets(10));
btns.setSpacing(10);
//adding drawing pane to center and buttons to bottom
root.setCenter(drawingPane);
root.setBottom(btns);
//adding event listeners for radio buttons, to call updateDrawing() method
redBtn.setOnAction(e -> updateDrawing());
greenBtn.setOnAction(e -> updateDrawing());
blueBtn.setOnAction(e -> updateDrawing());
//adding mouse clicked listener for drawing pane
drawingPane.setOnMouseClicked(e -> {
//creating a 10 radius circle at mouse point
Circle c = new Circle(e.getX(), e.getY(), 10);
//using selected color
c.setFill(selectedColor);
//adding to pane
drawingPane.getChildren().add(c);
});
//setting up and displaying a scene
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
//event handler method for radio btn click
public void updateDrawing() {
//using red as initial color
selectedColor = Color.RED;
//if green is selected, changing selectedColor to green
if (greenBtn.isSelected()) {
selectedColor = Color.GREEN;
} //if blue is selected, changing selectedColor to blue
else if (blueBtn.isSelected()) {
selectedColor = Color.BLUE;
}
//now looping through each circle in drawingPane changing color to selectedColor
for (Object ob : drawingPane.getChildren()) {
Circle circle = (Circle) ob;
circle.setFill(selectedColor);
}
}
public static void main(String[] args) {
launch(args);
}
}
/*OUTPUT*/