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 Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
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.
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
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...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT