我有以下代码:
package jung;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.algorithms.layout.*;
import java.awt.Dimension;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import javax.swing.JFrame;
/**
*
* @author studnet
*/
public class Jung {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Graph<Integer, String> g = new DirectedSparseGraph<Integer, String>();
g.addVertex((Integer)1);
g.addVertex((Integer)2);
g.addVertex((Integer)3);
g.addEdge("Edge-A", 1, 2); // Note that Java 1.5 auto-boxes primitives
g.addEdge("Edge-B", 2, 3);
FRLayout2<Integer, String> layout = new FRLayout2(g, new Dimension(600, 600));
while ( !layout.done() )
layout.step();
//Layout<Integer, String> layout = new CircleLayout(g);
//Layout<Integer, String> layout = new FRLayout(g);
VisualizationViewer<Integer,String> vv = new VisualizationViewer<Integer,String>(layout);
//vv.setPreferredSize(new Dimension(600,600)); //Sets the viewing area size
JFrame frame = new JFrame("Simple Graph View");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);
// TODO code application logic here
}
}
但是所有顶点都在左上角,有什么建议吗?
最佳答案
我相信你可能需要打电话
layout.initialize()
它将运行准备代码。
编辑:
我上面的说法是错误的。在浏览了 FRLayout
和 FRLayout2
的差异之后,我认为周围可能存在一些逻辑错误:
fvd1.setLocation(0, 0);
...
fvd1.setLocation(fvd1.getX()+2 * xDelta * forceOverDeltaLength,
fvd1.getY()+ 2 * yDelta * forceOverDeltaLength);
看起来 FRLayout2 对于原始算法并不太严格。
关于java - Jung FXLayout 未定位顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16777195/