0

我正在使用 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);
   }
}
4

1 回答 1

1

试一试:

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

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

frame.getContentPane().add(...)
于 2012-11-22T22:34:55.737 回答