You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by hardc0d3r <ha...@gmail.com> on 2008/07/06 13:59:19 UTC

getting the element in svg from an overlay

how can i get an element from an overlay? when i use getGraphicsNode() i get
the whole svg document. what i want is a single element only and not the
whole..
-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18301441.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
thanks for the reply. but the article explains how the screen coordinates can
be converted to svg coordinates. i am trying the other way around.. i want
the screen coordinates of the svg coordinates..


Helder Magalhães wrote:
> 
>> i did not quite understand.. where will i get the matrix? through the
>> element
>> that i want the coordinates converted? do i get the inverse of the matrix
>> and apply it to the point? i did that but i get the coordinates on the
>> canvas and not on the screen..
> 
> Although slightly off-topic, there's an interactive sample [1] which
> may help demonstrating the concept Thomas Deweese was reffering to.
> Getting screen coordinates makes it work even then the container
> element is resized (read "not using 1:1 scaling"). The dragging in
> demo works as expected when you resize the viewport, for example - in
> more naive samples, when you did that you ended up with a drag which
> was quicker or slower than expected due to the screen transform.
> 
> Hope this helps,
> 
>  Helder Magalhães
> 
> [1]
> http://blog.codedread.com/archives/2005/12/21/how-to-enable-dragging-in-svg/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18502018.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by Helder Magalhães <he...@gmail.com>.
> i did not quite understand.. where will i get the matrix? through the element
> that i want the coordinates converted? do i get the inverse of the matrix
> and apply it to the point? i did that but i get the coordinates on the
> canvas and not on the screen..

Although slightly off-topic, there's an interactive sample [1] which
may help demonstrating the concept Thomas Deweese was reffering to.
Getting screen coordinates makes it work even then the container
element is resized (read "not using 1:1 scaling"). The dragging in
demo works as expected when you resize the viewport, for example - in
more naive samples, when you did that you ended up with a drag which
was quicker or slower than expected due to the screen transform.

Hope this helps,

 Helder Magalhães

[1] http://blog.codedread.com/archives/2005/12/21/how-to-enable-dragging-in-svg/

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


use element coordinates

Posted by hardc0d3r <ha...@gmail.com>.
it's final. i'm going to use the overlays...

new question? is there a way to get the attributes of the use element?
especially the coordinates..
-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18749949.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi HardC0d3r,

hardc0d3r <ha...@gmail.com> wrote on 07/29/2008 10:07:13 PM:

> with your help, i can draw flicker free.. thanks..

    Ok, good.

> cpu usage is more worse with larger canvas dimension, right? my canvas 
is
> currently 10k pixels x 10k pixels and contains a lot of lines/paths, 
texts
> and shapes (a map of a city).

    You say the canvas is 10kx10k, that is rather large for any Monitor
I'm aware of :)  So this is either hooked up to some sort of wall sized
display system (quite possible for a map of a city) or you are confusing
the effective document size with the canvas size.  So are you showing
a small window into a 10Kx10K canvas or are you actually showing 10Kx10K
pixels to the user at once?

> i am planning to reduce the dimension by atleast half or use glass pane. 

> and the question is : what would you recommend? stick with the 
> overlay/immediateRepaint() and reduce the dimension of the svg or use 
> glass pane?

   If you are actually showing a 10K x 10K screen I would suggest
adding a new variant of immediateRepaint to the canvas.  One problem
with the current immediateRepaint is that it always updates the
entire canvas.  For normal situations this isn't a big deal since 
this involves copying only a few Mb of data.  In your case this is 
close to half a Gig of data.  I'm guessing that in most cases your
overlay only covers a small portion of the full 10Kx10K document
so adding a variant that allows you to specify the region that needs
the immediate repaint could significantly improve performance.

   If you look at the current implementation of the method this
should be fairly simple to do and could make a very significant
performance improvement.

