如何在jung中对顶点进行分组?

时间:2014-04-08 11:51:47

标签: java graph jung jung2

我开始学习JUNG的Graph建模。 我实际上有两个问题: 1.有什么办法可以对一些顶点进行分组吗?喜欢在他们周围画一个盒子? 2. JUNG是否支持没有源顶点的边缘?如果这是正常边缘:a - >湾我想说 - > B'/ P>

谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

您可以使用VisualizationViewer#addPreRenderPaintablePaintable中注册VisualizationViewer。在绘制任何其他东西之前(即在绘制顶点或边缘之前),将调用此paintable。在此paintable中,您可以计算要分组的顶点的边界框,并将其绘制为矩形。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.Layer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.AbstractModalGraphMouse;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;

public class JUNGVertexStuffTest 
{
    public static void main(String[] args) 
    {
        JFrame jf = new JFrame();
        final Graph<String, String> g = getGraph();
        final VisualizationViewer<String, String> vv = 
            new VisualizationViewer<String, String>(
                new FRLayout<String, String>(g));

        final AbstractModalGraphMouse graphMouse = 
            new DefaultModalGraphMouse<String,String>();
        vv.setGraphMouse(graphMouse);

        List<String> verticesInBox = Arrays.asList("v0", "v1");
        addVertexGroupPainter(vv, verticesInBox);

        jf.getContentPane().add(vv);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

    private static <V> void addVertexGroupPainter(
        final VisualizationViewer<V, ?> vv, final Iterable<V> verticesInBox)
    {
        vv.addPreRenderPaintable(new VisualizationViewer.Paintable()
        {
            @Override
            public boolean useTransform()
            {
                return true;
            }

            @Override
            public void paint(Graphics gr)
            {
                Graphics2D g = (Graphics2D)gr;

                Layout<V, ?> layout = vv.getGraphLayout();
                AffineTransform transform = 
                    vv.getRenderContext().
                       getMultiLayerTransformer().
                       getTransformer(Layer.LAYOUT).
                       getTransform();
                Rectangle2D boundingBox = 
                    computeBoundingBox(verticesInBox, layout, transform);

                double d = 20;
                Shape rect = new RoundRectangle2D.Double(
                    boundingBox.getMinX()-d, 
                    boundingBox.getMinY()-d,
                    boundingBox.getWidth()+d+d,
                    boundingBox.getHeight()+d+d, d, d);
                g.setColor(new Color(255,200,200));
                g.fill(rect);
                g.setColor(Color.BLACK);
                g.draw(rect);

            }
        });
    }


    private static <V> Rectangle2D computeBoundingBox(
        Iterable<V> vertices, Layout<V, ?> layout, AffineTransform at)
    {
        double minX = Double.MAX_VALUE;
        double minY = Double.MAX_VALUE;
        double maxX = -Double.MAX_VALUE;
        double maxY = -Double.MAX_VALUE;
        for (V vertex : vertices)
        {
            Point2D location = layout.transform(vertex);
            at.transform(location, location);
            minX = Math.min(minX, location.getX());
            minY = Math.min(minY, location.getY());
            maxX = Math.max(maxX, location.getX());
            maxY = Math.max(maxY, location.getY());
        }
        return new Rectangle2D.Double(minX, minY, maxX-minX, maxY-minY);
    }


    public static Graph<String, String> getGraph() 
    {
        Graph<String, String> g = new DirectedSparseGraph<String, String>();
        g.addVertex("v0");
        g.addVertex("v1");
        g.addVertex("v2");
        g.addVertex("v3");
        g.addVertex("v4");
        g.addEdge("e0", "v0", "v1");
        g.addEdge("e1", "v1", "v2");
        g.addEdge("e2", "v2", "v3");
        g.addEdge("e3", "v3", "v4");
        g.addEdge("e4", "v4", "v0");
        g.addEdge("e5", "v1", "v3");
        g.addEdge("e6", "v2", "v4");
        return g;
    }
}

注意:根据您确定应该对哪些顶点进行分组的方式,并根据布局,可能会有更优雅或有效的解决方案。但是这个应该是通用的,甚至可以使用动态布局或顶点集。


关于“没有顶点的边”的问题:不支持这样的边。首先,因为在调用Graph#addEdge(...)时指定两个顶点。除此之外,这里的关键问题是如何确定此边缘的方向。当然可以考虑“解决方法”。例如,插入一些声明为“虚拟”顶点并从渲染中排除的特殊顶点(因此,只存在于此类边缘的“一个(不可见)端点”。但这将是一个麻烦。

我的水晶球说,目的是表明顶点有“更多边缘”,其端点根本不相关。 IF 就是这种情况,您可以只绘制一些指示这些边缘的线条。这可以粗略完成如下(但这只是一个草图,应该被认为是一个深刻的推荐!对此有肯定“更好”的解决方案!)

    final Renderer.Vertex<String, String> originalVertexRenderer = 
        vv.getRenderer().getVertexRenderer();
    vv.getRenderer().setVertexRenderer(new Renderer.Vertex<String, String>()
    {
        @Override
        public void paintVertex(RenderContext<String, String> rc,
            Layout<String, String> layout, String vertex)
        {
            if (vertex.equals("v4"))
            {
                Point2D p = layout.transform(vertex);
                Line2D pseudoEdge = new Line2D.Double(
                    p.getX(), p.getY(), p.getX() + 100, p.getY() + 100); 
                rc.getGraphicsContext().draw(pseudoEdge);
            }
            originalVertexRenderer.paintVertex(rc, layout, vertex);
        }
    });
相关问题