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.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.Paint;
18  import java.awt.Stroke;
19  import java.awt.event.ActionEvent;
20  import java.awt.event.ActionListener;
21  
22  import javax.swing.BorderFactory;
23  import javax.swing.JApplet;
24  import javax.swing.JButton;
25  import javax.swing.JComboBox;
26  import javax.swing.JFrame;
27  import javax.swing.JPanel;
28  
29  import com.google.common.base.Function;
30  import com.google.common.base.Functions;
31  
32  import edu.uci.ics.jung.algorithms.layout.FRLayout;
33  import edu.uci.ics.jung.algorithms.layout.Layout;
34  import edu.uci.ics.jung.graph.Graph;
35  import edu.uci.ics.jung.graph.util.TestGraphs;
36  import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
37  import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
38  import edu.uci.ics.jung.visualization.VisualizationModel;
39  import edu.uci.ics.jung.visualization.VisualizationViewer;
40  import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
41  import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
42  import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
43  import edu.uci.ics.jung.visualization.control.ScalingControl;
44  import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
45  import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
46  import edu.uci.ics.jung.visualization.renderers.GradientVertexRenderer;
47  import edu.uci.ics.jung.visualization.renderers.VertexLabelAsShapeRenderer;
48  
49  
50  /**
51   * This demo shows how to use the vertex labels themselves as 
52   * the vertex shapes. Additionally, it shows html labels
53   * so they are multi-line, and gradient painting of the
54   * vertex labels.
55   * 
56   * @author Tom Nelson
57   * 
58   */
59  public class VertexLabelAsShapeDemo extends JApplet {
60  
61      /**
62  	 * 
63  	 */
64  	private static final long serialVersionUID = 1017336668368978842L;
65  
66      Graph<String,Number> graph;
67  
68      VisualizationViewer<String,Number> vv;
69      
70      Layout<String,Number> layout;
71      
72      /**
73       * create an instance of a simple graph with basic controls
74       */
75      public VertexLabelAsShapeDemo() {
76          
77          // create a simple graph for the demo
78          graph = TestGraphs.getOneComponentGraph();
79          
80          layout = new FRLayout<String,Number>(graph);
81  
82          Dimension preferredSize = new Dimension(400,400);
83          final VisualizationModel<String,Number> visualizationModel = 
84              new DefaultVisualizationModel<String,Number>(layout, preferredSize);
85          vv =  new VisualizationViewer<String,Number>(visualizationModel, preferredSize);
86          
87          // this class will provide both label drawing and vertex shapes
88          VertexLabelAsShapeRenderer<String,Number> vlasr = new VertexLabelAsShapeRenderer<String,Number>(vv.getRenderContext());
89          
90          // customize the render context
91          vv.getRenderContext().setVertexLabelTransformer(
92          		// this chains together Functions so that the html tags
93          		// are prepended to the toString method output
94          		Functions.<Object,String,String>compose(
95          				new Function<String,String>(){
96  							public String apply(String input) {
97  								return "<html><center>Vertex<p>"+input;
98  							}}, new ToStringLabeller()));
99          vv.getRenderContext().setVertexShapeTransformer(vlasr);
100         vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.red));
101         vv.getRenderContext().setEdgeDrawPaintTransformer(Functions.<Paint>constant(Color.yellow));
102         vv.getRenderContext().setEdgeStrokeTransformer(Functions.<Stroke>constant(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());
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     public static void main(String[] args) {
153         JFrame f = new JFrame();
154         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
155         f.getContentPane().add(new VertexLabelAsShapeDemo());
156         f.pack();
157         f.setVisible(true);
158     }
159 }
160 
161