> here is the code of my interactor :
> 
> import java.awt.BasicStroke;
> import java.awt.Color;
> import java.awt.Graphics;
> import java.awt.Graphics2D;
> import java.awt.Shape;
> import java.awt.event.InputEvent;
> import java.awt.event.MouseEvent;
> import java.awt.geom.AffineTransform;
> import java.awt.geom.Line2D;
> import java.awt.geom.NoninvertibleTransformException;
> import java.awt.geom.Point2D;
> import java.awt.geom.Rectangle2D;
> import org.apache.batik.bridge.BridgeContext;
> import org.apache.batik.gvt.GraphicsNode;
> import org.apache.batik.swing.JSVGCanvas;
> import org.apache.batik.swing.gvt.InteractorAdapter;
> import org.apache.batik.swing.gvt.JGVTComponent;
> import org.apache.batik.swing.gvt.Overlay;
> import org.w3c.dom.Element;
> import org.w3c.dom.svg.SVGLocatable;
> import org.w3c.dom.svg.SVGMatrix;
> import org.w3c.dom.svg.SVGPoint;
> import org.w3c.dom.svg.SVGRect;
> import org.w3c.dom.svg.SVGSVGElement;
> 
> public class CustomInteractor extends InteractorAdapter {
> 
>     protected boolean finished = true;
>     protected boolean isHandle = false;
>     protected float xStart;
>     protected float yStart;
>     protected float xEnd;
>     protected float yEnd;
>     protected Line2D line;
>     protected Shape shape;
>     protected Overlay overlay = new LineOverlay();
>     protected BasicStroke markerStroke = new BasicStroke(2,
>             BasicStroke.CAP_SQUARE,
>             BasicStroke.JOIN_MITER,
>             5,
>             new float[]{5, 5}, 0);
>     protected JGVTComponent source;
>     JSVGCanvas at;
> 
>     @Override
>     public boolean startInteraction(InputEvent ie) {
>         return true;
>     }
> 
>     @Override
>     public boolean endInteraction() {
>         return finished;
>     }
> 
>     private Point2D getCanvasCoordinate(Point2D p) {
>         Point2D pt = p;
>         try {
>             AffineTransform bt =
> source.getRenderingTransform().createInverse();
>             return bt.transform(p, null);
>         } catch (NoninvertibleTransformException ex) {
>             return pt;
>         }
>     }
> 
>     private Element getElementAt(JGVTComponent source, float x, float y) 
{
>         Point2D p = getCanvasCoordinate(new Point2D.Float(x, y));
>         GraphicsNode gn = source.getGraphicsNode().nodeHitAt(p);
>         BridgeContext bc =
>                 ((JSVGCanvas) 
source).getUpdateManager().getBridgeContext();
>         return (Element) bc.getElement(gn);
>     }
> 
>     public SVGPoint localPt(JGVTComponent c, Element el, float x, float 
y) {
>         SVGSVGElement svgRoot = ((JSVGCanvas)
> c).getSVGDocument().getRootElement();
>         SVGMatrix mat = ((SVGLocatable) el).getScreenCTM();
>         SVGMatrix imat = mat.inverse();
>         SVGPoint cPt = svgRoot.createSVGPoint();
>         cPt.setX(x);
>         cPt.setY(y);
>         cPt = cPt.matrixTransform(imat);
>         return cPt;
>     }
> 
>     @Override
>     public void mousePressed(MouseEvent e) {
>         JGVTComponent c = source = (JGVTComponent) e.getSource();
>         at = ((JSVGCanvas) c);
>         finished = false;
>         line = null;
>         Element domE = getElementAt(c, e.getX(), e.getY());
>         if ("handle1".equals(domE.getAttribute("id"))) {
>             isHandle = true;
>             xStart = Float.parseFloat(domE.getAttribute("cx"));
>             yStart = Float.parseFloat(domE.getAttribute("cy"));
>             SVGRect bbox = ((SVGLocatable) domE).getBBox();
>             shape = new Rectangle2D.Float(bbox.getX(), bbox.getY(),
> bbox.getWidth(), bbox.getHeight());
>         } else {
>             shape = null;
>         }
>         if (!c.getOverlays().contains(overlay)) {
>             c.getOverlays().add(overlay);
>         }
>         c.immediateRepaint();
>     }
> 
>     @Override
>     public void mouseReleased(MouseEvent e) {
>         JGVTComponent c = (JGVTComponent) e.getSource();
>         finished = true;
>         isHandle = false;
>     }
> 
>     @Override
>     public void mouseDragged(MouseEvent e) {
>         JGVTComponent c = (JGVTComponent) e.getSource();
>         if (isHandle) {
> 
>             Element domE = getElementAt(c, e.getX(), e.getY());
>             SVGPoint pt = localPt(c,
> ((JSVGCanvas)c).getSVGDocument().getRootElement(), e.getX(), e.getY());
>                 xEnd = pt.getX();
>                 yEnd = pt.getY();
>             line = new Line2D.Float(xStart, yStart, xEnd, yEnd);
>         }
>         c.immediateRepaint();
>     }
> 
>     protected class LineOverlay implements Overlay {
> 
>         public void paint(Graphics g) {
>             Graphics2D g2d = (Graphics2D) g;
>             g2d.transform(at.getViewBoxTransform());
>             g2d.setXORMode(Color.white);
>             g2d.setColor(Color.red);
>             g2d.setStroke(markerStroke);
> 
>             if (line != null) {
>                 g2d.draw(line);
>             }
>             if (shape != null) {
>                 g2d.draw(shape);
>             }
>         }
>     }
> }
> 
> -- 
> View this message in context: http://www.nabble.com/getting-the-
> element-in-svg-from-an-overlay-tp18301441p18724891.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 

Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
with your help, i can draw flicker free.. thanks.. but now i am after the
efficiency.. i am currently trying the glass pane and i am having a bit of
trouble when i am panning/zooming/rotating the canvas. i cannot sync
panning/zooming/rotating of the shape together with the canvas while
dragging. the shape only updates after the mouse button is released.

