JUNG (Java Universal Network/Graph Framework)

repository·master·Indexed 19 days ago

https://github.com/jrtom/jung

A modular Java software library for modeling, analyzing, and visualizing graph and network data. It includes components for core API definitions (jung-api), graph implementations (jung-graph-impl), analysis algorithms (jung-algorithms), I/O capabilities (jung-io), and visualization tools (jung-visualization). Key features include the MutableCTreeNetwork for directed tree structures and various visualization plugins such as AnnotatingGraphMousePlugin, PickingGraphMousePlugin, and GraphZoomScrollPane.

Tokens
3.4K
Snippets
9
Records
13
Agent score
65%

What's inside JUNG

  1. Install JUNG snapshots using Gradle

    master

    To use the latest snapshot from the master branch in a Gradle project, add the JitPack repository and the dependency using the com.github.jrtom:jung:master-SNAPSHOT coordinate.

    dependencies {
       // ...
    	compile("com.github.jrtom:jung:master-SNAPSHOT")
    }
    
    repositories {
        // ...
        maven { url "https://jitpack.io" }
    }
  2. Install JUNG using Maven

    master

    To use JUNG in a Maven project, add the specific subpackage dependencies you require. JUNG is modularized into several components. For version 2.1.1, use the net.sf.jung groupId and the corresponding jung-[subpackage] artifactId.

    <dependency>
      <groupId>net.sf.jung</groupId>
      <artifactId>jung-[subpackage]</artifactId>
      <version>2.1.1</version>
    </dependency>
  3. Use MutableCTreeNetwork for tree-structured networks

    master

    The MutableCTreeNetwork interface (implemented by DelegateCTreeNetwork) provides a specialized network structure that enforces C-Tree (directed tree) properties.

    Key characteristics of a C-Tree network in JUNG:

    • Directed: It is a directed graph.
    • Single Root: It has at most one root node.
    • No Self-Loops: Self-loops are not allowed.
    • No Parallel Edges: Parallel edges are not allowed.
    • Tree Structure: Every node (except the root) must have exactly one predecessor. Adding an edge to a node that is already in the tree will result in an error.
    • Subtree Removal: Removing a node or an edge automatically removes the entire subtree rooted at that node/edge's target.
  4. Use GraphZoomScrollPane for zooming and panning graphs

    master

    The GraphZoomScrollPane is a Swing JPanel container designed to wrap a VisualizationViewer. It provides custom horizontal and vertical scrollbars that automatically synchronize with the graph's scale and translation.

    When a user zooms in or out using the VisualizationViewer, the GraphZoomScrollPane updates the scrollbar ranges and sizes. Conversely, adjusting the scrollbars allows the user to pan across the graph by modifying the translation component of the VisualizationViewer's layout transformer.

    To use it, instantiate the component by passing an existing VisualizationViewer to its constructor.

    // Assuming 'vv' is an existing VisualizationViewer instance
    GraphZoomScrollPane scrollPane = new GraphZoomScrollPane(vv);
    // Add 'scrollPane' to your UI container
  5. Use AnnotatingGraphMousePlugin to add annotations via mouse

    master

    The AnnotatingGraphMousePlugin allows users to interactively add text and shape annotations to a graph visualization using mouse gestures. It integrates with a VisualizationViewer to handle rendering and input.

    Interaction Patterns

    • Text Annotations: Triggered by a popup event (e.g., right-click) which prompts the user for a string via a dialog.
    • Shape Annotations: Created by clicking and dragging (using the primary selection modifier). The plugin draws a transient rectangle while dragging and converts it into a permanent Shape annotation upon release.
    • Removing Annotations: Triggered by using the additionalModifiers (e.g., BUTTON1_MASK | SHIFT_MASK) while clicking on an existing annotation.

    Configuration

    • Annotation Color: Set via setAnnotationColor(Color).
    • Layering: Annotations can be placed in different layers using setLayer(Annotation.Layer). Available layers are defined in Annotation.Layer (e.g., LOWER).
    • Fill: Toggle whether shape annotations are filled using setFill(boolean).
    // Example initialization with a RenderContext
    AnnotatingGraphMousePlugin<N, E> plugin = new AnnotatingGraphMousePlugin<>(renderContext);
    
    // Customize appearance
    plugin.setAnnotationColor(Color.RED);
    plugin.setFill(true);
    plugin.setLayer(Annotation.Layer.LOWER);
    
    // Add the plugin to your VisualizationViewer
    viewer.addMouseListener(plugin);
    viewer.addMouseMotionListener(plugin);
  6. Identify JUNG subpackages

    master

    JUNG is distributed as several distinct modules. Depending on your needs (modeling, algorithms, visualization, or I/O), you should include the relevant artifact:

    • jung-api: Core API definitions
    • jung-graph-impl: Graph implementations
    • jung-algorithms: Graph analysis algorithms
    • jung-io: Input/Output capabilities
    • jung-visualization: Graph visualization tools
    • jung-samples: Sample data and use cases
  7. Access scrollbars and corner components in GraphZoomScrollPane

    master

    The GraphZoomScrollPane provides methods to access its internal UI components for further customization:

    • getHorizontalScrollBar(): Returns the JScrollBar used for horizontal panning.
    • getVerticalScrollBar(): Returns the JScrollBar used for vertical panning.
    • getCorner(): Returns the JComponent located at the intersection of the horizontal and vertical scrollbars (the lower-right corner).
    • setCorner(JComponent corner): Allows you to replace the default corner component with a custom one (e.g., a button or menu). The component's preferred size will be automatically set to match the scrollbar widths.
    GraphZoomScrollPane scrollPane = new GraphZoomScrollPane(vv);
    
    // Access scrollbars
    JScrollBar hBar = scrollPane.getHorizontalScrollBar();
    JScrollBar vBar = scrollPane.getVerticalScrollBar();
    
    // Set a custom corner component
    JButton customButton = new JButton("!");
    scrollPane.setCorner(customButton);
  8. Manage C-Tree hierarchy with MutableCTreeNetwork

    master

    When working with a MutableCTreeNetwork<N, E>, you can navigate and manipulate the tree hierarchy using the following methods:

    Hierarchy Navigation

    • root(): Returns an Optional<N> containing the root node of the tree.
    • predecessor(N node): Returns an Optional<N> containing the parent of the specified node. Since it is a tree, a node has at most one predecessor.
    • inEdge(N node): Returns an Optional<E> containing the edge connecting the node to its parent.
    • depth(N node): Returns the integer depth of the node (distance from the root).
    • height(): Returns an Optional<Integer> representing the maximum depth of the tree.

    Structural Modification

    • addNode(N node): Adds a node. If no root exists, the first node added becomes the root.
    • addEdge(N nodeU, N nodeV, E edge): Adds a directed edge from nodeU to nodeV. nodeV must not already exist in the tree (unless it is the root being established).
    • removeNode(N node): Removes the specified node and all its descendants (the entire subtree).
    • removeEdge(E edge): Removes the specified edge and the entire subtree rooted at the edge's target node.
  9. Configure AnnotatingGraphMousePlugin selection modifiers

    master

    When instantiating AnnotatingGraphMousePlugin, you can define custom mouse button masks for primary and additional actions.

    • Primary Selection: Used for creating shape annotations (click and drag).
    • Additional Selection: Used for removing existing annotations.

    By default, the plugin uses BUTTON1_MASK for primary selection and BUTTON1_MASK | SHIFT_MASK for additional selection.

    // Customizing modifiers: 
    // Primary: Button 1
    // Additional: Button 1 + Control
    AnnotatingGraphMousePlugin<N, E> plugin = new AnnotatingGraphMousePlugin<>( 
        rc, 
        InputEvent.BUTTON1_MASK, 
        InputEvent.BUTTON1_MASK | InputEvent.CTRL_DOWN_MASK
    );
  10. Render edges as cubic curves using CubicCurveEdgeEffects

    master

    The CubicCurveEdgeEffects class is an implementation of EdgeEffects used to provide visual feedback when creating or interacting with edges in a JUNG visualization. It renders edges as cubic curves rather than straight lines.

    When an edge is being created (e.g., during a mouse drag), this class manages the rendering of the curved edge shape and the arrow head (for directed edges) by adding Paintable objects to the BasicVisualizationServer.

    // Example usage context: providing edge effects to a visualization server
    // Note: This is typically used within the framework's control/interaction logic
    EdgeEffects<N, E> effects = new CubicCurveEdgeEffects<N, E>();
    // The effects are then utilized by the BasicVisualizationServer during edge creation events
  11. Use PickingGraphMousePlugin for graph element selection

    master

    The PickingGraphMousePlugin is a control plugin for VisualizationViewer that enables users to select nodes and edges using mouse input.

    Default Behavior:

    • Single Selection: Clicking a node or edge with the primary mouse button (default BUTTON1_MASK) selects that element. If no element is clicked, it clears existing selections and prepares for a multi-selection rectangle.
    • Additive Selection: Using the secondary modifier (default SHIFT_MASK + BUTTON1_MASK) adds elements to the current selection. If an already selected element is clicked, it is deselected.
    • Multi-Selection: Dragging the mouse without clicking an element draws a selection rectangle (the "lens") to pick all nodes contained within that area.
    • Repositioning: If a node is selected and dragged, the node will be repositioned in the layout to follow the mouse.

    Customization:

    • Modifiers: You can specify custom selectionModifiers and addToSelectionModifiers via the constructor.
    • Lens Color: The color of the selection rectangle can be changed using setLensColor(Color).
    • Locking: Use setLocked(boolean) to control whether nodes can be moved via dragging.
    // Default constructor uses BUTTON1 for selection and SHIFT+BUTTON1 for additive selection
    PickingGraphMousePlugin<N, E> plugin = new PickingGraphMousePlugin<>();
    
    // Custom modifiers (e.g., using CTRL instead of SHIFT)
    // selectionModifiers: primary selection
    // addToSelectionModifiers: additive selection
    PickingGraphMousePlugin<N, E> customPlugin = new PickingGraphMousePlugin(
        InputEvent.CTRL_DOWN_MASK, 
        InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK
    );
    
    // Change the selection rectangle color
    plugin.setLensColor(Color.RED);
    
    // Prevent nodes from being moved by dragging
    plugin.setLocked(true);
  12. Configure PickingGraphMousePlugin selection modifiers

    master

    When instantiating PickingGraphMousePlugin, you can define which mouse and keyboard modifiers trigger different selection behaviors.

    Constructors:

    • PickingGraphMousePlugin(): Uses default settings (BUTTON1_MASK for primary selection and BUTTON1_MASK | SHIFT_MASK for additive selection).
    • PickingGraphMousePlugin(int selectionModifiers, int addToSelectionModifiers):
      • selectionModifiers: The bitmask used for primary selection (e.g., clicking a single node or edge).
      • addToSelectionModifiers: The bitmask used for additive selection (e.g., adding to the current set of picked elements).
    // Example: Primary selection is BUTTON1, Additive selection is BUTTON2
    PickingGraphMousePlugin<N, E> plugin = new PickingGraphMousePlugin(
        InputEvent.BUTTON1_MASK, 
        InputEvent.BUTTON2_MASK
    );