JUNGに関して、結局使わなかったけど

2005-09-19 02:50:01 | Weblog
How to add vertex to a user-defined position
2005-09-01 01:40
hello , every one
What I want to do is very simple. I just want to add vertexs to the appointed position in graph without using Layout arigorism offered by JUNG, How can I do it , and Is there any useful methods like add(Vertex v, Positon p)? and where is it? I checked some threads about this topic , but failed to find any solution.

Thanks in advance.




By: Dhaval - dhaval04
RE: How to add vertex to a user-defined posit
2005-09-01 04:57
Hi,

I have post similar message like this before. (https://sourceforge.net/forum/forum.php?thread_id=1291001&forum_id=252062)

Anyway let me give you some hints. Layout does not need to be an algorithm. Using layout class, vertices are positioned on graph.

For your case, I would do following:

(1) Make my own vertex extending SparseVertex, say MyVertex.
(2) In MyVertex, I will put fields X, Y (x,y location of graph). You can set these fields while adding vertex to graph. Remember to set these fields before you add Layout to graph.
(3) Make my layout MyLayout extending AbstractLayout. (You can use other layout if you want. Abstract will give you bare basic stuff to go on). In this MyLayout, override the method initializeLocations() as follows:

protected void initializeLocations()
{
super.initializeLocations();
Vertex[] vertices = (Vertex[]) getVisibleVertices().toArray(new Vertex[0]);
orderVertices(vertices);

for (int i = 0; i <vertices.length; i++) Coordinates coord = getCoordinates(vertices[i]);

coord.setX( ((MyVertex) vertices[i]).getX() );
coord.setY( ((MyVertex) vertices[i]).getY() );

// Rest of stuff.
}
}

I think this way you can position vertex at desired position. One more thing. When you add vertex to graph, it will add to graph data structure. Still you need to write one layout to calculate positions of that vertex in graph drawing.

You can refer to examples and source for more information. I hope above talk will get you moving. Feel free to post questions.

Regards,
Dhaval



  By: Nobody/Anonymous - nobody
RE: How to add vertex to a user-defined position
2005-09-01 09:47
thank you so much for your kind reply , but I still got some problems, here is my code

public class MyVertex extends SparseVertex{
private int X ;
private int Y ;

public int getX() {
return X;
}

public void setX(int x) {
X = x;
}

public int getY() {
return Y;
}

public void setY(int y) {
Y = y;
}
}

public class MyLayout extends AbstractLayout{

public MyLayout(Graph arg0) {
super(arg0);
}

protected void initializeLocations() {

super.initializeLocations();
Vertex[] vertices = (Vertex[]) getVisibleVertices().toArray(new Vertex[0]);

for (int i = 0; i <vertices.length; i++) {
coord.setX( ((MyVertex) vertices[i]).getX() );
coord.setY( ((MyVertex) vertices[i]).getY() );
}
}
}

And when I run codes below , I got error at the last line ( graph.addVertex(mv); )

graph = new DirectedSparseGraph();
gd = new GraphDraw(graph);
MyVertex mv = new MyVertex() ;
mv.setX(100);
mv.setY(100);
gd.getVisualizationViewer().setGraphLayout(new MyLayout(graph));
vv = gd.getVisualizationViewer();
graph.addVertex(mv);

the error message is
java.lang.NullPointerException
at edu.uci.ics.jung.visualization.AbstractLayout.getX(AbstractLayout.java:248)
at edu.uci.ics.jung.visualization.VisualizationViewer.renderGraph(VisualizationViewer.java:696)
at edu.uci.ics.jung.visualization.VisualizationViewer.paintComponent(VisualizationViewer.java:636)
at javax.swing.JComponent.paint(JComponent.java:808)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:557)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4794)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
at javax.swing.JComponent.paint(JComponent.java:798)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1312)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
java.lang.NullPointerException
at edu.uci.ics.jung.visualization.AbstractLayout.getX(AbstractLayout.java:248)
at edu.uci.ics.jung.visualization.VisualizationViewer.renderGraph(VisualizationViewer.java:696)
at edu.uci.ics.jung.visualization.VisualizationViewer.paintComponent(VisualizationViewer.java:636)
at javax.swing.JComponent.paint(JComponent.java:808)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4787)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
at javax.swing.JComponent._paintImmediately(JComponent.java:4685)
at javax.swing.JComponent.paintImmediately(JComponent.java:4488)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
java.lang.NullPointerException

thanks again for all your help.





 

By: Dhaval - dhaval04
RE: How to add vertex to a user-defined posit
2005-09-01 14:36
Gotcha.

You were not adding vertex to graph before applying MyLayout to it and that's why it created problem.

Following is your code:

graph = new DirectedSparseGraph();
gd = new GraphDraw(graph);
MyVertex mv = new MyVertex() ;
mv.setX(100);
mv.setY(100);
gd.getVisualizationViewer().setGraphLayout(new MyLayout(graph));
vv = gd.getVisualizationViewer();
graph.addVertex(mv);

Here you are adding vertex after you declare your layout. Below is the solution:

graph = new DirectedSparseGraph();
gd = new GraphDraw(graph);
MyVertex mv = new MyVertex() ;
mv.setX(100);
mv.setY(100);
graph.addVertex(mv);
gd.getVisualizationViewer().setGraphLayout(new MyLayout(graph));
vv = gd.getVisualizationViewer();

Changes: I put addVertex(...) after mv.setY(...). Now in MyLayout it can find vertex in that for loop of initializeLocations() and it can set X and Y co-ordinates of it. It should work for you as it worked for me.

Let me know how it goes.

Regards,
Dhaval



 

By: Nobody/Anonymous - nobody
RE: How to add vertex to a user-defined position
2005-09-01 19:07
hi , Dhaval
Thank you so much !! It worked for me !! Thank you for your great help!!