How to create a view like this in Java-fx? - javafx

I'm having some problems with this view. What I need to do it in JavaFX to add this 'border' and divide the circle into 2 parts.
public void start(Stage primaryStage){
Circle mycircle = new Circle(200,200,200);
mycircle.setFill(Color.GREEN);
BorderPane root = new BorderPane();
root.setCenter(mycircle);
}
I don't have errors but it's not the view what I am looking for. So can anyone help me with this, please
And btw sorry for bad image quality

Would this satisfy your needs?
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class HalfCircleDemo extends Application {
#Override
public void start(final Stage stage) {
BorderPane root = new BorderPane();
Group circleGroup = new Group();
Circle greenCircle = new Circle(200,200,200);
greenCircle.setFill(Color.GREEN);
Circle blueCircle = new Circle(200,200,200);
blueCircle.setFill(Color.BLUE);
Rectangle clip = new Rectangle(400, 200);
greenCircle.setClip(clip);
circleGroup.getChildren().setAll(blueCircle, greenCircle);
root.setCenter(circleGroup);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

You could use Paths with ArcTo elements:
private static Group createHalfCircles(double radius) {
Path upperHalf = new Path(
new MoveTo(0, radius),
new ArcTo(radius, radius, 0, 2*radius, radius, true, false),
new ClosePath());
upperHalf.setFill(Color.PURPLE);
upperHalf.setStroke(null);
Path lowerHalf = new Path(
new MoveTo(2 * radius, radius),
new ArcTo(radius, radius, 0, 0, radius, true, false),
new ClosePath());
lowerHalf.setFill(Color.GREEN);
lowerHalf.setStroke(null);
return new Group(upperHalf, lowerHalf);
}
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createHalfCircles(50));
primaryStage.setScene(scene);
primaryStage.show();
}

Related

How to stop transition when checkbox is unchecked javafx

So I've made a checkbox that applies a scale transition to a rectangle when checked. But the problem is that the transition keeps going even after I uncheck the checkbox. Any ideas on how to make it stop after un-checking?
checkbox.setOnAction(e -> {
ScaleTransition scaleT = new ScaleTransition(Duration.seconds(5), rectangle);
scaleT.setAutoReverse(true);
scaleT.setCycleCount(Timeline.INDEFINITE);
scaleT.setToX(2);
scaleT.setToY(2);
scaleT.play();
});
To control the animation, you need to define the transistion(with INDEFINITE cycle count) outside the CheckBox listener/action. Then you can just play/pause the animation as you required.
Below is the quick demo:
import javafx.animation.ScaleTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
import javafx.util.Duration;
public class ScaleTransitionDemo extends Application {
#Override
public void start(Stage stage) {
Shape rectangle = new Rectangle(50, 50, Color.BLUE);
ScaleTransition transition = new ScaleTransition(Duration.seconds(1), rectangle);
transition.setDuration(Duration.seconds(1));
transition.setAutoReverse(true);
transition.setCycleCount(Timeline.INDEFINITE);
transition.setToX(3);
transition.setToY(3);
CheckBox checkBox = new CheckBox("Animate");
checkBox.selectedProperty().addListener((obs, old, selected) -> {
if (selected) {
transition.play();
} else {
transition.pause();
}
});
StackPane pane = new StackPane(rectangle);
VBox.setVgrow(pane, Priority.ALWAYS);
VBox root = new VBox(20, checkBox, pane);
root.setPadding(new Insets(10));
Scene scene = new Scene(root, 300, 300);
stage.setScene(scene);
stage.setTitle("Scale transition");
stage.show();
}
public static void main(String[] args) {
launch();
}
}
checking whether checkbox is selected or not with .isSelected() method . In this approach , scaled node will back to xy = 1 scale if checkbox is unchecked , but it will be disabled until transition ends .You can adjust setDuration . I've changed it just for gif recording. This is a single class javafx app you can try .
App.java
public class App extends Application {
#Override
public void start(Stage stage) {
Shape rectangle = new Rectangle(50, 50, Color.BLUE);
ScaleTransition scaleT = new ScaleTransition(Duration.seconds(1), rectangle);
CheckBox checkBox = new CheckBox("scale");
checkBox.setOnAction(e -> {
if (checkBox.isSelected()) {
scaleT.setDuration(Duration.seconds(1));
scaleT.setAutoReverse(true);
scaleT.setCycleCount(Timeline.INDEFINITE);
scaleT.setToX(2);
scaleT.setToY(2);
scaleT.play();
} else {
scaleT.setDuration(scaleT.getCurrentTime());
scaleT.stop();
scaleT.setCycleCount(1);
scaleT.setToX(1);
scaleT.setToY(1);
scaleT.play();
checkBox.setDisable(true);
scaleT.setOnFinished((t) -> {
checkBox.setDisable(false);
});
}
});
var scene = new Scene(new HBox(50, rectangle, checkBox), 640, 480);
stage.setScene(scene);
stage.setTitle("scale transition");
stage.show();
}
public static void main(String[] args) {
launch();
}
}

