George Armhold wrote:
Greetings,

We are trying to implement a whiteboard-like scribble tool using
JSVGComponent.  In order to get mouse events we extend JSVGComponent
and implement MouseListener & MouseMotionListener.  Then the trick is
to convert the AWT coordinates into viewbox coords, so that the SVG
doc can be appropriately modified.  (We are saving the drawing events
directly to the SVG for later playback.)  Here is how we are doing the
coordinate translation:

  /**
   * get an AffineTransform which will convert AWT screen coords to
   * viewbox coords for the current document.
   */
  private void getInverseAFT() {
    try {
      AffineTransform aft = getViewBoxTransform();
      inverseAFT = aft.createInverse();
    } catch(NoninvertibleTransformException err) {
      err.printStackTrace(System.err);  // FIXME: better error handling
    }
  }


/** * Convert an AWT screen coordinate to a SVG viewbox coord for the * current document. */ private Point2D screenToViewBox(java.awt.event.MouseEvent e) { if (inverseAFT == null) { getInverseAFT(); }

    Point2D p = new Point2D.Float(e.getX(), e.getY());
    return inverseAFT.transform(p, p);
  }

The problem: if the JSVGComponent has not been resized,
getViewBoxTransform() seems to return an AffineTransform that
does not do the proper scaling- the AWT coords end up rendering in the
upper-left hand corner of the SVG doc.  If I resize the component by
dragging the resize bar on the frame (or force a resize using
setSize()) it works fine.

Can anyone comment on this?  We are using the latest batik 1.5 from
CVS.

The caching of the inverseAFT seems like it might be problematic. I would always get the viewbox transform and get it's inverse (for a 2x2 matrix this is pretty light weight really). Otherwise this code looks almost exactly like the code in JSVGViewerFrame that is used to show mouse coordinates in the status bar - and that seems to work.




--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to