JUNG节点被剪切在可视化中

时间:2012-11-05 01:57:55

标签: jframe jcomponent jung

我正在使用JUNG来创建和可视化一些图表。似乎节点的中心由布局算法给出坐标。因此,当节点位于窗格的边缘时,该节点中的一些节点将被切断(也可能是节点标签)。我不确定这是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);
   }
}

1 个答案:

答案 0 :(得分:1)

试一试:

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

此外,您不应该直接添加到Swing Frame,而是应该使用以下内容添加到其内容窗格:

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