0
我使用JUNG創建和可視化一些圖形。看來節點的中心由佈局算法給出座標。因此,當一個節點位於窗格的邊緣時,該節點中的一些將被切斷(也可能是節點標籤)。我不確定這是JUNG問題還是JFrame/GUI問題(我對Java GUI開發不太熟悉)。我確實嘗試使圖形圖像(600x600)的最大尺寸小於窗格(800x800)的尺寸。我嘗試使用setLocation()來居中顯示圖形,但這似乎不起作用。即使它確實將圖形居中,我也不確定是否會照顧到這個問題。JUNG節點被剪裁在可視化中
這是我的代碼。
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);
}
}