Question

In: Computer Science

Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is...

Write a JavaFX application that draws a circle using a rubberbanding technique. The circle size is determined by a mouse drag. Use the initial mouse press location as the fixed center point of the circle. Compute the distance between the current location of the mouse pointer and the center point to determine the current radius of the circle.

Solutions

Expert Solution

Hi. I have answered similar questions before. 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

// DrawCircles.java

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.stage.Stage;

import javafx.scene.input.MouseEvent;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Circle;

public class DrawCircles extends Application {

    private Scene scene;

    private Circle c;

    //a flag denoting if a circle is being drawn or not

   private boolean isDrawing;

    Pane pane;

    @Override

    public void start(Stage primaryStage) {

        //initializing circle to null

        c = null;

       

        //setting isDrawing to false

        isDrawing = false;

       

        //initializing pane

        pane = new Pane();

       

        //initializing scene.

        scene = new Scene(pane, 500, 500, Color.WHITE);

       

        //adding mouse listeners to the scene

        scene.setOnMousePressed(this::processMousePress);

        scene.setOnMouseDragged(this::processMouseDrag);

        scene.setOnMouseReleased(this::processMouseRelease);

        //setting up and displaying the scene

        primaryStage.setTitle("Rubberband Circle");

        primaryStage.setScene(scene);

        primaryStage.show();

    }

    //mouse press handler method

    public void processMousePress(MouseEvent e) {

        //only if the circle is not being drawn, setting isDrawing to true

        if (!isDrawing) {

            isDrawing = true;

            //initializing circle

            c = new Circle(e.getX(), e.getY(), 5);

            //using no fill color

            c.setFill(null);

            //using black stroke color

            c.setStroke(Color.BLACK);

            //clearing current contents of pane and adding c.

            //if you want to keep the current circle when drawing another, remove below single line

            pane.getChildren().clear();

            pane.getChildren().add(c);

        }

    }

    //mouse drag handler method

    public void processMouseDrag(MouseEvent e) {

        //using distance formula, finding the distance from current mouse point to circle's center

        double radius = Math.sqrt(Math.pow((e.getX() - c.getCenterX()), 2) + Math.pow((e.getY() - c.getCenterY()), 2));

        //setting above value as circle's radius

        c.setRadius(radius);

    }

    //handler method for mouse release operation

    public void processMouseRelease(MouseEvent e) {

        //simply setting isDrawing to false to enable drawing of next circle on next mouse press

        isDrawing = false;

    }

    public static void main(String[] args) {

        launch(args);

    }

}

/*OUTPUT*/



Related Solutions

JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player...
JAVAFX Create a JavaFx application that is an MP3 player. The size of the media player should be 800 pixels in width and 650 pixels in height. The media player should have a TextField that allows the user to type the full path of the .mp3 file to be played. The application should also use 2 Checkbox controls to show/hide the total playing time and the status (methods of MediaPlayer class). Use 2 RadioButton controls to change background colors (red...
In Java and using JavaFX, write a client/server application with two parts, a server and a...
In Java and using JavaFX, write a client/server application with two parts, a server and a client. Have the client send the server a request to compute whether a number that the user provided is prime. The server responds with yes or no, then the client displays the answer.
Using NetBeans IDE, write a JavaFX application that allows theuser to choose insurance options. Use...
Using NetBeans IDE, write a JavaFX application that allows the user to choose insurance options. Use a ToggleGroup to allow the user to select only one of two insurance types—HMO (health maintenance organization) or PPO (preferred provider organization). Use CheckBoxes for dental insurance and vision insurance options; the user can select one option, both options, or neither option. As the user selects each option, display its name and price in a text field; the HMO costs $200 per month, the...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each...
(Not in Swing) Write a JavaFX application that creates polyline shapes dynamically using mouse clicks. Each mouse click adds a new line segment to the current polyline from the previous point to the current mouse position. Allow the user to end the current polyline with the double click. Also, provide a button that clears the window and allows the user to begin again.
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given...
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given...
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
Write a JavaFX application that presents two buttons and a number (initially 50) to the user....
Write a JavaFX application that presents two buttons and a number (initially 50) to the user. Label the buttons Increment and Decrement. When the increment button is pushed, increment the displayed value. Likewise, decrement the value when the decrement button is pushed. This has to use Java.Fx and not Java.Awt
Java Write a JavaFX application that displays a button and a number. Every time the button...
Java Write a JavaFX application that displays a button and a number. Every time the button is pushed, change the number to a random value between 1 and 100. Thank you and can you show a picture of the result as well
Write a program that draws a fixed circle centered at (100, 60) with radius 50. Whenever...
Write a program that draws a fixed circle centered at (100, 60) with radius 50. Whenever the mouse is moved, display a message indicating whether the mouse point is inside the circle at the mouse point or outside of it. Write in Java please.
(Display a Circle and Its Attributes) Write a program that displays a circle of random size...
(Display a Circle and Its Attributes) Write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations: diameter = 2 × radius, area = π × radius2, circumference = 2 × π × radius. Use the constant Math.PI for pi (π). All drawing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT