Hello Cojocar,

Thank you for taking the time to reply and write an example. However, I
don't think I explained myself very well.  As I understand it, the code
you posted uses the Java AWT mouse listener directly, rather than using
the SVG DOM event listener.   Therefore the co-ordinates retrieved using
e.getX() and e.getY() are the screen co-ordinates (relative to the
svgCanvas), rather than the SVG client co-ordinates.  Therefore, when
they are used to draw on the svgCanvas, the circle does not appear where
I clicked (testing your code in my application, the circles appear about
20 px up and to the left of where I clicked, due to a translate on the
document).  I need to be able to zoom, pan etc my SVG document and still
have the circles appear in the correct place. This is why I thought I
should be using the DOM event listener, as I could then get the
co-ordinates of the mouse click in terms of the SVG document client
co-ordinate system, and hence draw in the correct place. Please correct
me if I am wrong.

Thanks again for your help,
Matthew.

On Mon, 2008-01-21 at 20:24 +0200, Cojocar Cosmin wrote:
> Hello Matthew,
> 
> I think that you didn't put a listener in the correct/normal use.
> I will give you an small example how to put a circle on a canvas on
> mouse clicks.
> 
> Code:
> =====
> 
> //We are going to react on mouse clicks, so the appropriate listener
> is added
> svgCanvas.addMouseListener(this);
> 
> // The following line is needed for our canvas to react on every
> change of the document    
> // associated with it
> svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
> 
> ............
> 
> public void mouseClicked(MouseEvent e) {
>               addBall(e.getX(), e.getY());       
> }
> 
> private void addBall(int x, int y) {
>     // The code is put into a Runnable object
>         final int a = x; 
>         final int b = y;
>         Runnable r = new Runnable() {
>             public void run() {               
>                 Element root =
> canvas.getSVGDocument().getDocumentElement();
>                 doc = (SVGDocument) canvas.getSVGDocument();
>                 // A new ball is created
>                 Element circle = doc.createElementNS(svgNS,
> "circle");               
>                 circle.setAttributeNS(null, "stroke", "black"); 
>                 circle.setAttributeNS(null, "stroke-width", "1");
>                 circle.setAttributeNS(null, "r", "" + 5);
>                 circle.setAttributeNS(null, "cx", "" + a); 
>                 circle.setAttributeNS(null, "cy", "" + b);
>                 
>                 root.appendChild(circle);
>             }
>         };
>         // Running our code in the UpdateManager thread 
>         UpdateManager um = canvas.getUpdateManager();
>         um.getUpdateRunnableQueue().invokeLater(r);
>     }
> 
> Pay attention on what you "listen" and how you update your canvas and
> I think that you will get it. 
> 
> PS: Please excuse my English.
> 
> Regards,
> 
> 
> On Jan 20, 2008 11:47 PM, Matthew Wilson <[EMAIL PROTECTED]>
> wrote:
>         Hi,
>         I have written a Swing component which wraps JSVGCanvas, and I
>         would 
>         like to capture DOM mouse click events so that I can draw on
>         the canvas
>         where the user has clicked. So I register and define my event
>         listener
>         like this:
>         
>         public class PlanViewPanel extends JPanel implements
>         MouseWheelListener, 
>         EventListener, SVGLoadEventDispatcherListener {
>         
>         ....... stuff ......
>         
>                public PlanViewPanel(SVGDocument svg) {
>                svgCanvas.setDocument(svg);
>                   svgCanvas.addMouseWheelListener(this); // this is
>         for 
>         something else entirely
>                  scrollPane = new JSVGScrollPane(svgCanvas);
>                scrollPane.setScrollbarsAlwaysVisible(true);
>         
>         
> scrollPane.setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize()); 
>                add(scrollPane);
>                setMinimumSize(new Dimension(800,600));
>         
>         svgCanvas.getSVGDocument().getRootElement().addEventListener("click",
>         this, false);
>                }
>         
>                     .... stuff .... 
>                public void handleEvent(org.w3c.dom.events.Event evt) {
>                        System.out.println("did something");
>         
>                }
>         
>         }
>         
>         but my event handler is never called (I've placed a breakpoint
>         and run 
>         it in the eclipse debugger to confirm this).  I have confirmed
>         also
>         using a debugger that the event handler is present in the
>         hashtable in
>         the document root's EventSupport structure, but I do not
>         understand why 
>         it is not called. I have messed around with various different
>         event
>         types and tried bubble/capture, but nothing works.
>         
>         Using JDK 1.6.0 and Batik 1.7.
>         
>         Any suggestions would be greatly appreciated.
>         
>         Matthew Wilson.
>         
>         
>         
>         ---------------------------------------------------------------------
>         To unsubscribe, e-mail:
>         [EMAIL PROTECTED]
>         For additional commands, e-mail:
>         [EMAIL PROTECTED]
>         
> 


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

Reply via email to