View Javadoc

1   /*
2    * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3    * California All rights reserved.
4    * 
5    * This software is open-source under the BSD license; see either "license.txt"
6    * or http://jung.sourceforge.net/license.txt for a description.
7    * 
8    */
9   package edu.uci.ics.jung.samples;
10  
11  import java.awt.BasicStroke;
12  import java.awt.BorderLayout;
13  import java.awt.Color;
14  import java.awt.Container;
15  import java.awt.Dimension;
16  import java.awt.GridLayout;
17  import java.awt.event.ActionEvent;
18  import java.awt.event.ActionListener;
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 org.apache.commons.collections15.Transformer;
28  import org.apache.commons.collections15.functors.ChainedTransformer;
29  import org.apache.commons.collections15.functors.ConstantTransformer;
30  
31  import edu.uci.ics.jung.algorithms.layout.FRLayout;
32  import edu.uci.ics.jung.algorithms.layout.Layout;
33  import edu.uci.ics.jung.graph.Graph;
34  import edu.uci.ics.jung.graph.util.TestGraphs;
35  import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
36  import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
37  import edu.uci.ics.jung.visualization.VisualizationModel;
38  import edu.uci.ics.jung.visualization.VisualizationViewer;
39  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
40  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
41  import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
42  import edu.uci.ics.jung.visualization.control.ScalingControl;
43  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
44  import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
45  import edu.uci.ics.jung.visualization.renderers.GradientVertexRenderer;
46  import edu.uci.ics.jung.visualization.renderers.VertexLabelAsShapeRenderer;
47  
48  
49  /**
50   * This demo shows how to use the vertex labels themselves as 
51   * the vertex shapes. Additionally, it shows html labels
52   * so they are multi-line, and gradient painting of the
53   * vertex labels.
54   * 
55   * @author Tom Nelson
56   * 
57   */
58  public class VertexLabelAsShapeDemo extends JApplet {
59  
60      /**
61  	 * 
62  	 */
63  	private static final long serialVersionUID = 1017336668368978842L;
64  
65      Graph<String,Number> graph;
66  
67      VisualizationViewer<String,Number> vv;
68      
69      Layout<String,Number> layout;
70      
71      /**
72       * create an instance of a simple graph with basic controls
73       */
74      public VertexLabelAsShapeDemo() {
75          
76          // create a simple graph for the demo
77          graph = TestGraphs.getOneComponentGraph();
78          
79          layout = new FRLayout<String,Number>(graph);
80  
81          Dimension preferredSize = new Dimension(400,400);
82          final VisualizationModel<String,Number> visualizationModel = 
83              new DefaultVisualizationModel<String,Number>(layout, preferredSize);
84          vv =  new VisualizationViewer<String,Number>(visualizationModel, preferredSize);
85          
86          // this class will provide both label drawing and vertex shapes
87          VertexLabelAsShapeRenderer<String,Number> vlasr = new VertexLabelAsShapeRenderer<String,Number>(vv.getRenderContext());
88          
89          // customize the render context
90          vv.getRenderContext().setVertexLabelTransformer(
91          		// this chains together Transformers so that the html tags
92          		// are prepended to the toString method output
93          		new ChainedTransformer<String,String>(new Transformer[]{
94          		new ToStringLabeller<String>(),
95          		new Transformer<String,String>() {
96  					public String transform(String input) {
97  						return "<html><center>Vertex<p>"+input;
98  					}}}));
99          vv.getRenderContext().setVertexShapeTransformer(vlasr);
100         vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.red));
101         vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.yellow));
102         vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2.5f)));
103         
104         // customize the renderer
105         vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<String,Number>(Color.gray, Color.white, true));
106         vv.getRenderer().setVertexLabelRenderer(vlasr);
107 
108         vv.setBackground(Color.black);
109         
110         // add a listener for ToolTips
111         vv.setVertexToolTipTransformer(new ToStringLabeller<String>());
112         
113         final DefaultModalGraphMouse<String,Number> graphMouse = 
114             new DefaultModalGraphMouse<String,Number>();
115 
116         vv.setGraphMouse(graphMouse);
117         vv.addKeyListener(graphMouse.getModeKeyListener());
118         
119         Container content = getContentPane();
120         GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);
121         content.add(gzsp);
122         
123         JComboBox modeBox = graphMouse.getModeComboBox();
124         modeBox.addItemListener(graphMouse.getModeListener());
125         graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);
126         
127         final ScalingControl scaler = new CrossoverScalingControl();
128 
129         JButton plus = new JButton("+");
130         plus.addActionListener(new ActionListener() {
131             public void actionPerformed(ActionEvent e) {
132                 scaler.scale(vv, 1.1f, vv.getCenter());
133             }
134         });
135         JButton minus = new JButton("-");
136         minus.addActionListener(new ActionListener() {
137             public void actionPerformed(ActionEvent e) {
138                 scaler.scale(vv, 1/1.1f, vv.getCenter());
139             }
140         });
141         
142         JPanel controls = new JPanel();
143         JPanel zoomControls = new JPanel(new GridLayout(2,1));
144         zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom"));
145         zoomControls.add(plus);
146         zoomControls.add(minus);
147         controls.add(zoomControls);
148         controls.add(modeBox);
149         content.add(controls, BorderLayout.SOUTH);
150     }
151     
152     /**
153      * a driver for this demo
154      */
155     public static void main(String[] args) {
156         JFrame f = new JFrame();
157         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
158         f.getContentPane().add(new VertexLabelAsShapeDemo());
159         f.pack();
160         f.setVisible(true);
161     }
162 }
163 
164