Align a Hbox to the bottom center inside a StakePanel

I'm learning Javafx layouts and I would like to obtain the following result:
HBox in the bottom center fo a Pane I have tried with the following code but I'm not able to do that
Can you please explain what I'm doing wrong? and How to achieve that?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TestLayout extends Application {
#Override
public void start(Stage stage) {
initUI(stage);
}
private void initUI(Stage stage) {
Scene scene =null;
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
StackPane pane = new StackPane();
hbox.setStyle("-fx-background-color: #AAFAAA;");
hbox.prefWidthProperty().bind(pane.widthProperty().divide(4));
hbox.prefHeightProperty().bind(pane.heightProperty().divide(10));
pane.getChildren().add(hbox);
scene = new Scene(pane, 600, 600);
pane.prefWidthProperty().bind(scene.widthProperty());
pane.prefHeightProperty().bind(scene.heightProperty());
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
class AppLauncherTest {
public static void main(String[] args) {
TestLayout.main(args);
}
}
I would Use a VBox or BorderPane as the root node. In this example, I use a VBox. I am assuming more nodes will go into this so I used a StackPane as the top node. This may need to be changed out or some other Pane may need to be added to this. That depends on what you are trying to do with the end product. For the bottom, I used an HBox and I set it's Margins.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.Priority;
import javafx.geometry.Insets;
/**
* JavaFX App
*/
public class App extends Application {
#Override
public void start(Stage stage) {
StackPane subRootTop = new StackPane();
VBox.setVgrow(subRootTop, Priority.ALWAYS);
//subRootTop.setStyle("-fx-background-color: yellow;");
HBox subRootBotton = new HBox();
VBox.setVgrow(subRootBotton, Priority.ALWAYS);
subRootBotton.setStyle("-fx-background-color: green;");
subRootBotton.setMaxHeight(150);
VBox.setMargin(subRootBotton, new Insets(40, 40, 40, 40));
VBox root = new VBox(subRootTop, subRootBotton);
root.setStyle("-fx-background-color: red;");
Scene scene = new Scene(root, 600, 600);;
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
The binding of the StackPane is not size shouldn't be done. The scene automatically resizes its root to fill the whole area available. Assuming you want relative sizes, using StackPane is not really a good choice.
If you want the child to have a fixed distance to left right and bottom of the StackPane, you can do so specifying margins and alignment. Make sure the child does not grow to fit the parent though by setting the maxHeight to use the preferred height:
private void initUI(Stage stage) {
HBox hbox = new HBox();
hbox.setStyle("-fx-background-color: #FFFAAA;");
hbox.setMaxHeight(Region.USE_PREF_SIZE);
// could be calculated based on children instead of assigning
// an absolute value
hbox.setPrefHeight(30);
StackPane.setAlignment(hbox, Pos.BOTTOM_CENTER);
StackPane.setMargin(hbox, new Insets(20));
StackPane pane = new StackPane(hbox);
pane.setStyle("-fx-background-color: #AAFAAA;");
Scene scene = new Scene(pane, 600, 600);
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
Maybe you can use an AnchorPane as a base layout instead of the StacKPane. I made a small exmaple how it would look like. Of cause you could keep the StackPane as the base layout and just put the AnchorPane with the HBox onto it, but you should definitly use an AnchorPane for your plan.
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
private void initUI(Stage stage) {
// Create an anchor pane as base layout and set a color:
AnchorPane root = new AnchorPane();
root.setStyle("-fx-background-color: #FFFAAA;");
// Create a second container and set a minimum height and a color;
HBox hbox = new HBox();
hbox.setMinHeight(100d);
hbox.setStyle("-fx-background-color: #AAFAAA;");
// Give the child container a fixed location:
AnchorPane.setBottomAnchor(hbox, 50d);
AnchorPane.setLeftAnchor(hbox, 75d);
AnchorPane.setRightAnchor(hbox, 75d);
// Add the horizontal box to the base anchor pane:
root.getChildren().add(hbox);
stage.setScene(new Scene(root, 600, 600));
stage.show();
}
#Override
public void start(Stage stage) throws Exception {
initUI(stage);
}
public static void main(String[] args) {
launch(args);
}
}
Preview:

How to clip and resize javafx borderpane

I am just starting with JavaFX. I want to have a BorderPane with controls on top, left, and right, and an image in the center. I want the center pane to resize as you resize the window, but to always be able to see all left, right, and top controls.
With the code below, I can show a button in the left, top, and right. And I can display an image in the center.
But the image expands beyond center bounds and hides the right button.
Oddly, if I set a clipping rectangle on the imageview in the center pane (uncomment lines 67 & 68), it does in fact only draw the clipped region, but the rest of the layout behaves as if it were drawing the whole picture. That is, the UNDRAWN part of the image still obscures the button on the right.
Any help would be much appreciated.
Thanks in advance and apologies if it's simple.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ImageApp extends Application {
private BorderPane root;
private Rectangle clipRect;
private ImageView iv;
private StackPane leftPane;
private StackPane rightPane;
private Button topButton;
private Button leftButton;
private Button rightButton;
#Override
public void start(Stage primaryStage) {
root = new BorderPane();
Scene primaryScene = new Scene(root, 900, 800);
initializePrimaryStage(primaryStage, primaryScene);
initializeFrameContent(root, topButton, leftButton);
initializeContent(root);
primaryStage.show();
}
private void initializeFrameContent(BorderPane root, Button topButton, Button leftButton) {
topButton = new Button("TOP");
leftButton = new Button("LEFT");
rightButton = new Button("RIGHT");
leftPane = new StackPane(leftButton);
leftPane.setAlignment(Pos.TOP_LEFT);
rightPane = new StackPane(rightButton);
rightPane.setAlignment(Pos.TOP_RIGHT);
root.setLeft(leftPane);
root.setTop(topButton);
root.setRight(rightButton);
}
private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
primaryStage.setTitle("Image Clip Test");
primaryStage.setScene(primaryScene);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.minWidthProperty().setValue(400);
primaryStage.minHeightProperty().setValue(300);
}
public static void main(String[] args) {
launch(args);
}
private void initializeContent(BorderPane root) {
Image image = new Image(
"http://www.ciee.org/study-abroad/images/cities/0020/headers/desktop/big-ben-london-traffic-trafalgar-abroad-studies.jpg"
);
iv = new ImageView(image);
root.setCenter(iv);
//clipRect = new Rectangle(400,200);
//root.getCenter().setClip(clipRect);
}
}
You don't specify what you intend to do. Why would you want to clip the content? The way you describe it all you want is some background that's getting clipped. You can do that with various mechanisms, e. g. css.
Or you could use a proper parent, e. g. a ScrollPane in order to limit the region or e. g. an ImageViewPane in order to stretch to fit:
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ImageApp extends Application {
private BorderPane root;
private Rectangle clipRect;
private ImageView iv;
private StackPane leftPane;
private StackPane rightPane;
private Button topButton;
private Button leftButton;
private Button rightButton;
#Override
public void start(Stage primaryStage) {
root = new BorderPane();
Scene primaryScene = new Scene(root, 900, 800);
initializePrimaryStage(primaryStage, primaryScene);
initializeFrameContent(root, topButton, leftButton);
initializeContent(root);
primaryStage.show();
}
private void initializeFrameContent(BorderPane root, Button topButton, Button leftButton) {
topButton = new Button("TOP");
leftButton = new Button("LEFT");
rightButton = new Button("RIGHT");
leftPane = new StackPane(leftButton);
leftPane.setAlignment(Pos.TOP_LEFT);
rightPane = new StackPane(rightButton);
rightPane.setAlignment(Pos.TOP_RIGHT);
root.setLeft(leftPane);
root.setTop(topButton);
root.setRight(rightButton);
}
private void initializePrimaryStage(Stage primaryStage, Scene primaryScene) {
primaryStage.setTitle("Image Clip Test");
primaryStage.setScene(primaryScene);
primaryStage.setWidth(400);
primaryStage.setHeight(300);
primaryStage.minWidthProperty().setValue(400);
primaryStage.minHeightProperty().setValue(300);
}
public static void main(String[] args) {
launch(args);
}
private void initializeContent(BorderPane root) {
Image image = new Image(
"http://www.ciee.org/study-abroad/images/cities/0020/headers/desktop/big-ben-london-traffic-trafalgar-abroad-studies.jpg"
);
iv = new ImageView(image);
// ImageViewPane content = new ImageViewPane( iv);
ScrollPane content = new ScrollPane( imageView);
// hide scrollbars
content.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
content.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
content.setPadding(Insets.EMPTY);
root.setCenter(content);
}
// code from here: https://stackoverflow.com/questions/22993550/how-to-resize-an-image-when-resizing-the-window-in-javafx
class ImageViewPane extends Region {
private ObjectProperty<ImageView> imageViewProperty = new SimpleObjectProperty<ImageView>();
public ObjectProperty<ImageView> imageViewProperty() {
return imageViewProperty;
}
public ImageView getImageView() {
return imageViewProperty.get();
}
public void setImageView(ImageView imageView) {
this.imageViewProperty.set(imageView);
}
public ImageViewPane() {
this(new ImageView());
}
#Override
protected void layoutChildren() {
ImageView imageView = imageViewProperty.get();
if (imageView != null) {
imageView.setFitWidth(getWidth());
imageView.setFitHeight(getHeight());
layoutInArea(imageView, 0, 0, getWidth(), getHeight(), 0, HPos.CENTER, VPos.CENTER);
}
super.layoutChildren();
}
public ImageViewPane(ImageView imageView) {
imageViewProperty.addListener(new ChangeListener<ImageView>() {
#Override
public void changed(ObservableValue<? extends ImageView> arg0, ImageView oldIV, ImageView newIV) {
if (oldIV != null) {
getChildren().remove(oldIV);
}
if (newIV != null) {
getChildren().add(newIV);
}
}
});
this.imageViewProperty.set(imageView);
}
}
}

Show pies percentage of PieChart

For the MVCE underneath, how can you add a label on each pie in the piechart that shows its percentage of the total pie? In the JavaDoc they only add a % after the value, which isn't any solution.
public class PieChartSample extends Application {
#Override public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Imported Fruits");
stage.setWidth(500);
stage.setHeight(500);
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22),
new PieChart.Data("Apples", 30));
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Imported Fruits");
((Group) scene.getRoot()).getChildren().add(chart);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
All you have to do is iterate through the pie chart data and add up the values to get the total. Then divide the current value by the total.
Adapting the (awful) example from the link you provided:
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class PieChartSample extends Application {
#Override public void start(Stage stage) {
Pane root = new Pane();
Scene scene = new Scene(root);
stage.setTitle("Imported Fruits");
stage.setWidth(500);
stage.setHeight(500);
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Grapefruit", 13),
new PieChart.Data("Oranges", 25),
new PieChart.Data("Plums", 10),
new PieChart.Data("Pears", 22),
new PieChart.Data("Apples", 30));
final PieChart chart = new PieChart(pieChartData);
chart.setTitle("Imported Fruits");
final Label caption = new Label("");
caption.setTextFill(Color.DARKORANGE);
caption.setStyle("-fx-font: 24 arial;");
for (final PieChart.Data data : chart.getData()) {
data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
e -> {
double total = 0;
for (PieChart.Data d : chart.getData()) {
total += d.getPieValue();
}
caption.setTranslateX(e.getSceneX());
caption.setTranslateY(e.getSceneY());
String text = String.format("%.1f%%", 100*data.getPieValue()/total) ;
caption.setText(text);
}
);
}
root.getChildren().addAll(chart, caption);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If you want to avoid recomputing the total every time there's a mouse event, you can create a DoubleBinding to store it:
DoubleBinding total = Bindings.createDoubleBinding(() ->
pieChartData.stream().collect(Collectors.summingDouble(PieChart.Data::getPieValue)), pieChartData);
and then just
for (final PieChart.Data data : chart.getData()) {
data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
e -> {
caption.setTranslateX(e.getSceneX());
caption.setTranslateY(e.getSceneY());
String text = String.format("%.1f%%", 100*data.getPieValue()/total.get()) ;
caption.setText(text);
}
);
}

