I recreated a simplified setup of my problem, based on the basic JSVGCanvas sample on the Batik web page, and added the Java Scripting code. Basically i have a SVG definition that loads four other SVG´s. and a PNG, and activates the proper mouse listeners. A label displays the result of the actions.
The Actions get triggered, for the 4 SVG files that belong to the 'imgGroup' (see below) are only OVER and CLICK.
The Actions triggered for the PNG, that also belong to the 'imgGroup' are OVER, MOVE, DRAG and CLICK.
I do not get any response out of UP or DOWN actions.
The 1.svg, 2.svg, 3.svg and 4.svg files are plain vector illustrations created by exporting SVG from Adobe Illustrator,
Following is the SVG and JAVA files that i am using.
Thanks in advance.
---------- Start of SVG file:
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="500" height="200" viewBox="0 0 500 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" >
<g id="imgGroup"> <image x="20" y="20" width="100" height="100" xlink:href="1.svg"/> <image x="150" y="20" width="100" height="100" xlink:href="2.svg"/> <image x="260" y="20" width="100" height="100" xlink:href="3.svg"/> <image x="370" y="20" width="100" height="100" xlink:href="4.svg"/> <image x="200" y="120" width="100" height="80" xlink:href="1.png"/> </g> </svg> ---------- End of SVG file
---------- Start of JAVA file: import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*;
import org.apache.batik.swing.JSVGCanvas; import org.apache.batik.swing.gvt.GVTTreeRendererAdapter; import org.apache.batik.swing.gvt.GVTTreeRendererEvent; import org.apache.batik.swing.svg.SVGDocumentLoaderAdapter; import org.apache.batik.swing.svg.SVGDocumentLoaderEvent; import org.apache.batik.swing.svg.GVTTreeBuilderAdapter; import org.apache.batik.swing.svg.GVTTreeBuilderEvent; import org.apache.batik.swing.svg.SVGLoadEventDispatcherAdapter; import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.events.EventTarget; import org.w3c.dom.events.EventListener; import org.w3c.dom.events.Event; import org.apache.batik.script.Window;
public class batikDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Batik");
batikDemo app = new batikDemo(f);
f.getContentPane().add(app.createComponents());
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setSize(400, 400);
f.setVisible(true);
}
JFrame frame;
JButton button = new JButton("Load...");
JLabel label = new JLabel();
JSVGCanvas svgCanvas = new JSVGCanvas();
Document document;
Window window;
public batikDemo(JFrame f) {
frame = f;
}
public JComponent createComponents() { final JPanel panel = new JPanel(new BorderLayout());
JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT)); p.add(button); p.add(label);
panel.add("North", p); panel.add("Center", svgCanvas);
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svgCanvas.addSVGLoadEventDispatcherListener
(new SVGLoadEventDispatcherAdapter() {
public void svgLoadEventDispatchStarted
(SVGLoadEventDispatcherEvent e) {
// At this time the document is available...
document = svgCanvas.getSVGDocument();
// ...and the window object too.
window = svgCanvas.getUpdateManager().
getScriptingEnvironment().createWindow();
// Registers the listeners on the document
// just before the SVGLoad event is
// dispatched.
registerListeners();
// It is time to pack the frame.
frame.pack();
}
});
// Set the button action.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fc = new JFileChooser(".");
int choice = fc.showOpenDialog(panel);
if (choice == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
try {
svgCanvas.setURI(f.toURL().toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
// Set the JSVGCanvas listeners.
svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
label.setText("Document Loading...");
}
public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
label.setText("Document Loaded.");
}
});
svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() { public void gvtBuildStarted(GVTTreeBuilderEvent e) { label.setText("Build Started..."); } public void gvtBuildCompleted(GVTTreeBuilderEvent e) { label.setText("Build Done."); frame.pack(); } });
svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() { public void gvtRenderingPrepare(GVTTreeRendererEvent e) { label.setText("Rendering Started..."); } public void gvtRenderingCompleted(GVTTreeRendererEvent e) { label.setText(""); } });
return panel;
}
//Items added for action detection
public void registerListeners() {
// Gets an element from the loaded document.
Element elt = document.getElementById("imgGroup");
EventTarget t = (EventTarget)elt;
// Adds a 'onclick' listener
t.addEventListener("click", new OnClickAction(), false);
// Adds a 'mousedown' listener
t.addEventListener("mousedown", new OnDownAction(), false);
// Adds a 'mouseup' listener
t.addEventListener("mouseup", new OnUpAction(), false);
// Adds a 'mouseover' listener
t.addEventListener("mouseover", new OnOverAction(), false);
// Adds a 'mousemove' listener
t.addEventListener("mousemove", new OnMouseMoveAction(), false);
// Adds a 'drag' listener
t.addEventListener("drag", new OnDragAction(), false);
}
public class OnClickAction implements EventListener { public void handleEvent(Event evt) { label.setText("Click: "+evt.getTarget()); System.out.println("Click: "+evt.getTarget()); } } public class OnDownAction implements EventListener { public void handleEvent(Event evt) { label.setText("Down: "+evt.getTarget()); } } public class OnUpAction implements EventListener { public void handleEvent(Event evt) { label.setText("Up: "+evt.getTarget()); } } public class OnOverAction implements EventListener { public void handleEvent(Event evt) { label.setText("Over: "+evt.getTarget()); } } public class OnDragAction implements EventListener { public void handleEvent(Event evt) { label.setText("Drag: "+evt.getTarget()); } } public class OnMouseMoveAction implements EventListener { public void handleEvent(Event evt) { label.setText("MouseMove: "+evt.getTarget()); } } } ---------- End of JAVA file Thomas DeWeese wrote:
Hi Andres,
One other thing it's not mousedrag it's mousemove or <rect onmousemove="foo(evt)" .../>
SVG (DOM Events really) doesn't define 'drag' events. It's up to you to track onmousedown, onmousemove, onmouseup.
Thomas DeWeese wrote:
Andres Toussaint wrote:
Hi all,
I am trying to create a drag SVG component within my JSVGCanvas, i have created and registered the listeners. My SVG has embedded SVG´s it reads from external files and PNG´s. I register each external item as a SVG component, when loading.
The EventListener gives me a proper event in the SVG components for the CLICK and OVER events, but no response for the DRAG, UP or DOWN events.
The PNG file responds to the CLICK, OVER, DOWN and DRAG events.
Basically what i want to accomplish is to drag inside the JSVGCanvas the individual SVG components. I have managed to accomplish dragging PNG´s only using ecmascript scripting functions in the SVG, but I need to be able to drag SVG´s also.
I am using j2SDK1.4.1_01, with Batik 1.5 on a windows 2000 professional system.
Any help or guidance will be trully appreciated.
Can you provide a reproducable test case? I drag all sorts of things all the time.
--------------------------------------------------------------------- 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]