
edu.uci.ics.jung.samples.UnicodeLabelDemo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jung-samples Show documentation
Show all versions of jung-samples Show documentation
Sample programs using JUNG. Nearly all JUNG capabilities are demonstrated here.
Please study the source code for these examples prior to asking how to do something.
The newest version!
/*
* 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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
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.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ScalingControl;
import edu.uci.ics.jung.visualization.decorators.EllipseVertexShapeTransformer;
import edu.uci.ics.jung.visualization.decorators.PickableEdgePaintTransformer;
import edu.uci.ics.jung.visualization.decorators.PickableVertexPaintTransformer;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.decorators.VertexIconShapeTransformer;
import edu.uci.ics.jung.visualization.renderers.DefaultEdgeLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.DefaultVertexLabelRenderer;
/**
* A demo that shows flag images as vertices, and uses unicode
* to render vertex labels.
*
* @author Tom Nelson
*
*/
public class UnicodeLabelDemo {
/**
* the graph
*/
Graph graph;
/**
* the visual component and renderer for the graph
*/
VisualizationViewer vv;
boolean showLabels;
public UnicodeLabelDemo() {
// create a simple graph for the demo
graph = new DirectedSparseGraph();
Integer[] v = createVertices(10);
createEdges(v);
Map iconMap = new HashMap();
vv = new VisualizationViewer(new FRLayout(graph));
vv.getRenderContext().setVertexLabelTransformer(new UnicodeVertexStringer(v));
vv.getRenderContext().setVertexLabelRenderer(new DefaultVertexLabelRenderer(Color.cyan));
vv.getRenderContext().setEdgeLabelRenderer(new DefaultEdgeLabelRenderer(Color.cyan));
VertexIconShapeTransformer vertexIconShapeFunction =
new VertexIconShapeTransformer(new EllipseVertexShapeTransformer());
Function vertexIconFunction = Functions.forMap(iconMap);
vv.getRenderContext().setVertexShapeTransformer(vertexIconShapeFunction);
vv.getRenderContext().setVertexIconTransformer(vertexIconFunction);
loadImages(v, iconMap);
vertexIconShapeFunction.setIconMap(iconMap);
vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer(vv.getPickedVertexState(), Color.white, Color.yellow));
vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer(vv.getPickedEdgeState(), Color.black, Color.lightGray));
vv.setBackground(Color.white);
// add my listener for ToolTips
vv.setVertexToolTipTransformer(new ToStringLabeller());
// 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 DefaultModalGraphMouse gm
= new DefaultModalGraphMouse();
vv.setGraphMouse(gm);
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());
}
});
JCheckBox lo = new JCheckBox("Show Labels");
lo.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e) {
showLabels = e.getStateChange() == ItemEvent.SELECTED;
vv.repaint();
}
});
lo.setSelected(true);
JPanel controls = new JPanel();
controls.add(plus);
controls.add(minus);
controls.add(lo);
controls.add(gm.getModeComboBox());
content.add(controls, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
class UnicodeVertexStringer implements Function {
Map map = new HashMap();
Map iconMap = new HashMap();
String[] labels = {
"\u0057\u0065\u006C\u0063\u006F\u006D\u0065\u0020\u0074\u006F\u0020JUNG\u0021",
"\u6B22\u8FCE\u4F7F\u7528\u0020\u0020JUNG\u0021",
"\u0414\u043E\u0431\u0440\u043E\u0020\u043F\u043E\u0436\u0430\u043B\u043E\u0432\u0430\u0422\u044A\u0020\u0432\u0020JUNG\u0021",
"\u0042\u0069\u0065\u006E\u0076\u0065\u006E\u0075\u0065\u0020\u0061\u0075\u0020JUNG\u0021",
"\u0057\u0069\u006C\u006B\u006F\u006D\u006D\u0065\u006E\u0020\u007A\u0075\u0020JUNG\u0021",
"JUNG\u3078\u3087\u3045\u3053\u305D\u0021",
// "\u0053\u00E9\u006A\u0061\u0020\u0042\u0065\u006D\u0076\u0069\u006E\u0064\u006F\u0020JUNG\u0021",
"\u0042\u0069\u0065\u006E\u0076\u0065\u006E\u0069\u0064\u0061\u0020\u0061\u0020JUNG\u0021"
};
public UnicodeVertexStringer(V[] vertices) {
for(int i=0; i imageMap) {
ImageIcon[] icons = null;
try {
icons = new ImageIcon[] {
new ImageIcon(getClass().getResource("/images/united-states.gif")),
new ImageIcon(getClass().getResource("/images/china.gif")),
new ImageIcon(getClass().getResource("/images/russia.gif")),
new ImageIcon(getClass().getResource("/images/france.gif")),
new ImageIcon(getClass().getResource("/images/germany.gif")),
new ImageIcon(getClass().getResource("/images/japan.gif")),
new ImageIcon(getClass().getResource("/images/spain.gif"))
};
} catch(Exception ex) {
System.err.println("You need flags.jar in your classpath to see the flag icons.");
}
for(int i=0; icons != null && i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy