All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.uci.ics.jung.samples.AnnotationsDemo Maven / Gradle / Ivy

Go to download

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.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.TestGraphs;
import edu.uci.ics.jung.visualization.DefaultVisualizationModel;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationModel;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.VisualizationServer.Paintable;
import edu.uci.ics.jung.visualization.annotations.AnnotatingGraphMousePlugin;
import edu.uci.ics.jung.visualization.annotations.AnnotatingModalGraphMouse;
import edu.uci.ics.jung.visualization.annotations.AnnotationControls;
import edu.uci.ics.jung.visualization.control.CrossoverScalingControl;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse.Mode;
import edu.uci.ics.jung.visualization.control.ScalingControl;
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.renderers.Renderer;

/**
 * Demonstrates annotation of graph elements.
 * 
 * @author Tom Nelson
 * 
 */
@SuppressWarnings("serial")
public class AnnotationsDemo extends JApplet {

    static final String instructions = 
        ""+
        "

Instructions for Annotations

"+ "

The Annotation Controls allow you to select:"+ "

    "+ "
  • Shape"+ "
  • Color"+ "
  • Fill (or outline)"+ "
  • Above or below (UPPER/LOWER) the graph display"+ "
"+ "

Mouse Button one press starts a Shape,"+ "

drag and release to complete."+ "

Mouse Button three pops up an input dialog"+ "

for text. This will create a text annotation."+ "

You may use html for multi-line, etc."+ "

You may even use an image tag and image url"+ "

to put an image in the annotation."+ "

"+ "

To remove an annotation, shift-click on it"+ "

in the Annotations mode."+ "

If there is overlap, the Annotation with center"+ "

closest to the mouse point will be removed."; JDialog helpDialog; Paintable viewGrid; /** * create an instance of a simple graph in two views with controls to * demo the features. * */ public AnnotationsDemo() { // create a simple graph for the demo Graph graph = TestGraphs.getOneComponentGraph(); // the preferred sizes for the two views Dimension preferredSize1 = new Dimension(600,600); // create one layout for the graph FRLayout layout = new FRLayout(graph); layout.setMaxIterations(500); VisualizationModel vm = new DefaultVisualizationModel(layout, preferredSize1); // create 2 views that share the same model final VisualizationViewer vv = new VisualizationViewer(vm, preferredSize1); vv.setBackground(Color.white); vv.getRenderContext().setEdgeDrawPaintTransformer(new PickableEdgePaintTransformer(vv.getPickedEdgeState(), Color.black, Color.cyan)); vv.getRenderContext().setVertexFillPaintTransformer(new PickableVertexPaintTransformer(vv.getPickedVertexState(), Color.red, Color.yellow)); vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller()); vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR); // add default listener for ToolTips vv.setVertexToolTipTransformer(new ToStringLabeller()); Container content = getContentPane(); Container panel = new JPanel(new BorderLayout()); GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv); panel.add(gzsp); helpDialog = new JDialog(); helpDialog.getContentPane().add(new JLabel(instructions)); RenderContext rc = vv.getRenderContext(); AnnotatingGraphMousePlugin annotatingPlugin = new AnnotatingGraphMousePlugin(rc); // create a GraphMouse for the main view // final AnnotatingModalGraphMouse graphMouse = new AnnotatingModalGraphMouse(rc, annotatingPlugin); vv.setGraphMouse(graphMouse); vv.addKeyListener(graphMouse.getModeKeyListener()); 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()); } }); JComboBox modeBox = graphMouse.getModeComboBox(); modeBox.setSelectedItem(ModalGraphMouse.Mode.ANNOTATING); JButton help = new JButton("Help"); help.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { helpDialog.pack(); helpDialog.setVisible(true); } }); JPanel controls = new JPanel(); JPanel zoomControls = new JPanel(); zoomControls.setBorder(BorderFactory.createTitledBorder("Zoom")); zoomControls.add(plus); zoomControls.add(minus); controls.add(zoomControls); JPanel modeControls = new JPanel(); modeControls.setBorder(BorderFactory.createTitledBorder("Mouse Mode")); modeControls.add(graphMouse.getModeComboBox()); controls.add(modeControls); JPanel annotationControlPanel = new JPanel(); annotationControlPanel.setBorder(BorderFactory.createTitledBorder("Annotation Controls")); AnnotationControls annotationControls = new AnnotationControls(annotatingPlugin); annotationControlPanel.add(annotationControls.getAnnotationsToolBar()); controls.add(annotationControlPanel); JPanel helpControls = new JPanel(); helpControls.setBorder(BorderFactory.createTitledBorder("Help")); helpControls.add(help); controls.add(helpControls); content.add(panel); content.add(controls, BorderLayout.SOUTH); } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new AnnotationsDemo()); f.pack(); f.setVisible(true); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy