Middle of view

I am trying to make the graph root node appear always in the middle

of the JGoView. So, I get the root node Y position by:
getCanvas().getSize().height / 3
This is so that the root node is always in the middle of the canvas
no matter what the JGoView panel height is.
The above code is not working. Any suggestions?
Thanks!
  1. why / 3 instead of / 2 ?
2) component units are in pixels -- do you need to convert from view to document coordinates? There are various methods to do that on JGoView. And you should use the JGoView.getViewPosition method to get the top-left corner of the view in document coordinates.

I call the following API to resize the graph.

public void rescaleToFit() {
Dimension dispSize = getCanvas().getSize();
Rectangle docbounds = getDocument().computeBounds();
double newscale = 1;
if (docbounds.width > 0 && docbounds.height > 0)
newscale = Math.min((dispSize.width / (double)docbounds.width), (dispSize.height / (double)docbounds.height));
if (newscale > 1)
newscale = 1;
setScale(newscale);
}
After rescaling, I want to make sure the graph is in the middle height of
the view.

The root node y position needs to be less than half the height of the

view, so that the node appears in the middle of the view. Thus /3 not /2

Call JGoView.setViewPosition to scroll it as far as you want.

But if the node you want to center is near the edge of the document, you might not be able to scroll it far enough for the node to actually be centered. (Imagine a text editor -- you can't scroll the first line of the file to be in the middle of the window, at least in most text editors.)
To get around that limitation, you'll need to set the document's Size and TopLeft properties to be large enough to allow such scrolling.

Can you explain to me the difference between view and document?

Is there a demo for JGoOverview?

read comments:

/**JGoDocument represents a group of JGoObjects that can be viewed inside a JGoView. JGoDocument represents the model in the model-view-controller architecture; JGoView plays the role of the view and as a default controller. The JGoDocument should be thought of as an ordered list of objects. The objects are drawn in sequential order, so objects at the beginning of the list appear "behind" objects that are at the end. You can add, remove, and iterate over the document's objects by using the document's implementation of the JGoObjectCollection interface. The JGoObjects held in the document have a size and position. The coordinate system used by the document just comes from the units used by its objects. JGoViews have a coordinate system that may be translated and scaled from that of the document, so as to support panning and zooming. The document's size is automatically expanded to encompass all of its objects. The document keeps track of all registered JGoDocumentListeners. JGoView is a predefined implementor of JGoDocumentListener. It needs to notice when document objects change so that it can update the visible rendering of those objects. You can register your own listeners to notice changes to the document or its objects. The fireUpdate() method actually does the notification of all document listeners. **/

something more:

/** JGoView is a lightweight Swing JComponent that supports the display and editing of graphical objects such as nodes and links. JGoView supports the model-view-controller paradigm. JGoDocument is the model for JGoView. The primary purpose of JGoView is to display a JGoDocument and its JGoObjects. You can use the default JGoDocument that is created for the default JGoView constructor, or you can supply your own, either at construction or later. A JGoView is just a regular Swing JComponent. The part of a JGoView that shows the document is called the canvas. A view can also have scroll bars. A view can have a border, but the canvas does not support one. JGoView also supports the display of its own view-specific objects. Thus each view on the same document can have its own set of JGoObjects. These view objects will appear in front of all document objects. The most common example of view object is a selection handle (JGoHandle). **/