gpt4 book ai didi

java - 随机化顶点位置 JUNG

转载 作者:行者123 更新时间:2023-11-30 04:10:33 25 4
gpt4 key购买 nike

我创建了一个 java 程序,该程序从 1 个顶点开始,从那里开始每个周期添加 1 个顶点和 2 条边。它使用静态布局

Layout<Number, Number> staticLayout = new StaticLayout<Number, Number>(g, layout);
vv = new VisualizationViewer<Number, Number>(staticLayout, new Dimension(550, 550));

这听起来非常不技术性,但图表看起来不够随机,基本上我的意思是,每次运行时,它们似乎总是围绕着图形的边缘,而很少有接近中心的地方。我的程序通常使用 100 个生成的顶点,最终我将在中心使用六个顶点,而其他顶点则在边缘周围。

下面是我刚刚创建的一个随机示例。

也许有人可以确认这实际上是随机的,或者如果没有,是否有办法解决这个问题,或者我是否设置了错误。因为我希望节点尽可能随机。

如有任何帮助,我们将不胜感激。谢谢

Random Example

下面是小程序的相关代码。涉及其设置。

public class AnimatingAddNodeDemo extends JApplet {

//create a graph
Graph<Number, Number> ig = Graphs.synchronizedUndirectedGraph(new UndirectedSparseMultigraph<Number, Number>());

ObservableGraph<Number, Number> og = new ObservableGraph<Number, Number>(ig);
og.addGraphEventListener(new GraphEventListener<Number, Number>() {

public void handleGraphEvent(GraphEvent<Number, Number> evt) {
//System.err.println("got " + evt);
}
});
this.g = og;
//create a graphdraw
layout = new FRLayout<Number, Number>(g);
layout.setSize(new Dimension(600, 600));
setSize(700, 700);
Relaxer relaxer = new VisRunner((IterativeContext) layout);
relaxer.stop();
relaxer.prerelax();

Layout<Number, Number> staticLayout = new StaticLayout<Number, Number>(g, layout);
vv = new VisualizationViewer<Number, Number>(staticLayout, new Dimension(550, 550));

JRootPane rp = this.getRootPane();
rp.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);

getContentPane().setLayout(new BorderLayout());
}

Integer v_prev = null;

public void process() {

vv.getRenderContext().getPickedVertexState().clear();
vv.getRenderContext().getPickedEdgeState().clear();
try {
if (g.getVertexCount() < 100) {
//add a vertex

Integer v1 = nodeCount;
g.addVertex(v1);
nodeCount++;
System.out.println("adding vertex " + v1);
vv.getRenderContext().getPickedVertexState().pick(v1, true);
j.setText(myText);

// wire it to some edges
if (v_prev != null) {
Integer edge = edgeCount;
//vv.getRenderContext().getPickedEdgeState().pick(edge, true);

// let's connect to a random vertex, too!

int rand = (int) (Math.random() * (edgeCount-1)); // because there is a 0 node
while (v1.equals(rand)) {
System.out.println("avoided connecting to myself");
rand = (int) (Math.random() * (edgeCount-1)); // because there is a 0 node
}

edgeCount++;
g.addEdge(edge, rand, v1); //add an edge called var1, between the nodes var2 and var3
vv.getRenderContext().getPickedEdgeState().pick(edge, true);
System.out.println("Adding edge " + edge + " between " + rand + " & " + v1 + "()");
}

v_prev = v1;
layout.initialize();

Relaxer relaxer = new VisRunner((IterativeContext) layout);
relaxer.stop();
relaxer.prerelax();
vv.getRenderContext().getMultiLayerTransformer().setToIdentity();
vv.repaint();

} else {
done = true;
}

} catch (Exception e) {
System.out.println(e);
}
}


public static void main(String[] args) {
AnimatingAddNodeDemo and = new AnimatingAddNodeDemo();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(and);

and.init();
and.start();
frame.pack();
//frame.setVisible(true);
}
}

最佳答案

您的图表不是随机的原因可能是由于您将 FRLayout 传递给构造函数。

layout = new FRLayout<Number, Number>(g);
// ...
Layout<Number, Number> staticLayout = new StaticLayout<Number, Number>(g, layout);

您可以通过扩展 AbstractLayout 来创建自己的随机布局类。但是,根据JavaDoc , 如果排除第二个构造函数参数,StaticLayout 将随机布局节点。

Layout<Number, Number> staticLayout = new StaticLayout(Number, Number>(g);

关于java - 随机化顶点位置 JUNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19709217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com