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

org.jungrapht.samples.sugiyama.MinCrossMulticomponent Maven / Gradle / Ivy

The newest version!
package org.jungrapht.samples.sugiyama;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import org.jgrapht.Graph;
import org.jungrapht.samples.util.TestGraphs;
import org.jungrapht.samples.util.TitlePaintable;
import org.jungrapht.visualization.VisualizationViewer;
import org.jungrapht.visualization.decorators.EdgeShape;
import org.jungrapht.visualization.layout.algorithms.HierarchicalMinCrossLayoutAlgorithm;
import org.jungrapht.visualization.layout.algorithms.sugiyama.Layering;
import org.jungrapht.visualization.renderers.Renderer;

/**
 * Demo that uses the EiglspergerLayoutAlgorithm to display a directed graph that has several
 * components (not connected to each other by any edges).
 */
public class MinCrossMulticomponent extends JFrame {

  public MinCrossMulticomponent() {

    JPanel container = new JPanel(new GridLayout(0, 2));

    Graph graph = TestGraphs.getDemoGraph(true);

    VisualizationViewer vv1 = configureVisualizationViewer(graph);
    HierarchicalMinCrossLayoutAlgorithm layoutAlgorithm1 =
        HierarchicalMinCrossLayoutAlgorithm.edgeAwareBuilder()
            .postStraighten(true)
            .threaded(false)
            .layering(Layering.COFFMAN_GRAHAM)
            .build();
    layoutAlgorithm1.setVertexBoundsFunction(vv1.getRenderContext().getVertexBoundsFunction());
    vv1.getVisualizationModel().setLayoutAlgorithm(layoutAlgorithm1);
    container.add(vv1.getComponent());

    VisualizationViewer vv2 = configureVisualizationViewer(graph);
    HierarchicalMinCrossLayoutAlgorithm layoutAlgorithm2 =
        HierarchicalMinCrossLayoutAlgorithm.edgeAwareBuilder()
            .postStraighten(true)
            .threaded(false)
            .layering(Layering.COFFMAN_GRAHAM)
            .separateComponents(false)
            .build();
    layoutAlgorithm2.setVertexBoundsFunction(vv2.getRenderContext().getVertexBoundsFunction());
    vv2.getVisualizationModel().setLayoutAlgorithm(layoutAlgorithm2);
    container.add(vv2.getComponent());

    vv2.setSelectedVertexState(vv1.getSelectedVertexState());

    vv1.addPreRenderPaintable(new TitlePaintable("Separated Components", vv1.getPreferredSize()));
    vv2.addPreRenderPaintable(
        new TitlePaintable("Not Separated Components", vv2.getPreferredSize()));

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    add(container);
    pack();
    setVisible(true);
  }

  private VisualizationViewer configureVisualizationViewer(
      Graph graph) {
    VisualizationViewer vv =
        VisualizationViewer.builder(graph)
            .layoutSize(new Dimension(600, 600))
            .viewSize(new Dimension(700, 500))
            .build();

    vv.getRenderContext().setEdgeShapeFunction(EdgeShape.line());
    vv.setVertexToolTipFunction(Object::toString);
    vv.getRenderContext().setArrowFillPaintFunction(n -> Color.lightGray);

    vv.getRenderContext().setVertexLabelPosition(Renderer.VertexLabel.Position.CNTR);
    vv.getRenderContext().setVertexLabelDrawPaintFunction(c -> Color.white);
    vv.getRenderContext().setVertexLabelFunction(Object::toString);
    vv.getRenderContext().setVertexLabelPosition(Renderer.VertexLabel.Position.CNTR);
    return vv;
  }

  public static void main(String[] args) {
    new MinCrossMulticomponent();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy