View Javadoc
1   /*
2    * Copyright (c) 2003, The JUNG Authors
3    * All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or https://github.com/jrtom/jung/blob/master/LICENSE for a description.
7    * 
8    */
9   package edu.uci.ics.jung.samples;
10  
11  import java.awt.BorderLayout;
12  import java.awt.Color;
13  import java.awt.Container;
14  import java.awt.Dimension;
15  import java.awt.GridLayout;
16  import java.awt.event.ActionEvent;
17  import java.awt.event.ActionListener;
18  import java.net.URL;
19  
20  import javax.swing.BorderFactory;
21  import javax.swing.JApplet;
22  import javax.swing.JButton;
23  import javax.swing.JComboBox;
24  import javax.swing.JFrame;
25  import javax.swing.JPanel;
26  
27  import com.google.common.base.Function;
28  
29  import edu.uci.ics.jung.algorithms.layout.FRLayout;
30  import edu.uci.ics.jung.graph.DirectedGraph;
31  import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
32  import edu.uci.ics.jung.graph.util.EdgeType;
33  import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
34  import edu.uci.ics.jung.visualization.VisualizationViewer;
35  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
36  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
37  import edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode;
38  import edu.uci.ics.jung.visualization.control.ScalingControl;
39  import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
40  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
41  import edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer;
42  import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
43  
44  /**
45   * Demonstrates the use of images on graph edge labels.
46   * 
47   * @author Tom Nelson
48   * 
49   */
50  public class ImageEdgeLabelDemo extends JApplet {
51  
52      /**
53  	 * 
54  	 */
55  	private static final long serialVersionUID = -4332663871914930864L;
56  	
57  	private static final int VERTEX_COUNT=11;
58  
59  	/**
60       * the graph
61       */
62      DirectedGraph<Number, Number> graph;
63  
64      /**
65       * the visual component and renderer for the graph
66       */
67      VisualizationViewer<Number, Number> vv;
68      
69      public ImageEdgeLabelDemo() {
70          
71          // create a simple graph for the demo
72          graph = new DirectedSparseMultigraph<Number,Number>();
73          createGraph(VERTEX_COUNT);
74          
75          FRLayout<Number, Number> layout = new FRLayout<Number, Number>(graph);
76          layout.setMaxIterations(100);
77          vv =  new VisualizationViewer<Number, Number>(layout, new Dimension(400,400));
78          
79          vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer<Number>(vv.getPickedEdgeState(), Color.black, Color.cyan));
80  
81          vv.setBackground(Color.white);
82          
83          vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
84          vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));
85          vv.getRenderContext().setEdgeLabelTransformer(new Function<Number,String>() {
86          	URL url = getClass().getResource("/images/lightning-s.gif");
87  			public String apply(Number input) {
88  				return "<html><img src="+url+" height=10 width=21>";
89  			}});
90          
91          // add a listener for ToolTips
92          vv.setVertexToolTipTransformer(new ToStringLabeller());
93          vv.setEdgeToolTipTransformer(new ToStringLabeller());
94          Container content = getContentPane();
95          final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
96          content.add(panel);
97          
98          final DefaultModalGraphMouse<Number, Number> graphMouse = new DefaultModalGraphMouse<Number, Number>();
99          vv.setGraphMouse(graphMouse);
100         vv.addKeyListener(graphMouse.getModeKeyListener());
101         final ScalingControl scaler = new CrossoverScalingControl();
102 
103         JButton plus = new JButton("+");
104         plus.addActionListener(new ActionListener() {
105             public void actionPerformed(ActionEvent e) {
106                 scaler.scale(vv, 1.1f, vv.getCenter());
107             }
108         });
109         JButton minus = new JButton("-");
110         minus.addActionListener(new ActionListener() {
111             public void actionPerformed(ActionEvent e) {
112                 scaler.scale(vv, 1/1.1f, vv.getCenter());
113             }
114         });
115 
116         JComboBox<Mode> modeBox = graphMouse.getModeComboBox();
117         JPanel modePanel = new JPanel();
118         modePanel.setBorder(BorderFactory.createTitledBorder("Mouse Mode"));
119         modePanel.add(modeBox);
120         
121         JPanel scaleGrid = new JPanel(new GridLayout(1,0));
122         scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));
123         JPanel controls = new JPanel();
124         scaleGrid.add(plus);
125         scaleGrid.add(minus);
126         controls.add(scaleGrid);
127         controls.add(modePanel);
128         content.add(controls, BorderLayout.SOUTH);
129     }
130     
131     /**
132      * create some vertices
133      * @param count how many to create
134      * @return the Vertices in an array
135      */
136     private void createGraph(int vertexCount) {
137         for (int i = 0; i < vertexCount; i++) {
138             graph.addVertex(i);
139         }
140     	int j=0;
141         graph.addEdge(j++, 0, 1, EdgeType.DIRECTED);
142         graph.addEdge(j++, 3, 0, EdgeType.DIRECTED);
143         graph.addEdge(j++, 0, 4, EdgeType.DIRECTED);
144         graph.addEdge(j++, 4, 5, EdgeType.DIRECTED);
145         graph.addEdge(j++, 5, 3, EdgeType.DIRECTED);
146         graph.addEdge(j++, 2, 1, EdgeType.DIRECTED);
147         graph.addEdge(j++, 4, 1, EdgeType.DIRECTED);
148         graph.addEdge(j++, 8, 2, EdgeType.DIRECTED);
149         graph.addEdge(j++, 3, 8, EdgeType.DIRECTED);
150         graph.addEdge(j++, 6, 7, EdgeType.DIRECTED);
151         graph.addEdge(j++, 7, 5, EdgeType.DIRECTED);
152         graph.addEdge(j++, 0, 9, EdgeType.DIRECTED);
153         graph.addEdge(j++, 9, 8, EdgeType.DIRECTED);
154         graph.addEdge(j++, 7, 6, EdgeType.DIRECTED);
155         graph.addEdge(j++, 6, 5, EdgeType.DIRECTED);
156         graph.addEdge(j++, 4, 2, EdgeType.DIRECTED);
157         graph.addEdge(j++, 5, 4, EdgeType.DIRECTED);
158         graph.addEdge(j++, 4, 10, EdgeType.DIRECTED);
159         graph.addEdge(j++, 10, 4, EdgeType.DIRECTED);
160     }
161 
162     public static void main(String[] args) {
163         JFrame frame = new JFrame();
164         Container content = frame.getContentPane();
165         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
166 
167         content.add(new ImageEdgeLabelDemo());
168         frame.pack();
169         frame.setVisible(true);
170     }
171 }