In: Computer Science
In Java, Write a JavaFX application that draws multiple circles 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.Thank you and could you also show the finished product?
Please find the code below:
DrawCircleOnDragWithRadius.java
package gui;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class DrawCircleOnDragWithRadius extends Application {
double startingX,endingX;
double startingY,endingY;
Circle circle;
boolean start;
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 1000,
1000);
scene.setOnMouseDragged((t) ->
{
if(start){
startingX = t.getSceneX();
startingY = t.getSceneY();
circle.setCenterX(startingX);
circle.setCenterY(startingY);
start = false;
}
double dist =
Math.sqrt(Math.pow(startingX-t.getSceneX(),
2)+Math.pow(startingY-t.getSceneY(), 2));
circle.setRadius(dist);
});
circle = new Circle(0,0,0);
scene.setOnMouseClicked((t) ->
{
start =
true;
});
start = true;
root.getChildren().add(circle);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
output: