org.jungrapht.samples.sugiyama.LayeredTestGraphExample Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jungrapht-visualization-samples Show documentation
Show all versions of jungrapht-visualization-samples Show documentation
Sample programs using Graph Visualization.
The newest version!
package org.jungrapht.samples.sugiyama;
import java.awt.*;
import java.util.stream.IntStream;
import javax.swing.*;
import org.jgrapht.Graph;
import org.jgrapht.graph.builder.GraphTypeBuilder;
import org.jgrapht.util.SupplierUtil;
import org.jungrapht.samples.sugiyama.test.algorithms.LayeredLayoutAlgorithm;
import org.jungrapht.visualization.VisualizationScrollPane;
import org.jungrapht.visualization.VisualizationViewer;
import org.jungrapht.visualization.decorators.EdgeShape;
import org.jungrapht.visualization.renderers.Renderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Builds a test graph from the BrandesKopf paper, layers the graph, then manually sets all the
* synthetic vertices and edges. Does not apply horizontal coordinate assignment (nor straighten
* edges)
*
* @author Tom Nelson
*/
public class LayeredTestGraphExample extends JFrame {
private static final Logger log = LoggerFactory.getLogger(LayeredTestGraphExample.class);
Graph graph;
VisualizationViewer vv;
public LayeredTestGraphExample() {
JPanel container = new JPanel(new BorderLayout());
graph = createInitialGraph();
vv = VisualizationViewer.builder(graph).viewSize(new Dimension(600, 600)).build();
vv.getRenderContext().setEdgeShapeFunction(EdgeShape.line());
vv.setVertexToolTipFunction(Object::toString);
vv.getRenderContext().setArrowFillPaintFunction(n -> Color.lightGray);
vv.getRenderContext().setVertexLabelPosition(Renderer.VertexLabel.Position.CNTR);
vv.getRenderContext().setVertexLabelDrawPaintFunction(c -> Color.white);
vv.getRenderContext().setVertexLabelFunction(Object::toString);
vv.getRenderContext().setVertexLabelPosition(Renderer.VertexLabel.Position.CNTR);
LayeredLayoutAlgorithm layoutAlgorithm =
LayeredLayoutAlgorithm.edgeAwareBuilder().build();
layoutAlgorithm.setVertexBoundsFunction(vv.getRenderContext().getVertexBoundsFunction());
vv.getVisualizationModel().setLayoutAlgorithm(layoutAlgorithm);
final VisualizationScrollPane panel = new VisualizationScrollPane(vv);
container.add(panel);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(container);
pack();
setVisible(true);
}
/**
* creates a graph to look like the one in the paper
*
* @return
*/
Graph createInitialGraph() {
Graph graph =
GraphTypeBuilder.directed()
.edgeSupplier(SupplierUtil.createIntegerSupplier())
.vertexSupplier(SupplierUtil.createIntegerSupplier())
.buildGraph();
IntStream.rangeClosed(1, 23).forEach(graph::addVertex);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(1, 13);
graph.addEdge(1, 21);
graph.addEdge(2, 3);
graph.addEdge(2, 20);
graph.addEdge(3, 4);
graph.addEdge(3, 5);
graph.addEdge(3, 23);
graph.addEdge(4, 6);
graph.addEdge(5, 7);
graph.addEdge(6, 8);
graph.addEdge(6, 16);
graph.addEdge(6, 23);
graph.addEdge(7, 9);
graph.addEdge(8, 10);
graph.addEdge(8, 11);
graph.addEdge(9, 12);
graph.addEdge(10, 13);
graph.addEdge(10, 14);
graph.addEdge(10, 15);
graph.addEdge(11, 15);
graph.addEdge(11, 16);
graph.addEdge(12, 20);
graph.addEdge(13, 17);
graph.addEdge(14, 17);
graph.addEdge(14, 18);
// no 15 targets
graph.addEdge(16, 18);
graph.addEdge(16, 19);
graph.addEdge(16, 20);
graph.addEdge(18, 21);
graph.addEdge(19, 22);
graph.addEdge(21, 23);
graph.addEdge(22, 23);
return graph;
}
public static void main(String[] args) {
new LayeredTestGraphExample();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy