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.getContentPane().add(...)
于 2012-11-22T22:34:55.737 に答える