Drawing rounded rectangle while dragging fails

I am trying to draw a simple rectangle with rounded corners in javafx while dragging the mouse.
First issue: I can't make the upper-left corner rounded.
Second issue: I want my application to be able to draw nice and full rounded rectangles not like the second one from my picture.
Please, how can I fix these?
Thanks in advance for your help.
Here is my code:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage){
stage.setTitle("Test");
root = new BorderPane();
Scene scene = new Scene(root, 400, 400);
paintings = new Group();
stage.setScene(scene);
canvas = new Rectangle(0, 0, stage.getScene().getWidth(), stage.getScene().getHeight());
canvas.setFill(Color.WHITE);
addHandlers();
root.setCenter(canvas);
root.getChildren().add(paintings);
stage.show();
}
private void paintRectangle(){
roundedRect = new Rectangle(pressedX,
pressedY,
draggedX - pressedX,
draggedY - pressedY);
roundedRect.setFill(Color.RED);
roundedRect.setArcHeight(40);
roundedRect.setArcWidth(40);
paintings.getChildren().add(roundedRect);
}
private void addHandlers(){
canvas.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
pressedX = me.getX();
pressedY = me.getY();
}
});
canvas.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
/* If I make the drawing in here, I get round corners, but it doesn't get drawn while dragging*/
//paintRectangle();
}
});
canvas.setOnMouseDragged(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent me) {
draggedX = me.getX();
draggedY = me.getY();
paintRectangle();
}
});
}
double pressedX, pressedY;
double draggedX, draggedY;
Rectangle canvas;
Group paintings;
Rectangle roundedRect;
BorderPane root;
}
And here is my picture:
I could not examine your code but here is an alternative approach. You may merge this with your code:
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Daer extends Application {
BorderPane pane;
Rectangle rect;
SimpleDoubleProperty rectinitX = new SimpleDoubleProperty();
SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();
SimpleDoubleProperty rectX = new SimpleDoubleProperty();
SimpleDoubleProperty rectY = new SimpleDoubleProperty();
#Override
public void start(Stage stage) {
pane = new BorderPane();
Scene scene = new Scene(pane, 800, 600);
stage.setScene(scene);
scene.setOnMouseDragged(mouseHandler);
scene.setOnMousePressed(mouseHandler);
scene.setOnMouseReleased(mouseHandler);
rect = getNewRectangle();
rect.widthProperty().bind(rectX.subtract(rectinitX));
rect.heightProperty().bind(rectY.subtract(rectinitY));
pane.getChildren().add(rect);
stage.show();
}
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
rect.setX(mouseEvent.getX());
rect.setY(mouseEvent.getY());
rectinitX.set(mouseEvent.getX());
rectinitY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
rectX.set(mouseEvent.getX());
rectY.set(mouseEvent.getY());
} else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
// Clone the rectangle
Rectangle r = getNewRectangle();
r.setX(rect.getX());
r.setY(rect.getY());
r.setWidth(rect.getWidth());
r.setHeight(rect.getHeight());
pane.getChildren().add(r);
// Hide the rectangle
rectX.set(0);
rectY.set(0);
}
}
};
private Rectangle getNewRectangle() {
Rectangle r = new Rectangle();
r.setFill(Color.web("blue", 0.1));
r.setStroke(Color.BLUE);
r.setArcHeight(40);
r.setArcWidth(40);
return r;
}
public static void main(String[] args) {
launch(args);
}
}

Resources