1

I have created an application with the help of jgraph for the visualization. I have couple of problems regarding this.

1: I need to change the names of Vertices according to an attribute of the Vertex object. When i run the application with the default settings, the names of the vertices are printed as Vertex@c8191c (Changes based on the vertex). I want to change this name to an attribute value of the vertex.

2: This is the most critical one. The number of vertices generated is not static. Number depends on various other factors of the application and can change every time the application runs. When i run this application with the default setting the nodes overlap and only one is shown at first place. I need to distribute the nodes randomly within the jgraph.

Can someone please help me with these two problems. If you need further information please mention. Following is my code to visualize the graph.

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");

}

Thank you in advance.

Pathfinder92
  • 117
  • 2
  • 5
  • 12

1 Answers1

1

1) Override the toString method of your Vertices object.

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

2) Put your Vertices in a HashSet. This will ensure that unique vertices are only added to your list. Moreover you need to override the .equals() and .hashCode() methods of your Vertices object to ensure uniqueness. (See here https://stackoverflow.com/a/27609/441692). Keep generating more vertices until your HashSet size is equal to your desired value.

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());
}
Community
  • 1
  • 1
Sanchit
  • 2,240
  • 4
  • 23
  • 34
  • Hi, thank you for the reply. I got the first problem sorted out. Still have few Confusions with the second solution. I'm pretty sure that the application produces different objects with unique attribute values. That's is a feature of the application. Besides, it creates separate nodes for all the vertices. Just that they are overlapped. Still i can drag them around within the jframe and separate them. What i need is, i want the nodes to be separate at the time they are created. Sorry if i have missed something. If you can please explain your answer bit further... – Pathfinder92 May 11 '13 at 14:12
  • I want them to be evenly distributed within the jframe despite of the the number of nodes. – Pathfinder92 May 11 '13 at 14:14
  • I'll need to stare at your code that would let me programatically move these Nodes. You can just use the same HashSet idea to figure out if a node already exists in a certain position and then place it differently. – Sanchit May 11 '13 at 15:38
  • I have given the code below. I create the graph in a different class. – Pathfinder92 May 11 '13 at 15:53
  • That code is useless. Please don't put it in an answer. Just put it in the question itself by editing it. I need the code that generates the `graph` variable / code for the class ListenableDirectedGraph – Sanchit May 12 '13 at 04:00
  • hi, i edited the question with the graph generation code. I included the relevant functions as well. I'm sorry if it is too long. – Pathfinder92 May 12 '13 at 15:27
  • Still wrong code :\ graph.addVertex(v) probably has the answer? I'm looking for stuff with x and y coordinates. Once you reach that code I'm sure you can figure it outself as well. – Sanchit May 12 '13 at 16:46
  • I'm really sorry for posting irrelevant code on this. I edited the question. If you can please have a look at it. – Pathfinder92 May 15 '13 at 04:45
  • I edited my answer. Hopefully this will suffice. Similarly we can also check if any two rectangles intersect and if they do then reroll the dice. You should also probably delete your answer below or merge your code in with the above stuff. – Sanchit May 15 '13 at 07:48