Java tutorial
/* * Copyright (c) 2003, the JUNG Project and the Regents of the University of * California All rights reserved. * * This software is open-source under the BSD license; see either "license.txt" * or http://jung.sourceforge.net/license.txt 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.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 org.apache.commons.collections15.Transformer; import org.apache.commons.collections15.functors.ConstantTransformer; 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 <code>GraphZoomScrollPane</code>. * This class shows the <code>VisualizationViewer</code> zooming * and panning capabilities, using horizontal and * vertical scrollbars. * * <p>This demo also shows ToolTips on graph vertices and edges, * and a key listener to change graph mouse modes.</p> * * @author Tom Nelson * */ public class GraphZoomScrollPaneDemo { /** * the graph */ DirectedSparseGraph<String, Number> graph; /** * the visual component and renderer for the graph */ VisualizationViewer<String, Number> vv; /** * create an instance of a simple graph with controls to * demo the zoom features. * */ @SuppressWarnings({ "unchecked", "rawtypes" }) public GraphZoomScrollPaneDemo() { // create a simple graph for the demo graph = new DirectedSparseGraph<String, Number>(); 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<String, Number>(new KKLayout<String, Number>(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<String>()); vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<String, Number>(Color.white, Color.red, Color.white, Color.blue, vv.getPickedVertexState(), false)); vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.lightGray)); vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray)); vv.getRenderContext().setArrowDrawPaintTransformer(new ConstantTransformer(Color.lightGray)); // add my listeners for ToolTips vv.setVertexToolTipTransformer(new ToStringLabeller<String>()); vv.setEdgeToolTipTransformer(new Transformer<Number, String>() { public String transform(Number edge) { return "E" + graph.getEndpoints(edge).toString(); } }); vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>()); 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<String, Number>(); vv.setGraphMouse(graphMouse); vv.addKeyListener(graphMouse.getModeKeyListener()); vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type '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<V> implements GraphMouseListener<V> { 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() + ")"); } } /** * a driver for this demo */ public static void main(String[] args) { new GraphZoomScrollPaneDemo(); } }