1

我在 jgraph 的帮助下创建了一个用于可视化的应用程序。我对此有几个问题。

1:我需要根据Vertex对象的一个​​属性来改变Vertices的名字。当我使用默认设置运行应用程序时,顶点的名称打印为 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");

}

先感谢您。

4

1 回答 1

1

1) 覆盖 Vertices 对象的 toString 方法。

@Override
public String toString() {
  return "Whatever attribute you want to display here";
}

2)把你的顶点放在一个HashSet中。这将确保仅将唯一顶点添加到您的列表中。此外,您需要覆盖 Vertices 对象的 .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());
}
于 2013-05-11T12:19:48.403 回答