Repository navigation
Expand file tree
/
Copy pathPannelloPrincipaleTree.java
More file actions
353 lines (285 loc) · 11.2 KB
/
Copy pathPannelloPrincipaleTree.java
File metadata and controls
353 lines (285 loc) · 11.2 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package code;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class PannelloPrincipaleTree implements Initializable {
@FXML
private Pane pane;
@FXML
private ScrollPane scrollPane;
@FXML
private TextArea log;
@FXML
private Slider slider;
static int index = 0;
private Circle selectedCircle;
private Node selectedNode;
Tree tree = new Tree();
Parent parent;
Stage stage;
Scene scene;
private double mouseX, mouseY; // Store the initial mouse click position
DFS dfs = new DFS();
BFS bfs = new BFS();
public void onStart() {
Circle circle = new Circle(20, Color.BLUE);
circle.setCenterX(350);
circle.setCenterY(40);
Node root = new Node(0, circle);
tree.addNode(root);
Text numberText = new Text("0");
numberText.setFill(Color.WHITE);
numberText.setX(circle.getCenterX() - 5);
numberText.setY(circle.getCenterY() + 5);
root.setNumberText(numberText);
pane.getChildren().addAll(circle, numberText);
circle.setOnMouseClicked(event -> {
log.appendText("Root " + root.getIndexNode() + "\n");
circle.setFill(Color.GREEN);
selectedCircle = circle;
selectedNode = root;
});
circle.setOnMousePressed(e -> {
mouseX = e.getSceneX() - circle.getCenterX();
mouseY = e.getSceneY() - circle.getCenterY();
});
circle.setOnMouseDragged(e -> {
double newCircleX = e.getSceneX() - mouseX;
double newCircleY = e.getSceneY() - mouseY;
circle.setCenterX(newCircleX);
circle.setCenterY(newCircleY);
numberText.setX(newCircleX - 5);
numberText.setY(newCircleY + 5);
for (int i = 0; i < selectedNode.getSizeVicini(); i++) {
selectedNode.getLine(i).setStartX(selectedCircle.getCenterX());
selectedNode.getLine(i).setStartY(selectedCircle.getCenterY());
selectedNode.getLine(i).setEndX(selectedNode.getVicino(i).getCenterX());
selectedNode.getLine(i).setEndY(selectedNode.getVicino(i).getCenterY());
}
});
}
@FXML
void buttonInstruction(ActionEvent event) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("How to use the app");
alert.setHeaderText(null);
String bullet = "\u2022";
String contentText = bullet
+ "To insert a node select a node and click 'Insert node'\n"
+ bullet + "You can move a node by double-clicking it and moving it with the mouse";
alert.setContentText(contentText);
String customStyle = "-fx-font-size: 16px;";
DialogPane dialogPane = alert.getDialogPane();
dialogPane.setStyle(customStyle);
alert.showAndWait();
}
@FXML
void buttonIndietro(ActionEvent event) throws IOException {
buttonReset(event);
parent = FXMLLoader.load(getClass()
.getResource("../resources/PannelloPrincipale.fxml"));
stage = (Stage) ((javafx.scene.Node) event.getSource()).getScene().getWindow();
scene = new Scene(parent, 900, 700);
stage.setScene(scene);
stage.setResizable(false);
stage.setTitle("Interfaccia principale");
stage.show();
}
@FXML
void buttonDFS(ActionEvent event) {
log.appendText("---------------------\nStarting DFS...\n");
dfs.executeDFS(tree.getRoot(), tree, slider.valueProperty().doubleValue(), log);
}
@FXML
void buttonBFS(ActionEvent event) {
log.appendText("---------------------\nStarting BFS...\n");
bfs.executeBFS(tree.getRoot(), tree, slider.valueProperty().doubleValue(), log);
}
@FXML
void buttonDeleteNode(ActionEvent event) {
removeNodeAndDescendants(selectedNode);
}
private void removeNodeAndDescendants(Node node) {
ArrayList<Node> children = node.getChildren();
pane.getChildren().remove(node.getCircle());
pane.getChildren().remove(node.getLine(0));
pane.getChildren().remove(node.getNumberText());
tree.deleteNode(node);
for (Node child : children) {
removeNodeAndDescendants(child);
}
Node padre = tree.getFather(selectedNode);
if (padre != null) {
padre.removeChildren(selectedNode);
}
pane.getChildren().remove(selectedNode.getLine(0));
pane.getChildren().remove(selectedNode.getCircle());
pane.getChildren().remove(node.getNumberText());
tree.deleteNode(selectedNode);
}
@FXML
Node buttonInsertNode(ActionEvent event) {
if (selectedNode==null) {
selectedNode = tree.getRoot();
selectedCircle = selectedNode.getCircle();
}
Circle node = new Circle(20, Color.BLUE);
Node children = new Node(++index, node);
int numChildren = selectedNode.get_nNode();
tree.addNode(children);
selectedNode.setFigli(children);
selectedNode.set_nNodes();
changePosition(numChildren);
Text numberText = new Text(Integer.toString(children.getIndexNode()));
numberText.setFill(Color.WHITE);
numberText.setX(node.getCenterX() - 5);
numberText.setY(node.getCenterY() + 5);
children.setNumberText(numberText);
pane.getChildren().addAll(node, numberText);
log.appendText("Node " + children.getIndexNode() + " added\n");
node.setOnMouseClicked(e -> {
selectedNode = children;
log.appendText("Index " + children.getIndexNode() + "\n");
node.setFill(Color.GREEN);
selectedCircle.setFill(Color.BLUE);
selectedCircle = node;
});
Line connectionLine = new Line(
selectedCircle.getCenterX(), selectedCircle.getCenterY(),
node.getCenterX(), node.getCenterY());
selectedNode.setLine(connectionLine);
children.setLine(connectionLine);
selectedNode.setVicino(children);
children.setVicino(selectedNode);
changePositionLine(children);
pane.getChildren().add(connectionLine);
connectionLine.toBack();
node.setOnMousePressed(e -> {
mouseX = e.getSceneX() - node.getCenterX();
mouseY = e.getSceneY() - node.getCenterY();
});
node.setOnMouseDragged(e -> {
double newCircleX = e.getSceneX() - mouseX;
double newCircleY = e.getSceneY() - mouseY;
node.setCenterX(newCircleX);
node.setCenterY(newCircleY);
numberText.setX(newCircleX - 5);
numberText.setY(newCircleY + 5);
for (int i = 0; i < selectedNode.getSizeVicini(); i++) {
selectedNode.getLine(i).setStartX(selectedCircle.getCenterX());
selectedNode.getLine(i).setStartY(selectedCircle.getCenterY());
selectedNode.getLine(i).setEndX(selectedNode.getVicino(i).getCenterX());
selectedNode.getLine(i).setEndY(selectedNode.getVicino(i).getCenterY());
}
});
return children;
}
private void changePositionLine(Node figlio) {
for (int i = 0; i < selectedNode.getSizeVicini(); i++) { // to move the children
selectedNode.getLine(i).setStartX(selectedCircle.getCenterX());
selectedNode.getLine(i).setStartY(selectedCircle.getCenterY());
selectedNode.getLine(i).setEndX(selectedNode.getVicino(i).getCenterX());
selectedNode.getLine(i).setEndY(selectedNode.getVicino(i).getCenterY());
}
ArrayList<Node> figli = selectedNode.getChildren();
for (Node node : figli) { // per spostare i number text
node.getNumberText().setX(node.getCircle().getCenterX() - 5);
node.getNumberText().setY(node.getCircle().getCenterY() + 5);
}
for (Node node : figli) { // to move the children's children
for (int i = 0; i < node.getSizeVicini(); i++) {
node.getLine(i).setStartX(node.getCircle().getCenterX());
node.getLine(i).setStartY(node.getCircle().getCenterY());
node.getLine(i).setEndX(node.getVicino(i).getCenterX());
node.getLine(i).setEndY(node.getVicino(i).getCenterY());
}
}
}
private void changePosition(int numFigli) {
int x;
ArrayList<Node> figli = selectedNode.getChildren();
if (numFigli == 0) {
x = 0;
} else {
x = 50 * numFigli;
}
for (Node node : figli) {
node.getCircle().setCenterX(selectedNode.getCircle().getCenterX() - x);
node.getCircle().setCenterY(selectedNode.getCircle().getCenterY() + 70);
x -= 100;
}
}
@FXML
void buttonReset(ActionEvent event) {
tree.deleteTree();
pane.getChildren().clear();
log.clear();
// reset the variable at the initial state
index = 0;
selectedCircle = null;
selectedNode = null;
tree = new Tree();
// start the scene
onStart();
}
@FXML
void buttonRandom(ActionEvent event) {
buttonReset(event);
Random random = new Random();
log.appendText("Generating a tree...\n");
int randomDim = random.nextInt(4) + 3;
selectedNode = tree.getRoot();
selectedCircle = selectedNode.getCircle();
for (int i = 0; i < randomDim; i++) {
int randomChildren = random.nextInt(3) + 1;
for (int j = 1; j <= randomChildren; j++) {
Node n = buttonInsertNode(event);
if (!tree.checkCoordianteNode(n)) {
// si deve cancellare
deleteNode(n);
}
}
Node node = tree.selectRandomNode();
selectedNode = node;
selectedCircle = selectedNode.getCircle();
}
}
private void deleteNode(Node n) {
pane.getChildren().removeAll(n.getCircle(), n.getNumberText());
for (int i = 0; i < n.getSizeVicini(); i++) {
pane.getChildren().remove(n.getLine(i));
}
tree.deleteNode(n);
if (n == selectedNode) {
selectedNode = null;
selectedCircle = null;
}
for (Node node : tree.getNodes()) {
System.out.println(node.getIndexNode());
}
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
onStart();
}
}