-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDraggableNode.java
134 lines (109 loc) · 3.98 KB
/
DraggableNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
* Code snippet from www.crazyandcoding.com
*
* @author Colt
*
*/
public class DraggableNode extends Application {
/**
* The current x coordinate of the node.
*/
private double m_nX = 0;
/**
* The current y coordinate of the node.
*/
private double m_nY = 0;
/**
* The current mouse x coordinate when dragging.
*/
private double m_nMouseX = 0;
/**
* The current mouse y coordinate when dragging.
*/
private double m_nMouseY = 0;
/**
* The node that will be draggable.
*/
private Group m_draggableNode;
@Override
public void start(Stage primaryStage) throws Exception {
m_draggableNode = new Group();
// create a rectangle to be displayed.
Rectangle rectangle = new Rectangle();
rectangle.setWidth(95);
rectangle.setHeight(55);
rectangle.setFill(Color.CHARTREUSE);
rectangle.setStroke(Color.BLACK);
rectangle.setStrokeWidth(1);
m_draggableNode.getChildren().add(rectangle);
// add the event handlers to the mouse events.
m_draggableNode.setOnMousePressed(pressMouse());
m_draggableNode.setOnMouseDragged(dragMouse());
// add the draggable node to the root.
Group root = new Group();
root.getChildren().add(m_draggableNode);
Scene scene = new Scene(root, 800, 800);
primaryStage.setTitle("Draggable Node");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* Creates an event handler that handles a mouse press on the node.
*
* @return the event handler.
*/
private EventHandler<MouseEvent> pressMouse() {
EventHandler<MouseEvent> mousePressHandler = new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) {
// get the current mouse coordinates according to the scene.
m_nMouseX = event.getSceneX();
m_nMouseY = event.getSceneY();
// get the current coordinates of the draggable node.
m_nX = m_draggableNode.getLayoutX();
m_nY = m_draggableNode.getLayoutY();
}
}
};
return mousePressHandler;
}
/**
* Creates an event handler that handles a mouse drag on the node.
*
* @return the event handler.
*/
private EventHandler<MouseEvent> dragMouse() {
EventHandler<MouseEvent> dragHandler = new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) {
// find the delta coordinates by subtracting the new mouse
// coordinates with the old.
double deltaX = event.getSceneX() - m_nMouseX;
double deltaY = event.getSceneY() - m_nMouseY;
// add the delta coordinates to the node coordinates.
m_nX += deltaX;
m_nY += deltaY;
// set the layout for the draggable node.
m_draggableNode.setLayoutX(m_nX);
m_draggableNode.setLayoutY(m_nY);
// get the latest mouse coordinate.
m_nMouseX = event.getSceneX();
m_nMouseY = event.getSceneY();
}
}
};
return dragHandler;
}
public static void main(String[] args) {
launch(args);
}
}