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 Mikhail Gerdov <ge...@gmail.com> on 2009/02/25 20:03:29 UTC

How to detect mouse clicks on SVG elements.

Hi, I am trying to load SVG document created in third party
application. Display it in my Java application, and detect mouse
clicks on different svg elements.
I am new to batik so probably I am doing something wrong. Here is my code:

		String svgUri = new File("data/test.svg").toURI().toString();

		JSVGCanvas svg = new JSVGCanvas();
		try
		{
			String parser = XMLResourceDescriptor.getXMLParserClassName();
			SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
			SVGDocument doc = (SVGDocument)f.createDocument(svgUri);


			EventTarget node = (EventTarget) doc.getElementById("rect1");  //
just for test, such element exists.
			if(node != null)
			{
				node.addEventListener("click", new EventListener()
				{
					@Override
					public void handleEvent(Event evt)
					{
						int x = ((DOMMouseEvent) evt).getClientX();
						int y = ((DOMMouseEvent) evt).getClientY();
						JOptionPane.showMessageDialog(GamingInterfaceClient.this,
"Click: " + x + "," + y);
					}
				}, false);
			}
			
			svg.setSVGDocument(doc);
		}
		catch(IOException ex)
		{
			// ...
		}

then I just display JSVGCanvas svg inside a panel on JFrame.

The problem is that mouse clicks does not get detected, no matter where I click.

Any code examples how to make that work?

Thanks,
Mike

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


Re: How to detect mouse clicks on SVG elements.

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

Mikhail Gerdov <ge...@gmail.com> wrote on 02/26/2009 07:33:28 AM:

> Hi, I am trying to load SVG document created in third party
> application. Display it in my Java application, and detect mouse
> clicks on different svg elements.
> I am new to batik so probably I am doing something wrong. Here is my 
code:
> 
>                String svgUri = new 
File("data/test.svg").toURI().toString();
> 
>                JSVGCanvas svg = new JSVGCanvas();

   I suspect you just need to set the canvas state to ALWAYS_DYNAMIC.

>                try
>                {
>                        String parser =
> XMLResourceDescriptor.getXMLParserClassName();
>                        SAXSVGDocumentFactory f = new
> SAXSVGDocumentFactory(parser);
>                        SVGDocument doc = (SVGDocument)f.
> createDocument(svgUri);
> 
> 
>                        EventTarget node = (EventTarget)
> doc.getElementById("rect1");  //

[...]

>                        svg.setSVGDocument(doc);
>                }

   Also In some cases the Canvas will clone your document (if
the DOM implementation isn't compatible).  I don't think this
is your problem.  If it were you would need to register an on load,
event handler (with the canvas) and add your event handler there.

How to detect mouse clicks on SVG elements.

Posted by Mikhail Gerdov <ge...@gmail.com>.
Hi, I am trying to load SVG document created in third party
application. Display it in my Java application, and detect mouse
clicks on different svg elements.
I am new to batik so probably I am doing something wrong. Here is my code:

               String svgUri = new File("data/test.svg").toURI().toString();

               JSVGCanvas svg = new JSVGCanvas();
               try
               {
                       String parser =
XMLResourceDescriptor.getXMLParserClassName();
                       SAXSVGDocumentFactory f = new
SAXSVGDocumentFactory(parser);
                       SVGDocument doc = (SVGDocument)f.createDocument(svgUri);


                       EventTarget node = (EventTarget)
doc.getElementById("rect1");  //
just for test, such element exists.
                       if(node != null)
                       {
                               node.addEventListener("click", new
EventListener()
                               {
                                       @Override
                                       public void handleEvent(Event evt)
                                       {
                                               int x =
((DOMMouseEvent) evt).getClientX();
                                               int y =
((DOMMouseEvent) evt).getClientY();

JOptionPane.showMessageDialog(GamingInterfaceClient.this,
"Click: " + x + "," + y);
                                       }
                               }, false);
                       }

                       svg.setSVGDocument(doc);
               }
               catch(IOException ex)
               {
                       // ...
               }

then I just display JSVGCanvas svg inside a panel on JFrame.

The problem is that mouse clicks does not get detected, no matter where I click.

Any code examples how to make that work?


-- 

Thanks,
Mike                                    mailto:gerdov@gmail.com