
edu.uci.ics.jung.samples.GraphZoomScrollPaneDemo Maven / Gradle / Ivy
Show all versions of jung-samples Show documentation
/*
* 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/jrtom/jung/blob/master/LICENSE for a description.
*
*/
package edu.uci.ics.jung.samples;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Paint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import edu.uci.ics.jung.algorithms.layout.KKLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.Layer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.AbstractModalGraphMouse;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.GraphMouseListener;
import edu.uci.ics.jung.visualization.control.ScalingControl;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.GradientVertexRenderer;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import edu.uci.ics.jung.visualization.renderers.BasicVertexLabelRenderer.InsidePositioner;
/**
* Demonstrates the use of GraphZoomScrollPane
.
* This class shows the VisualizationViewer
zooming
* and panning capabilities, using horizontal and
* vertical scrollbars.
*
* This demo also shows ToolTips on graph vertices and edges,
* and a key listener to change graph mouse modes.
*
* @author Tom Nelson
*
*/
public class GraphZoomScrollPaneDemo {
/**
* the graph
*/
DirectedSparseGraph graph;
/**
* the visual component and renderer for the graph
*/
VisualizationViewer vv;
/**
* create an instance of a simple graph with controls to
* demo the zoom features.
*
*/
public GraphZoomScrollPaneDemo() {
// create a simple graph for the demo
graph = new DirectedSparseGraph();
String[] v = createVertices(10);
createEdges(v);
ImageIcon sandstoneIcon = null;
String imageLocation = "/images/Sandstone.jpg";
try {
sandstoneIcon =
new ImageIcon(getClass().getResource(imageLocation));
} catch(Exception ex) {
System.err.println("Can't load \""+imageLocation+"\"");
}
final ImageIcon icon = sandstoneIcon;
vv = new VisualizationViewer(new KKLayout(graph));
if(icon != null) {
vv.addPreRenderPaintable(new VisualizationViewer.Paintable(){
public void paint(Graphics g) {
Dimension d = vv.getSize();
g.drawImage(icon.getImage(),0,0,d.width,d.height,vv);
}
public boolean useTransform() { return false; }
});
}
vv.addPostRenderPaintable(new VisualizationViewer.Paintable(){
int x;
int y;
Font font;
FontMetrics metrics;
int swidth;
int sheight;
String str = "GraphZoomScrollPane Demo";
public void paint(Graphics g) {
Dimension d = vv.getSize();
if(font == null) {
font = new Font(g.getFont().getName(), Font.BOLD, 30);
metrics = g.getFontMetrics(font);
swidth = metrics.stringWidth(str);
sheight = metrics.getMaxAscent()+metrics.getMaxDescent();
x = (d.width-swidth)/2;
y = (int)(d.height-sheight*1.5);
}
g.setFont(font);
Color oldColor = g.getColor();
g.setColor(Color.lightGray);
g.drawString(str, x, y);
g.setColor(oldColor);
}
public boolean useTransform() {
return false;
}
});
vv.addGraphMouseListener(new TestGraphMouseListener());
vv.getRenderer().setVertexRenderer(
new GradientVertexRenderer(
Color.white, Color.red,
Color.white, Color.blue,
vv.getPickedVertexState(),
false));
vv.getRenderContext().setEdgeDrawPaintTransformer(Functions.constant(Color.lightGray));
vv.getRenderContext().setArrowFillPaintTransformer(Functions.constant(Color.lightGray));
vv.getRenderContext().setArrowDrawPaintTransformer(Functions.constant(Color.lightGray));
// add my listeners for ToolTips
vv.setVertexToolTipTransformer(new ToStringLabeller());
vv.setEdgeToolTipTransformer(new Function() {
public String apply(Number edge) {
return "E"+graph.getEndpoints(edge).toString();
}});
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);
vv.setForeground(Color.lightGray);
// create a frome to hold the graph
final JFrame frame = new JFrame();
Container content = frame.getContentPane();
final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
content.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse();
vv.setGraphMouse(graphMouse);
vv.addKeyListener(graphMouse.getModeKeyListener());
vv.setToolTipText("Type 'p' for Pick modeType 't' for Transform mode");
final ScalingControl scaler = new CrossoverScalingControl();
JButton plus = new JButton("+");
plus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1.1f, vv.getCenter());
}
});
JButton minus = new JButton("-");
minus.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
scaler.scale(vv, 1/1.1f, vv.getCenter());
}
});
JButton reset = new JButton("reset");
reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setToIdentity();
vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).setToIdentity();
}});
JPanel controls = new JPanel();
controls.add(plus);
controls.add(minus);
controls.add(reset);
content.add(controls, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
/**
* create some vertices
* @param count how many to create
* @return the Vertices in an array
*/
private String[] createVertices(int count) {
String[] v = new String[count];
for (int i = 0; i < count; i++) {
v[i] = "V"+i;
graph.addVertex(v[i]);
}
return v;
}
/**
* create edges for this demo graph
* @param v an array of Vertices to connect
*/
void createEdges(String[] v) {
graph.addEdge(new Double(Math.random()), v[0], v[1], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[0], v[3], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[0], v[4], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[4], v[5], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[3], v[5], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[1], v[2], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[1], v[4], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[8], v[2], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[3], v[8], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[6], v[7], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[7], v[5], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[0], v[9], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[9], v[8], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[7], v[6], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[6], v[5], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[4], v[2], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[5], v[4], EdgeType.DIRECTED);
}
/**
* A nested class to demo the GraphMouseListener finding the
* right vertices after zoom/pan
*/
static class TestGraphMouseListener implements GraphMouseListener {
public void graphClicked(V v, MouseEvent me) {
System.err.println("Vertex "+v+" was clicked at ("+me.getX()+","+me.getY()+")");
}
public void graphPressed(V v, MouseEvent me) {
System.err.println("Vertex "+v+" was pressed at ("+me.getX()+","+me.getY()+")");
}
public void graphReleased(V v, MouseEvent me) {
System.err.println("Vertex "+v+" was released at ("+me.getX()+","+me.getY()+")");
}
}
public static void main(String[] args)
{
new GraphZoomScrollPaneDemo();
}
}