Hi Thomas,

>    At this point you will either have to provide a complete
> standalone example (including your Java app), or debug the problem
> yourself (you have the source to Batik so you can either step through
> or add System.out to track where and how it goes wrong).

i have attached my de.svg and SVGApplication5.java for your reference. Please 
let me know the problem.


de.svg

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-
SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"; 
xmlns:xlink="http://www.w3.org/1999/xlink"; width="450" height="500" viewBox="0 
0 450 500">
<defs>
<linearGradient id="MyGradient">
<stop offset="5%" stop-color="#F60" />
<stop offset="95%" stop-color="#FF6" />
</linearGradient>
</defs>
<rect id="chessboard" x="25" y="25" width="400" height="400" fill="url
(#MyGradient)"/>
</svg>


SVGApplication5.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.w3c.dom.*;
import org.w3c.dom.svg.*;
import javax.swing.*;
import java.awt.*;
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.dom.svg.SVGDOMImplementation;
import org.apache.batik.bridge.UpdateManager;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.apache.batik.swing.gvt.Interactor;
import org.apache.batik.swing.gvt.AbstractZoomInteractor;
import org.apache.batik.swing.*;
import org.apache.batik.dom.svg.*;
import org.apache.batik.dom.events.*;

public class SVGApplication5 {

    static String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    public static final String XLINK_NAMESPACE_URI 
="http://www.w3.org/1999/xlink";;

    JSVGScrollPane scroll;
    static JSVGCanvas svgCanvas = new JSVGCanvas();
    JFrame frame;
    JLabel label = new JLabel();

    String SCALE = "scale";
    String DRAG = "drag";
    String action;
    EventTarget actionNode, actionTgt;
    SVGPoint startPt;
    boolean dragged = false;
    
    Action zoomInAction = svgCanvas.getActionMap().get
(JSVGCanvas.ZOOM_IN_ACTION);
    Action zoomOutAction = svgCanvas.getActionMap().get
(JSVGCanvas.ZOOM_OUT_ACTION);
    JButton zoomInButton = new JButton(zoomInAction);
    JButton zoomOutButton = new JButton(zoomOutAction);

    protected Interactor zoomInteractor = new AbstractZoomInteractor() {
        public boolean startInteraction(InputEvent ie) {
            int mods = ie.getModifiers();
            boolean b = ie.getID() == MouseEvent.MOUSE_PRESSED && (mods & 
InputEvent.CTRL_MASK) != 0;
            return b;
        }
    };

    public static void main(String[] args) {
        SVGApplication5 app = new SVGApplication5();
    }

    public SVGApplication5() {
        JFrame f = new JFrame();
        f.getContentPane().add(createComponents());
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        f.setSize(600, 400);
        f.setVisible(true);
    }

    public JComponent createComponents() {
        final JPanel panel = new JPanel(new BorderLayout());
        JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));

        zoomInButton.setText("ZoomIn");
        p.add(zoomInButton);
        p.add(label);
        p.add(zoomOutButton);
        zoomOutButton.setText("ZoomOut");

//        scroll = new JSVGScrollPane(svgCanvas);

        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
        SVGDocument doc = (SVGDocument)impl.createDocument(svgNS, "svg", null);

        panel.add("North", p);
        panel.add("Center", svgCanvas);
        svgCanvas.setDocument(doc);
        registerListeners();
        return panel;
    }

    public void registerListeners() {
        svgCanvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
            public void documentLoadingStarted(SVGDocumentLoaderEvent e) {
                System.out.println("Document Loading...");
            }
            public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                System.out.println("Document Loaded.");
            }
        });
        svgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            public void gvtBuildStarted(GVTTreeBuilderEvent e) {
                System.out.println("gvt bUILD Started...");
            }
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
                System.out.println("GVT Build Done...");
                frame.pack();
            }
        });
        svgCanvas.addGVTTreeRendererListener(new GVTTreeRendererAdapter() {
            public void gvtRenderingPrepare(GVTTreeRendererEvent e) {
                System.out.println("Renderer Started...");
            }
            public void gvtRenderingCompleted(GVTTreeRendererEvent e) {
                System.out.println("Renderer Completed...");
                
                UpdateManager um = svgCanvas.getUpdateManager();
                um.getUpdateRunnableQueue().invokeLater(new Runnable() {
                    public void run() {
                        resizeCircle();
                    }
                });
            }
        });

        SVGDocument doc = svgCanvas.getSVGDocument();

        SVGSVGElement svgRoot = doc.getRootElement();
        EventTarget t = (EventTarget)svgRoot;
        t.addEventListener("SVGLoad", new OnLoadAction(), false);
    }

    public class OnLoadAction implements EventListener {
        public void handleEvent(Event evt) {
        }
    }

    private void resizeCircle() throws DOMException {
        try {
            SVGDocument doc = svgCanvas.getSVGDocument();
            SVGSVGElement svgRoot = doc.getRootElement();

            Element rect = doc.createElementNS(svgNS, "rect");
            rect.setAttributeNS(null, "id", "bgrectangle");
            rect.setAttributeNS(null, "x", "0");
            rect.setAttributeNS(null, "y", "0");
            rect.setAttributeNS(null, "width", "100%");
            rect.setAttributeNS(null, "height", "100%");
            rect.setAttributeNS(null, "pointer-events", "fill");
            rect.setAttributeNS(null, "style", "fill:none;stroke:none");

            Element g1 = doc.createElementNS(svgNS, "g");
            g1.setAttributeNS(null, "id", "group1");
            Element use1 = doc.createElementNS(svgNS, "rect");
            use1.setAttributeNS(null, "x", "5");
            use1.setAttributeNS(null, "y", "5");
            use1.setAttributeNS(null, "id", "r1");
            use1.setAttributeNS(null, "width", "13");
            use1.setAttributeNS(null, "height", "13");
            use1.setAttributeNS(null, "style", "fill:red; stroke:black");
            g1.appendChild(use1);

            Element g2 = doc.createElementNS(svgNS, "g");
            g2.setAttributeNS(null, "id", "group2");

            Element use2 = doc.createElementNS(svgNS, "rect");
            use2.setAttributeNS(null, "x", "30");
            use2.setAttributeNS(null, "y", "30");
            use2.setAttributeNS(null, "id", "r2");
            use2.setAttributeNS(null, "width", "13");
            use2.setAttributeNS(null, "height", "13");
            use2.setAttributeNS(null, "style", "fill:url
(file:///C:/de.svg#MyGradient)");

            g2.appendChild(use2);

            svgRoot.appendChild(rect);
            svgRoot.appendChild(g1);
            svgRoot.appendChild(g2);
            
            EventTarget t = (EventTarget)svgRoot;
            t.addEventListener("mousemove", new OnMoveAction(), false);
            t.addEventListener("mousedown", new OnDownAction(), false);
            t.addEventListener("mouseup", new OnUpAction(), false);
        } catch (Exception e) {}
    }

   

    private SVGPoint localPt(Element elem, int x, int y) {
        SVGDocument svgDocument = svgCanvas.getSVGDocument();
        SVGMatrix mat = ((SVGLocatable)elem).getScreenCTM();
        SVGMatrix imat = mat.inverse();
        SVGPoint cPt = svgDocument.getRootElement().createSVGPoint();
        cPt.setX(x);
        cPt.setY(y);
        cPt = cPt.matrixTransform(imat);
        return cPt;
    }


    private class OnDownAction implements EventListener {
        public void handleEvent(Event evt) {
            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
            actionNode = elEvt.getTarget();
            action = DRAG;
            Node n = ((Element)elEvt.getTarget()).getParentNode();
            startPt = localPt((Element)n, elEvt.getClientX(), elEvt.getClientY
());
            dragged = false;
        }
    }

    private class OnUpAction implements EventListener {
        public void handleEvent(Event evt) {
            if (actionNode != null) {
                actionNode = null;
            }
        }
    }

    private class OnMoveAction implements EventListener {
        public void handleEvent(Event evt) {
            SVGDocument svgDocument = svgCanvas.getSVGDocument();
            if (actionNode == null)return;
            dragged = true;
            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
            if (action == DRAG) {
                Element ee = (Element)((Element)actionNode).getParentNode();
                if (ee.getParentNode() != null && ee.getParentNode() 
instanceof Element) {
                    SVGPoint pt = localPt((Element)ee.getParentNode(), 
elEvt.getClientX(), elEvt.getClientY());
                    float dx = pt.getX() - startPt.getX();
                    float dy = pt.getY() - startPt.getY();
                    ee.setAttribute("transform", "translate(" + dx + ", " + dy 
+ ")");
                }
            }
        }
    }

}


Thanks,
Selva



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

Reply via email to