cpu usage is more worse with larger canvas dimension, right? my canvas is
currently 10k pixels x 10k pixels and contains a lot of lines/paths, texts
and shapes (a map of a city). i am planning to reduce the dimension by
atleast half or use glass pane. and the question is : what would you
recommend? stick with the overlay/immediateRepaint() and reduce the
dimension of the svg or use glass pane?

here is the code of my interactor :

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.InteractorAdapter;
import org.apache.batik.swing.gvt.JGVTComponent;
import org.apache.batik.swing.gvt.Overlay;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGLocatable;
import org.w3c.dom.svg.SVGMatrix;
import org.w3c.dom.svg.SVGPoint;
import org.w3c.dom.svg.SVGRect;
import org.w3c.dom.svg.SVGSVGElement;

public class CustomInteractor extends InteractorAdapter {

    protected boolean finished = true;
    protected boolean isHandle = false;
    protected float xStart;
    protected float yStart;
    protected float xEnd;
    protected float yEnd;
    protected Line2D line;
    protected Shape shape;
    protected Overlay overlay = new LineOverlay();
    protected BasicStroke markerStroke = new BasicStroke(2,
            BasicStroke.CAP_SQUARE,
            BasicStroke.JOIN_MITER,
            5,
            new float[]{5, 5}, 0);
    protected JGVTComponent source;
    JSVGCanvas at;

    @Override
    public boolean startInteraction(InputEvent ie) {
        return true;
    }

    @Override
    public boolean endInteraction() {
        return finished;
    }

    private Point2D getCanvasCoordinate(Point2D p) {
        Point2D pt = p;
        try {
            AffineTransform bt =
source.getRenderingTransform().createInverse();
            return bt.transform(p, null);
        } catch (NoninvertibleTransformException ex) {
            return pt;
        }
    }

    private Element getElementAt(JGVTComponent source, float x, float y) {
        Point2D p = getCanvasCoordinate(new Point2D.Float(x, y));
        GraphicsNode gn = source.getGraphicsNode().nodeHitAt(p);
        BridgeContext bc =
                ((JSVGCanvas) source).getUpdateManager().getBridgeContext();
        return (Element) bc.getElement(gn);
    }

    public SVGPoint localPt(JGVTComponent c, Element el, float x, float y) {
        SVGSVGElement svgRoot = ((JSVGCanvas)
c).getSVGDocument().getRootElement();
        SVGMatrix mat = ((SVGLocatable) el).getScreenCTM();
        SVGMatrix imat = mat.inverse();
        SVGPoint cPt = svgRoot.createSVGPoint();
        cPt.setX(x);
        cPt.setY(y);
        cPt = cPt.matrixTransform(imat);
        return cPt;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        JGVTComponent c = source = (JGVTComponent) e.getSource();
        at = ((JSVGCanvas) c);
        finished = false;
        line = null;
        Element domE = getElementAt(c, e.getX(), e.getY());
        if ("handle1".equals(domE.getAttribute("id"))) {
            isHandle = true;
            xStart = Float.parseFloat(domE.getAttribute("cx"));
            yStart = Float.parseFloat(domE.getAttribute("cy"));
            SVGRect bbox = ((SVGLocatable) domE).getBBox();
            shape = new Rectangle2D.Float(bbox.getX(), bbox.getY(),
bbox.getWidth(), bbox.getHeight());
        } else {
            shape = null;
        }
        if (!c.getOverlays().contains(overlay)) {
            c.getOverlays().add(overlay);
        }
        c.immediateRepaint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        JGVTComponent c = (JGVTComponent) e.getSource();
        finished = true;
        isHandle = false;
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        JGVTComponent c = (JGVTComponent) e.getSource();
        if (isHandle) {

            Element domE = getElementAt(c, e.getX(), e.getY());
            SVGPoint pt = localPt(c,
((JSVGCanvas)c).getSVGDocument().getRootElement(), e.getX(), e.getY());
                xEnd = pt.getX();
                yEnd = pt.getY();
            line = new Line2D.Float(xStart, yStart, xEnd, yEnd);
        }
        c.immediateRepaint();
    }

