1 import javafx.scene.Group;
2 import javafx.scene.layout.BorderPane;
3 import javafx.scene.shape.Circle;
4 import javafx.scene.shape.Line;
5 import javafx.scene.text.Text;
6
7 public class GraphView extends BorderPane {
8 private Graph<? extends Displayable> graph;
9 private Group group = new Group();
10
11 public GraphView(Graph<? extends Displayable> graph) {
12 this.graph = graph;
13 this.setCenter(group);
14 repaintGraph();
15 }
16
17 private void repaintGraph() {
18 group.getChildren().clear();
19
20
21 java.util.List<? extends Displayable> vertices
22 = graph.getVertices();
23 for (int i = 0; i < graph.getSize(); i++) {
24 double x = vertices.get(i).getX();
25 double y = vertices.get(i).getY();
26 String name = vertices.get(i).getName();
27
28 group.getChildren().add(new Circle(x, y, 16));
29 group.getChildren().add(new Text(x - 8, y - 18, name));
30 }
31
32
33 for (int i = 0; i < graph.getSize(); i++) {
34 java.util.List<Integer> neighbors = graph.getNeighbors(i);
35 double x1 = graph.getVertex(i).getX();
36 double y1 = graph.getVertex(i).getY();
37 for (int v: neighbors) {
38 double x2 = graph.getVertex(v).getX();
39 double y2 = graph.getVertex(v).getY();
40
41
42 group.getChildren().add(new Line(x1, y1, x2, y2));
43 }
44 }
45 }
46 }