-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawGraph.java
40 lines (31 loc) · 1.19 KB
/
DrawGraph.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
import java.awt.*;
import java.util.function.Function;
import java.util.stream.IntStream;
import javax.swing.*;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.DefaultGraphType;
import org.jgrapht.graph.builder.GraphTypeBuilder;
import org.jgrapht.util.SupplierUtil;
import org.jungrapht.visualization.VisualizationViewer;
import org.jungrapht.visualization.layout.algorithms.KKLayoutAlgorithm;
public class DrawGraph {
private Graph<Integer, DefaultEdge> g;
public DrawGraph(Graph<Integer, DefaultEdge> g, Rational r) {
VisualizationViewer<Integer, DefaultEdge> vv =
VisualizationViewer.builder(g)
.viewSize(new Dimension(700, 700))
.layoutAlgorithm(new KKLayoutAlgorithm<>())
.build();
vv.getRenderContext().setVertexLabelFunction(v -> v.toString());
// create a frame to hold the graph visualization
final JFrame frame = new JFrame(r.toString());
frame.getContentPane().add(vv.getComponent());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void setGraph(Graph<Integer, DefaultEdge> graph) {
this.g = graph;
}
}