    protected class LineOverlay implements Overlay {

        public void paint(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.transform(at.getViewBoxTransform());
            g2d.setXORMode(Color.white);
            g2d.setColor(Color.red);
            g2d.setStroke(markerStroke);

            if (line != null) {
                g2d.draw(line);
            }
            if (shape != null) {
                g2d.draw(shape);
            }
        }
    }
}

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18724891.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi Hardc0d3r,

hardc0d3r <ha...@gmail.com> wrote on 07/26/2008 10:03:51 AM:

> i am really confused.. can i just use glass pane and pass the events on 
the
> JSVGCanvas?

   In theory you should be able to do this.

> i want to preserve the zooming and panning functions of the
> JSVGCanvas but i am worried that the glass pane may 'block' the events..

   Sure it's a concern.  Is this still to try and fix your
flicker problem?

   If you can post a small self contained example of the
problem we might be able to fix it (or you may be able
to figure out what in your larger example is causing the
problem).

Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
i am really confused.. can i just use glass pane and pass the events on the
JSVGCanvas? i want to preserve the zooming and panning functions of the
JSVGCanvas but i am worried that the glass pane may 'block' the events..

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18667106.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi Hardc03r,

hardc0d3r <ha...@gmail.com> wrote on 07/22/2008 07:42:16 AM:

> i think that is not the problem because the overlay and interactor is
> patterned from the zoom interactor..

   Well the zoom interactor uses xor mode to draw it's indicator
so it can easily clean up it's 'path' by simply drawing it again
with xor, so as you may have noticed it doesn't tell the canvas
to repaint at all.

> i have read somewhere that canvas.immediateRepaint() can also be used 
even
> with complex svg.. what do you mean by complex? large svg? 

   Well, the canvas already 'buffers' the image so calling
immediateRepaint doesn't require rasterizing the document from 
scratch again (in most cases).  So even if the canvas is complex
it shouldn't take much time.  Additionally it should be safe
to call from threads other than the Swing thread (if it is not
in the swing thread when called it will post a runnable to the
swing thread and wait for it to complete).

   The one drawback to immediateRepaint is that there is no
version that takes a Rect so that a smaller portion of the
canvas can be repainted (for a very large canvas and small
changes, in terms of area, this could be significant).

> is it ideal to use for svg editors?

   For most SVG Editing the canvas will take care of
everything for you.  You only need to worry about repainting
stuff manually when stuff you are drawing outside of the
SVG tree changes (i.e. overlays), but this is just like any
other Swing component.

> thomas.deweese wrote:
> > 
> > Hi Hardc0d3r,
> > 
> > hardc0d3r <ha...@gmail.com> wrote on 07/21/2008 03:54:58 AM:
> > 
> >> how do i prevent the flickering? how do i do double buffering from an
> >> overlay?
> > 
> >    Do you have the Swing double buffering turned off for some
> > reason?
> > 
> >    If you do then you should turn it back on: 
> >         canvas.setDoubleBuffered(true);
> > 
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/getting-the-
> element-in-svg-from-an-overlay-tp18301441p18587219.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 

Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
i think that is not the problem because the overlay and interactor is
patterned from the zoom interactor..
i have read somewhere that canvas.immediateRepaint() can also be used even
with complex svg.. what do you mean by complex? large svg? is it ideal to
use for svg editors?


thomas.deweese wrote:
> 
> Hi Hardc0d3r,
> 
> hardc0d3r <ha...@gmail.com> wrote on 07/21/2008 03:54:58 AM:
> 
>> how do i prevent the flickering? how do i do double buffering from an
>> overlay?
> 
>    Do you have the Swing double buffering turned off for some
> reason?
> 
>    If you do then you should turn it back on: 
>         canvas.setDoubleBuffered(true);
> 
> 

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18587219.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi Hardc0d3r,

