last day (18 days later) » 

11:41 AM
0
A: Placing a vertex at the center of a graph

flavio.donzeFrom the graphComponent you can do this if it is an instance of mxGraphComponent: Rectangle bounds = graphComponent.getGraphControl().getBounds(); double center = bounds.getWidth()/2;

 
Does this give the centre of the graph or the graphComponent?
 
Your right the graphBounds are probably not what you want. It is the bounds of the existing graph, if you have elements positioned further right on the x axis, the graphBounds.getWidth() is larger. I edited the post.
 
Thanks flavio.donze but there seems to be a type mismatch , the right hand side graphComponent.getGraphControl().getBounds() is only a rectangle and not mxRectangle , typecasting wont work too . And do you have any idea abt mxStacklayout ?
 
Ups my bad, fixed it. I edited the previous code and missed that part. Sorry never used mxStacklayout.
 
Can the center be (0,0)
 
11:41 AM
bounds.getWidth() is 0 too? Do not use the x and y axis value, the will be 0.
 
ya bounds.getWidth () is 0 . x,y axis value didn't get u??
 
That's odd, I get the size of the drawing area java.awt.Rectangle[x=0,y=0,width=1299,height=968]. Which will change if I change the window size. Is it possible your graph is not initialized, do you get a different result if there are already elements on the graph?
 
Hey Hii flavio.donze , the vertex is always shown at the bottom of the graph , no matter what coordinates I give
I have initialized the graph in the beginning
 
just to make it clear I'm using the java API not javascript, should be similar though.
 
ya im using the java API JGraphX and not mxGraph which is the javascript version
 
11:44 AM
ok great
what do you get when you print the following : System.out.println(graphComponent.getGraphControl().getBounds());
 
My graph has multiple groups which contains vertices in it . Do u think the groups are occuping extra space which is forcing the vertex which I want in the center to the bottom of the graph
 
should not be a problem since it is possible to layer vertex on top of eachother.
 
I get this java.awt.Rectangle[x=0,y=0,width=0,height=0] after printing System.out.println(graphComponent.getGraphControl().getBounds()); but I have set bounds for my graphComponent this way : graphComponent.setBounds(0, 30, contentPane.getPreferredSize().width+20, contentPane.getPreferredSize().height);
where the contentPane width and height are 1500,800
 
Can you try without setting the bounds of the graphComponent, just for testing?
 
The graph wont be displayed
 
11:59 AM
maybe set a layout of the parent component? could be that this is the problem...
 
parent you mean the default parent for the graph while calling the insertVertex API?? how can I set a layout for the default parent??
 
I mean the parent swing component
In my application I do not set the bounds of the graph component, the graph component grows and shrinks if the window is resized.
 
U mean the jPanel where I'm placing my graphcomponent??
 
contentPane.setPreferredSize(new Dimension(cpwidth, cpHeight)); this is what I have done cpwidth = 1500 cpheight =800 ; still gives the same result
 
12:14 PM
how is you application setup? standalone window? why do you need a fix size for the graph
graph or any component?
what happens if you resize the window?
 
because my PanelSize is set to the above values (1500,800) , i want my graph to occupy the entire space of the panel . If i resize the panel , either the graph vertices aren't shown or half of the panel remains empty
 
are you familiar with UI layouts, e.g. BorderLayout?
 
ya I have heard if it i have tried giving BorderLayout.Center as one of the coordinates for my vertex which I desire to be in the center of my graph but invain
Did u have a look at this link : forum.jgraph.com/questions/2002/… but it is in the javascript version of mxgraph
 
I'm not talking about the vertex but the UI itself. Normally in Swing you use Layouts instead of fix position values, I have a feeling that if you manually set the size of your graphcomponent it could cause trouble.
So on your jpanel you should do .setLayout(new BorderLayout());
and then adding your graph component .add(graphComponent, BorderLayout.CENTER);
now resizing should also resize the graph component
then test if you get the correct bounds: System.out.println(graphComponent.getGraphControl().getBounds());
 
12:36 PM
it still shows 0 for all values
 
12:48 PM
hmm ok, clue less sorry. lets focus on the inserting @ bottom
how do you insert your vertex?
 
1:31 PM
Object vtx1 = graph.insertVertex(graph.getDefaultParent(),null,mesh,m,n,cardPanelWidth,cardPan‌​elHeight)
My m,n values are 10,10 and i increment them at the end of a loop
cardPanelWidth =150 , cardPanelHeight =40 . Then in the inner loop i create mutiple groups using Object vn = graph.insertVertex(graph.getDefaultParent(), null, "Side - " + cmf.getLabel(),m,n,2000,400); and then there is a recursive function call which builds vertices and sends them into the groups according to some conditions
Though the group and center vertex have the same x,y coordinates , i can see them distinctly , the center vertex goes to the bottom left of the graphComponent
 
1:46 PM
try to insert the vertext graph.insertVertex(null, null, ...
ok that won't help since it will insert on the default parent
 
2:23 PM
sorry span, I don't think I can help you and I got to go now. wish you a nice day
 

  last day (18 days later) »