Centering a vertex

2005-05-28
2013-05-29
  • Michael Boeni
    Michael Boeni
    2005-05-28

    Hi all

    I would like to be able to pan a graph so that a selected vertice is centered (on the screen, JInternalFrame in my case).

    What is the best way to do this?

    I have tried a few things but seem to be stuck with the position of the vertex (if I pan the graph, the vertex position stays put relative to the layout) - so I have no reference on how much and where to I have to pan the whole thing.

    Any ideas?

    Cheers,
    Michael

     
    • Tom Nelson
      Tom Nelson
      2005-05-28

      Michael,

      I don't understand what you are asking.
      Could you explain how it is different from the result
      you would get from either:

      a) Mousing on an empty area of the graph and dragging
      to pan the graph until the desired Vertex is at the center
      of the display.

      b) Mousing on a Vertex and dragging it to the center
      of the display, leaving all other Vertices where at their
      original locations.

      I'm sure you can do both a and b programatically (without
      the mouse) but I'm not sure if that's what you want.

      Maybe of you describe the things you have tried, I
      would be able to see what you are trying to do.

      Tom

       
    • Michael Boeni
      Michael Boeni
      2005-05-28

      Hi Tom

      sorry for being unspecific. I want to do a right-click on a vertex and select "center" which then should move the graph (not the vertex) so the selected vertex is centered on the screen...ok now?

      Its actually panning the graph until a given vertex is in the center of the screen...

      Cheers,
      m.

       
      • Tom Nelson
        Tom Nelson
        2005-05-28

        Michael,
        Here is the code I use. This is for the latest version
        of jung in CVS. You may have to rename the
        vv.inverseTransform method to vv.transform if you
        are using 1.6.0.
        Hopefully, we will get a new release soon, as the cvs
        version has many changes, bug fixes and improvements
        in it.

        Tom

            Thread animationThread;
           
            public void moveCenter(final Point2D center) {
                if (animationThread != null) {
                    animationThread.interrupt();
                    animationThread = null;
                }
                animationThread = new Thread() {
                    boolean keepGoing = true;

                    static final int PIXEL_COUNT = 5;

                    public void run() {
                        Dimension d = vv.getSize();
                        Point2D viewCenter = new Point2D.Float(d.width/2,d.height/2);
                        viewCenter = vv.inverseTransform(viewCenter);
                        double xdist = viewCenter.getX() - center.getX();
                        double ydist = viewCenter.getY() - center.getY();

                        double hdistance = Math.sqrt(xdist * xdist + ydist * ydist);
                        if (hdistance > 200) {
                            hdistance = 200.0;
                        }
                        int intervals = (int) (hdistance / PIXEL_COUNT);
                        double dx = xdist / intervals;
                        double dy = ydist / intervals;
                        for (int i = 0; i < intervals && keepGoing; i++) {
                                vv.translate(dx, dy);
                            vv.repaint();
                            try {
                                sleep(10);
                            } catch (InterruptedException e) {
                            }
                        }
                        vv.repaint();
                    }

                    public void interrupt() {
                        keepGoing = false;
                        super.interrupt();
                    }
                };
                animationThread.start();
            }

         
    • Michael Boeni
      Michael Boeni
      2005-05-29

      Thanks, Tom.

      I am really keen now to see whats coming in 1.6.1 :)

      Cheers,
      Michael

       
      • Is this functionality (centering a vertex) now built into JUNG?  If so, which class would handle this?

         
    • benkne
      benkne
      2006-12-07

      Hi!

      I also want to change the center of the view, but without changing the location of the vertices. First I have done this manually using just the scaling functions. But I want to know if it is possible to automate this procedure?

      Thanks

       
    • benkne
      benkne
      2006-12-07

      Hi!

      Sorry, I haven't understand your code, Tom.... I think this is exactly what I want! :-)

      Cheers

       
    • Thanks for the code.  There's one part that I don't quite understand.  What is supposed to be passed into moveCenter?  What does the variable "center" represent?  How do I determine its value?

       
      • Tom Nelson
        Tom Nelson
        2006-12-27

        The latest code to center a vertex is in the
        AnimatedPicking GraphMousePlugin.
        Basically, it translates the layout so that the
        picked vertex moves from its current location
        to the point in the layout that is at the center
        of the VisualizationViewer:
        Point2D vvCenterInLayout =
           vv.inverseTransform(vv.getCenter());

        Tom Nelson

         
        • I'm using that code almost verbatim in my JUNG 1.7.6 app
          to center vertices selected from a JTable in a JDialog.

          The problem is while the selected vertex is always centered
          horizontally it is usually not centered vertically.  In
          fact the Y coordinate is all over the place as I select
          various vertices.  And selecting the same vertex several
          times in a row will keep moving it up or down by apparently
          the same displacement for each click.

          Any ideas?

          Thanks,
          Ned

           
          • Tom Nelson
            Tom Nelson
            2007-04-20

            Assuming you are using the DefaultModalGraphMouse which
            contains the AnimatedPickingGraphMousePlugin,
            what happens when, in the picking mode, you CTRL-select
            a vertex with mouse button 1? It should animate a translation
            of the selected vertex to the center of the view....

            Do you see any variation in the Y coordinate in any of
            our demos that have this feature?

            Tom Nelson

             
    • I modified the code above and I had a similar problem (erratic layout movement).  My problem was: I had a satelliteviewer and only one pluggablerenderer for both the satelliteview and visualizationviewer (should be one each).  You didn't mention if you were using satellite view however so this may not help you.

      Here's my code:

      Vertex v = sl.getVertex(label);          //sl - StringLabeller
      Coordinates vd = dl.getCoordinates(v);   //dl - Layout
                         
      PickedState ps = vv.getPickedState();
      ps.clearPickedVertices();ps.pick(v, true);
                         
                          vv.getLayoutTransformer().setToIdentity(); //vv - visualizationviewer
      Dimension d = vv.getSize();
      Point2D viewCenter = new Point2D.Float(d.width/2,d.height/2);
      viewCenter = vv.inverseTransform(viewCenter);
      double xdist = viewCenter.getX() - vd.getX();
      double ydist = viewCenter.getY() - vd.getY();
      vv.translate(xdist, ydist);
      vv.repaint();

      Hope that helps -Mark

       
    • minieymousee
      minieymousee
      2007-04-20

      Hi Mark..

      I try to implement the code that you attach. But no success. Does the code only apply to satellite view only? And waht is dl in your code stands for? Sorry for these question as I am new in using JUNG.

      Need your help.

       
    • Mark,

      You code is a definite improvement.  The selected vertices now move to the
      same location *near* the center (always centered correctly horizontally but
      off center somewhat vertically).  However,  selecting the same vertex successively
      will first cause it jump off the "center" location and then move back to the "center".

      Thanks,
      Ned

       
      • It turns out the centering problem was my bug.  It's working fine now.
        Sorry for disburbing the peace.

        Ned

         
    • minieymousee, in that code dl is the layout; it's a DAGLayout in that case.  I'm learning too, and my advice is to study the examples and search these forums. -Mark

       
    • minieymousee
      minieymousee
      2007-04-23

      Hi Mark..

      I manage to add in your code snippet in my program. I tweak it a little and I manage to get it perfectly.
      Thanks for giving an inspiring idea to me.

      Regards
      Amy