org.jungrapht.samples.tree.TreeLayoutDemoNoRoots 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!
/*
* Copyright (c) 2003, The JUNG Authors
* All rights reserved.
*
* This software is open-source under the BSD license; see either "license.txt"
* or https://github.com/tomnelson/jungrapht-visualization/blob/master/LICENSE for a description.
*
*/
package org.jungrapht.samples.tree;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.function.Function;
import javax.swing.*;
import org.jgrapht.Graph;
import org.jungrapht.samples.util.ControlHelpers;
import org.jungrapht.samples.util.DemoTreeSupplier;
import org.jungrapht.samples.util.TreeLayoutSelector;
import org.jungrapht.visualization.VisualizationScrollPane;
import org.jungrapht.visualization.VisualizationViewer;
import org.jungrapht.visualization.control.DefaultGraphMouse;
import org.jungrapht.visualization.decorators.EdgeShape;
import org.jungrapht.visualization.layout.algorithms.TreeLayoutAlgorithm;
import org.jungrapht.visualization.renderers.Renderer;
import org.jungrapht.visualization.util.AWT;
import org.jungrapht.visualization.util.LayoutPaintable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Demonstrates TreeLayout and RadialTreeLayout.
*
* @author Tom Nelson
*/
public class TreeLayoutDemoNoRoots extends JPanel {
private static final Logger log = LoggerFactory.getLogger(TreeLayoutDemoNoRoots.class);
Graph graph;
/** the visual component and renderer for the graph */
VisualizationViewer vv;
public TreeLayoutDemoNoRoots() {
setLayout(new BorderLayout());
// create a simple graph for the demo
graph = DemoTreeSupplier.createTreeTwo();
graph.addEdge("C3", "A0");
graph.addEdge("E0", "C3");
int width = 20;
int height = 20;
Function vertexShapeFunction =
v -> new Ellipse2D.Float(-width / 2.f, -height / 2.f, width, height);
final DefaultGraphMouse graphMouse = new DefaultGraphMouse<>();
vv =
VisualizationViewer.builder(graph)
.layoutAlgorithm(
TreeLayoutAlgorithm.builder()
.vertexBoundsFunction(
vertexShapeFunction.andThen(s -> AWT.convert(s.getBounds2D())))
.build())
.viewSize(new Dimension(600, 600))
.graphMouse(graphMouse)
.build();
vv.getRenderContext().setEdgeShapeFunction(EdgeShape.line());
vv.getRenderContext().setVertexLabelFunction(Object::toString);
vv.getRenderContext().setVertexLabelPosition(Renderer.VertexLabel.Position.CNTR);
vv.getRenderContext().setVertexLabelDrawPaintFunction(n -> Color.white);
vv.getRenderContext().setVertexShapeFunction(vertexShapeFunction);
// add a listener for ToolTips
vv.setVertexToolTipFunction(Object::toString);
vv.getRenderContext().setArrowFillPaintFunction(n -> Color.lightGray);
if (log.isTraceEnabled()) {
vv.addPreRenderPaintable(
new LayoutPaintable.LayoutBounds(
vv.getVisualizationModel(), vv.getRenderContext().getMultiLayerTransformer()));
}
final VisualizationScrollPane panel = new VisualizationScrollPane(vv);
add(panel);
// temporary feature to draw the layout bounds in the viewer
vv.addPreRenderPaintable(
new LayoutPaintable.LayoutBounds(
vv.getVisualizationModel(), vv.getRenderContext().getMultiLayerTransformer()));
JPanel layoutPanel = new JPanel(new GridLayout(0, 1));
layoutPanel.add(
TreeLayoutSelector.builder(vv)
.vertexShapeFunction(vertexShapeFunction)
.build());
JPanel controls = new JPanel();
controls.add(layoutPanel);
controls.add(ControlHelpers.getZoomControls(vv));
add(controls, BorderLayout.SOUTH);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Container content = frame.getContentPane();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel demo = new TreeLayoutDemoNoRoots();
content.add(demo);
frame.pack();
frame.setVisible(true);
log.trace("frame width {}", frame.getWidth());
log.trace("demo width {}", demo.getWidth());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy