我已经在jgraph的帮助下创建了一个可视化应用程序。我对此有几个问题。
1:我需要根据Vertex对象的属性更改顶点的名称。当我使用默认设置运行应用程序时,顶点的名称打印为Vertex@c8191c (根据顶点的不同而变化)。我想将此名称更改为顶点的属性值。
2:这是最关键的一个。生成的顶点数量不是静态的。数字取决于应用程序的其他各种因素,并且可以在每次应用程序运行时更改。当我使用默认设置运行此应用程序时,节点重叠,并且只有一个节点显示在第一个位置。我需要在jgraph中随机分布节点。
有人能帮我解决这两个问题吗?如果您需要进一步的信息,请提出来。下面是我用来可视化图形的代码。
public void randomizeLocations(JGraph jgraph) {
System.out.println("Visualization 1");
GraphLayoutCache cache = jgraph.getGraphLayoutCache();
System.out.println("Visualization 2");
Random r = new Random();
for (Object item : jgraph.getRoots()) {
System.out.println("Visualization 3");
GraphCell cell = (GraphCell) item;
CellView view = cache.getMapping(cell, true);
Rectangle2D bounds = view.getBounds();
System.out.println("next double"+r.nextDouble()*400);
bounds.setRect(r.nextDouble() * 400, r.nextDouble() * 5,
bounds.getWidth(), bounds.getHeight());
}
System.out.println("Visualization 4");
cache.reload();
System.out.println("Visualization 5");
jgraph.repaint();
System.out.println("Visualization 6");
}提前谢谢你。
发布于 2013-05-11 20:19:48
1)覆盖顶点对象的toString方法。
@Override
public String toString() {
return "Whatever attribute you want to display here";
}
2)将顶点放入HashSet中。这将确保仅将唯一顶点添加到列表中。此外,您需要重写顶点对象的.equals()和.hashCode()方法,以确保唯一性。(请参阅此处https://stackoverflow.com/a/27609/441692)。继续生成更多顶点,直到HashSet大小等于所需的值。
HashSet<Point2D.Double> unique = new HashSet<Point2D.Double>();
Random r = new Random();
for (Object item : jgraph.getRoots()) {
System.out.println("Visualization 3");
GraphCell cell = (GraphCell) item;
CellView view = cache.getMapping(cell, true);
Rectangle2D bounds = view.getBounds();
int currentSize = unique.size();
double x;
double y;
while (unique.size() == currentSize) {
x = r.nextDouble() * 400;
y = r.nextDouble() * 5;
unique.add(new Point2D.Double(x,y));
}
bounds.setRect(x, y, bounds.getWidth(), bounds.getHeight());
}https://stackoverflow.com/questions/16496918
复制相似问题