hardc0d3r <ha...@gmail.com> wrote on 07/21/2008 03:54:58 AM:

> how do i prevent the flickering? how do i do double buffering from an
> overlay?

   Do you have the Swing double buffering turned off for some
reason?

   If you do then you should turn it back on: 
        canvas.setDoubleBuffered(true);

Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
i just figured it out.. so simple.. thanks for the links.. another question..
how do i prevent the flickering? how do i do double buffering from an
overlay?
-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18563758.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
Uhm.. ok.. not the screen coordinates.. the coordinates in the visible
rect..the visible part or the panel..

thomas.deweese wrote:
> 
> Hi Hardc0d3r
> 
> hardc0d3r <ha...@gmail.com> wrote on 07/16/2008 10:43:06 AM:
> 
>> i did not quite understand.. where will i get the matrix? through the 
> element
>> that i want the coordinates converted? 
> 
>     Yes you get the screen CTM from the element who's local coordinate
> system you want to convert to screen coordinates.
> 
>> do i get the inverse of the matrix and apply it to the point? 
> 
>    I don't think you need to get the inverse of the matrix.
> 
>> i did that but i get the coordinates on the
>> canvas and not on the screen..
> 
>    Umm, are you saying that you can't figure out how to map from
> canvas pixel coordinates to screen pixel coordinates?
> 
>    Try adding the results of calling:
>         canvas.getLocationOnScreen()
> 
>    BTW it is correct (at least for SVG 1.1) for getScreenCTM to
> convert to the canvas coordinate system (same coordinate system
> as clientX/Y, not screenX/Y in a DOM Mouse Event).
> 
>> thomas.deweese wrote:
>> > 
>> > Hi HardCod3r,
>> > 
>> > hardc0d3r <ha...@gmail.com> wrote on 07/15/2008 04:09:11 AM:
>> > 
>> >> Thanks! That solved the problem! Another question.. How do i convert 
> the
>> >> coordinates of the element to the coordinates of the screen/overlay? 
>> > What
>> >> classes and/or methods do i use?
>> > 
>> >    I would suggest using the SVG DOM 'getScreenCTM' method to get
>> > the transform to the screen coordinate system.  Then you can run your
>> > points through that transform to get the screen coordinate for them.
>> > 
>> >    You need to take some care with what attributes you do this with,
>> > some attributes are in the element's local coordinate system (i.e.
>> > rect elements 'x', 'y') and some attributes 'define' the element's
>> > local coordinate system (i.e. the use element's 'x' & 'y' attributes).
>> > 
>> > 
>> > 
>> 
>> -- 
>> View this message in context: http://www.nabble.com/getting-the-
>> element-in-svg-from-an-overlay-tp18301441p18489087.html
>> Sent from the Batik - Users mailing list archive at Nabble.com.
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
>> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18507937.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi Hardc0d3r

hardc0d3r <ha...@gmail.com> wrote on 07/16/2008 10:43:06 AM:

> i did not quite understand.. where will i get the matrix? through the 
element
> that i want the coordinates converted? 

    Yes you get the screen CTM from the element who's local coordinate
system you want to convert to screen coordinates.

> do i get the inverse of the matrix and apply it to the point? 

   I don't think you need to get the inverse of the matrix.

> i did that but i get the coordinates on the
> canvas and not on the screen..

   Umm, are you saying that you can't figure out how to map from
canvas pixel coordinates to screen pixel coordinates?

   Try adding the results of calling:
        canvas.getLocationOnScreen()

   BTW it is correct (at least for SVG 1.1) for getScreenCTM to
convert to the canvas coordinate system (same coordinate system
as clientX/Y, not screenX/Y in a DOM Mouse Event).

> thomas.deweese wrote:
> > 
> > Hi HardCod3r,
> > 
> > hardc0d3r <ha...@gmail.com> wrote on 07/15/2008 04:09:11 AM:
> > 
> >> Thanks! That solved the problem! Another question.. How do i convert 
the
> >> coordinates of the element to the coordinates of the screen/overlay? 
> > What
> >> classes and/or methods do i use?
> > 
> >    I would suggest using the SVG DOM 'getScreenCTM' method to get
> > the transform to the screen coordinate system.  Then you can run your
> > points through that transform to get the screen coordinate for them.
> > 
> >    You need to take some care with what attributes you do this with,
> > some attributes are in the element's local coordinate system (i.e.
> > rect elements 'x', 'y') and some attributes 'define' the element's
> > local coordinate system (i.e. the use element's 'x' & 'y' attributes).
> > 
> > 
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/getting-the-
> element-in-svg-from-an-overlay-tp18301441p18489087.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 

Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
i did not quite understand.. where will i get the matrix? through the element
that i want the coordinates converted? do i get the inverse of the matrix
and apply it to the point? i did that but i get the coordinates on the
canvas and not on the screen..


thomas.deweese wrote:
> 
> Hi HardCod3r,
> 
> hardc0d3r <ha...@gmail.com> wrote on 07/15/2008 04:09:11 AM:
> 
>> Thanks! That solved the problem! Another question.. How do i convert the
>> coordinates of the element to the coordinates of the screen/overlay? 
> What
>> classes and/or methods do i use?
> 
>    I would suggest using the SVG DOM 'getScreenCTM' method to get
> the transform to the screen coordinate system.  Then you can run your
> points through that transform to get the screen coordinate for them.
> 
>    You need to take some care with what attributes you do this with,
> some attributes are in the element's local coordinate system (i.e.
> rect elements 'x', 'y') and some attributes 'define' the element's
> local coordinate system (i.e. the use element's 'x' & 'y' attributes).
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18489087.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi HardCod3r,

hardc0d3r <ha...@gmail.com> wrote on 07/15/2008 04:09:11 AM:

> Thanks! That solved the problem! Another question.. How do i convert the
> coordinates of the element to the coordinates of the screen/overlay? 
What
> classes and/or methods do i use?

   I would suggest using the SVG DOM 'getScreenCTM' method to get
the transform to the screen coordinate system.  Then you can run your
points through that transform to get the screen coordinate for them.

   You need to take some care with what attributes you do this with,
some attributes are in the element's local coordinate system (i.e.
rect elements 'x', 'y') and some attributes 'define' the element's
local coordinate system (i.e. the use element's 'x' & 'y' attributes).


Re: getting the element in svg from an overlay

Posted by javaNoob <no...@yahoo.com>.
this is the best that i can do.. still not plotting the points properly..
what i want to do is when the point clicked or dragged to is the right
element, i want the line to snap on the center of the element.

 (please forgive the dirty code)

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.swing.gvt.InteractorAdapter;
import org.apache.batik.swing.gvt.JGVTComponent;
import org.apache.batik.swing.gvt.Overlay;
import org.w3c.dom.svg.SVGElement;

public class CustomInteractor extends InteractorAdapter {

    protected boolean finished = true;
    protected float xStart;
    protected float yStart;
    protected float xEnd;
    protected float yEnd;
    protected Line2D line;
    protected Overlay overlay = new LineOverlay();
    protected BasicStroke markerStroke = new BasicStroke(2,
            BasicStroke.CAP_SQUARE,
            BasicStroke.JOIN_MITER,
            5,
            new float[]{5, 5}, 0);
    protected JGVTComponent source;

    @Override
    public boolean startInteraction(InputEvent ie) {
        return true;
    }

    @Override
    public boolean endInteraction() {
        return finished;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        JGVTComponent c = source = (JGVTComponent) e.getSource();
        Point2D p = new Point2D.Float(e.getX(), e.getY());

        finished = false;
        line = null;
        try {
            AffineTransform bt = c.getRenderingTransform().createInverse();
            p = bt.transform(p, null);
        } catch (NoninvertibleTransformException ex) {
        }
        GraphicsNode gn = c.getGraphicsNode().nodeHitAt(p);
        BridgeContext bc =
                ((JSVGCanvas) c).getUpdateManager().getBridgeContext();
        SVGElement domE = (SVGElement) bc.getElement(gn);

        if ("handle1".equals(domE.getAttribute("id"))) {
            p.setLocation(Float.parseFloat(domE.getAttribute("cx")),
                    Float.parseFloat(domE.getAttribute("cy")));
            AffineTransform bt = c.getRenderingTransform();
            p = bt.transform(p, null);
            xStart = (float) p.getX();
            yStart = (float) p.getY();
        } else {
            xStart = e.getX();
            yStart = e.getY();
        }
        c.getOverlays().add(overlay);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        JGVTComponent c = (JGVTComponent) e.getSource();
        finished = true;
        c.getOverlays().remove(overlay);
        overlay.paint(c.getGraphics());

        xEnd = e.getX();
        yEnd = e.getY();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        JGVTComponent c = (JGVTComponent) e.getSource();
        Point2D p = new Point2D.Float(e.getX(), e.getY());
        
        overlay.paint(c.getGraphics());
        try {
            AffineTransform bt = c.getRenderingTransform().createInverse();
            p = bt.transform(p, null);
        } catch (NoninvertibleTransformException ex) {
        }
        GraphicsNode gn = c.getGraphicsNode().nodeHitAt(p);
        BridgeContext bc =
                ((JSVGCanvas) c).getUpdateManager().getBridgeContext();
        SVGElement domE = (SVGElement) bc.getElement(gn);
        if ("handle2".equals(domE.getAttribute("id"))) {
            p.setLocation(Float.parseFloat(domE.getAttribute("cx")),
                    Float.parseFloat(domE.getAttribute("cy")));
            AffineTransform bt = c.getRenderingTransform();
            p = bt.transform(p, null);
            xEnd = (float) p.getX();
            yEnd = (float) p.getY();
        } else {
            xEnd = e.getX();
            yEnd = e.getY();
        }
        line = new Line2D.Float(xStart, yStart, xEnd, yEnd);
        overlay.paint(c.getGraphics());
    }

    protected class LineOverlay implements Overlay {

        public void paint(Graphics g) {
            if (line != null) {
                Graphics2D g2d = (Graphics2D) g;

                g2d.setXORMode(Color.white);
                g2d.setColor(Color.black);
                g2d.setStroke(markerStroke);

                g2d.draw(line);
            }
        }
    }
}
-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18466057.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
Thanks! That solved the problem! Another question.. How do i convert the
coordinates of the element to the coordinates of the screen/overlay? What
classes and/or methods do i use?
-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18460034.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi Hardcod3r,

hardc0d3r <ha...@gmail.com> wrote on 07/13/2008 09:44:24 PM:

>         JGVTComponent c = (JGVTComponent) e.getSource();
>         Point2D p = new Point2D.Float(e.getX(), e.getY());
>         try {
>             AffineTransform bt = 
c.getRenderingTransform().createInverse();
>             p = bt.transform(p, null);
>         } catch(Exception ex) {
>         }
>         GraphicsNode gn = c.getGraphicsNode().nodeHitAt(p);

This is the problem:

>         UserAgentAdapter userAgent = new UserAgentAdapter();
>         BridgeContext bc = new BridgeContext(userAgent);

    You need to get the BridgeContext from the Canvas.  The
simplest way to get it is with:

            BridgeContext bc = c.getUpdateManager().getBridgeContext();

>         SVGElement domE = (SVGElement) bc.getElement(gn);
>         System.out.println(domE == null);
> 
> what am i doing wrong here?
> 
> 
> thomas.deweese wrote:
> > 
> > Hi Hardc0d3r,
> > 
> > hardc0d3r <ha...@gmail.com> wrote on 07/12/2008 09:27:51 PM:
> > 
> >> is this possible? i can paint on the overlay just fine.. i just need 
to 
> > know
> >> if it is possible 2 get the element under the clicked point in the 
> > overlay..
> > 
> >    MouseEvent evt;
> > 
> >    Point2D p = new Point2D.Float(evt.getX(), evt.getY());
> >    AffineTransform bt = 
canvas.getRenderingTransform().createInverse();
> >    p = bt.transform(p);
> >    GraphicsNode gn = canvas.getGraphicsNode().nodeHitAt(gnp);
> >    Element domE  = bridgeCtx.getElement(gn);
> > 
> >    Note that the RenderingTransform can be null so you should
> > check for that (and in that case you can simply use 'p' as is.
> > 
> >> hardc0d3r wrote:
> >> > 
> >> > ok.. let me rephrase the question.. how can i get an element in the 

> > svg if
> >> > there is an overlay? for example, when i clicked on an element but 
> > there's
> >> > an overlay, how can i get the element below the point where i 
clicked?
> >> > 
> >> 
> >> -- 
> >> View this message in context: http://www.nabble.com/getting-the-
> >> element-in-svg-from-an-overlay-tp18301441p18425405.html
> >> Sent from the Batik - Users mailing list archive at Nabble.com.
> >> 
> >> 
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: 
batik-users-unsubscribe@xmlgraphics.apache.org
> >> For additional commands, e-mail: 
batik-users-help@xmlgraphics.apache.org
> >> 
> > 
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/getting-the-
> element-in-svg-from-an-overlay-tp18301441p18436175.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 

Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
i tried what you said but i get a null.. her is a snippet.. this code is
placed on a mousePressed in an interactor:

        JGVTComponent c = (JGVTComponent) e.getSource();
        Point2D p = new Point2D.Float(e.getX(), e.getY());
        try {
            AffineTransform bt = c.getRenderingTransform().createInverse();
            p = bt.transform(p, null);
        } catch(Exception ex) {
        }
        GraphicsNode gn = c.getGraphicsNode().nodeHitAt(p);
        UserAgentAdapter userAgent = new UserAgentAdapter();
        BridgeContext bc = new BridgeContext(userAgent);
        SVGElement domE = (SVGElement) bc.getElement(gn);
        System.out.println(domE == null);

what am i doing wrong here?


thomas.deweese wrote:
> 
> Hi Hardc0d3r,
> 
> hardc0d3r <ha...@gmail.com> wrote on 07/12/2008 09:27:51 PM:
> 
>> is this possible? i can paint on the overlay just fine.. i just need to 
> know
>> if it is possible 2 get the element under the clicked point in the 
> overlay..
> 
>    MouseEvent evt;
> 
>    Point2D p = new Point2D.Float(evt.getX(), evt.getY());
>    AffineTransform bt = canvas.getRenderingTransform().createInverse();
>    p = bt.transform(p);
>    GraphicsNode gn = canvas.getGraphicsNode().nodeHitAt(gnp);
>    Element domE  = bridgeCtx.getElement(gn);
> 
>    Note that the RenderingTransform can be null so you should
> check for that (and in that case you can simply use 'p' as is.
> 
>> hardc0d3r wrote:
>> > 
>> > ok.. let me rephrase the question.. how can i get an element in the 
> svg if
>> > there is an overlay? for example, when i clicked on an element but 
> there's
>> > an overlay, how can i get the element below the point where i clicked?
>> > 
>> 
>> -- 
>> View this message in context: http://www.nabble.com/getting-the-
>> element-in-svg-from-an-overlay-tp18301441p18425405.html
>> Sent from the Batik - Users mailing list archive at Nabble.com.
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
>> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18436175.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by th...@kodak.com.
Hi Hardc0d3r,

hardc0d3r <ha...@gmail.com> wrote on 07/12/2008 09:27:51 PM:

> is this possible? i can paint on the overlay just fine.. i just need to 
know
> if it is possible 2 get the element under the clicked point in the 
overlay..

   MouseEvent evt;

   Point2D p = new Point2D.Float(evt.getX(), evt.getY());
   AffineTransform bt = canvas.getRenderingTransform().createInverse();
   p = bt.transform(p);
   GraphicsNode gn = canvas.getGraphicsNode().nodeHitAt(gnp);
   Element domE  = bridgeCtx.getElement(gn);

   Note that the RenderingTransform can be null so you should
check for that (and in that case you can simply use 'p' as is.

> hardc0d3r wrote:
> > 
> > ok.. let me rephrase the question.. how can i get an element in the 
svg if
> > there is an overlay? for example, when i clicked on an element but 
there's
> > an overlay, how can i get the element below the point where i clicked?
> > 
> 
> -- 
> View this message in context: http://www.nabble.com/getting-the-
> element-in-svg-from-an-overlay-tp18301441p18425405.html
> Sent from the Batik - Users mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 

Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
is this possible? i can paint on the overlay just fine.. i just need to know
if it is possible 2 get the element under the clicked point in the overlay..

hardc0d3r wrote:
> 
> ok.. let me rephrase the question.. how can i get an element in the svg if
> there is an overlay? for example, when i clicked on an element but there's
> an overlay, how can i get the element below the point where i clicked?
> 

-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18425405.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: getting the element in svg from an overlay

Posted by hardc0d3r <ha...@gmail.com>.
ok.. let me rephrase the question.. how can i get an element in the svg if
there is an overlay? for example, when i clicked on an element but there's
an overlay, how can i get the element below the point where i clicked?
-- 
View this message in context: http://www.nabble.com/getting-the-element-in-svg-from-an-overlay-tp18301441p18396788.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org