Jung节点被夹在可视化中



我正在使用荣格创建和可视化某些图。看来节点的中心通过布局算法给出了坐标。因此,当一个节点位于窗格的边缘时,其中一些节点将被切断(也许也可能是节点标签)。我不确定这是荣格问题还是Jframe/GUI问题(我对Java GUI开发不太熟悉)。我确实尝试使图形图像(600x600)的最大尺寸小于窗格的尺寸(800x800)。我尝试使用setLocation()将图表集中,但这似乎不起作用。即使它确实居中了图,我也不确定它是否会解决问题。

这是我的代码。

import java.awt.Dimension;
import javax.swing.*;
import edu.uci.ics.jung.graph.*;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.algorithms.generators.random.EppsteinPowerLawGenerator;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import org.apache.commons.collections15.Factory;
public class Main {
    public static void main(String[] args) {
        // Factories required for the graph generator
        Factory <Integer> vertexFactory = new Factory<Integer>() {
            int vertexCount = 0;
            public Integer create() {
                return vertexCount++;
            }
        };
        Factory <Integer> edgeFactory = new Factory<Integer>() {
            int edgeCount = 0;
            public Integer create() {
                return edgeCount++;
            }
        };
        Factory<Graph<Integer,Integer>> graphFactory = new Factory<Graph<Integer,Integer>>() {
            public Graph<Integer,Integer> create() {
                return new UndirectedSparseGraph<Integer, Integer>();
            }
        };
        int numVertices = 150;
        int numEdges = 150;
        int numIter = 100;       
        // Create a graph using a power law generator
        EppsteinPowerLawGenerator gen = new EppsteinPowerLawGenerator(graphFactory, vertexFactory, edgeFactory, numVertices, numEdges, numIter);      
        Graph<Integer, Integer> graph = gen.create();
        // Visualization of graph
        SpringLayout layout = new SpringLayout(graph);
        layout.setSize(new Dimension(600,600));
        VisualizationImageServer vs = new VisualizationImageServer(layout, new Dimension(800, 800));
        vs.setLocation(100,700); // seems to have no effect
        vs.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
        vs.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<Integer, Number>());
        JFrame frame = new JFrame(); 
        frame.add(vs); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
   }
}

给它拍摄:

float factor = 0.5;
ScalingControl scalingControl = new CrossoverScalingControl();
scalingControl.scale(vs, factor, vs.getCenter());

另外,您不应该直接添加到秋千框架中,而应该使用以下方式添加到其内容窗格中:

frame.getContentPane().add(...)

最新更新