Question

In: Computer Science

Create a JavaFX program in java that does the following items: Display a drawing area of...

Create a JavaFX program in java that does the following items:

  1. Display a drawing area of dimension 500 x 400, with a black background.
  2. Provides a radio button group to allow the user to select one of the following three colors: red, green, and blue. Upon startup, the red radio button should be selected.
  3. Each time the user clicks in the drawing area, a circle of size 10 of the color selected by the radio button group in item 2 will be drawn at where the user clicks.
  4. When the use selects a color in the radio button group in item 2, all the circles drawn in the drawing area should change to the color of selection.

Solutions

Expert Solution

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*/


Related Solutions

In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Create a program in java that calculates area and perimeter of a square - use a...
Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
In Java create a program that does the following. Suppose you shop for rice in two...
In Java create a program that does the following. Suppose you shop for rice in two different packages. You would like to write a program to compare the cost. The program prompts the user to enter the weight and price of each package and displays the one with the better price. Here are two sample runs: Enter the weight for package 1: 50 Enter the price for package 1: 24.59 Enter the weight for package 2: 25 Enter the price...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Java code for a binary tree that does the following:  Display a menu to the...
Java code for a binary tree that does the following:  Display a menu to the user and request for the desired option.  Based on the user’s input, request for additional information as follows: o If the user wants to add a node, request for the name (or ID) of the new node to be added as well as the name of the desired parent of that node.  If the parent node already has two children, the new...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using...
Using Eclipse IDE Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs...
in java What object is passed to the start method in a JavaFX program? What should...
in java What object is passed to the start method in a JavaFX program? What should you do with it?
implement a JavaFX program to demonstrate skills and knowledge using the following: 1.General Java programming skills...
implement a JavaFX program to demonstrate skills and knowledge using the following: 1.General Java programming skills (e.g. conditionals, branching and loops) as well as object-oriented concepts) 2. Writing JavaFX applications incl. using fxml 3. • GUI Layouts (organizing UI controls) I just need some samples and explanations.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT