xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sudhakar S <yessudha...@hotmail.com>
Subject Re: Multiple Group Elements Selection and Moving
Date Wed, 21 Jun 2006 13:05:18 GMT

Hi Tonny Kohar,

As per your code snippet i tried to move the rectangle element by setting x
and y attributes. This is working fine without "zoom in" the document.  But
after zoomed in, if i i tried to move, it is not moving properly. I dont
know why it's happening like this. Please explain why it's happening like
this? 

Please find the attached code so that you can understand the problem. 

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.*;
import org.apache.batik.dom.svg.*;
import org.apache.batik.dom.events.*;
import java.util.Hashtable;
import java.util.Enumeration;

public class SVGApplication12 {
   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);
   
    static JSVGCanvas svgCanvas = new JSVGCanvas();
    Hashtable selElements = new Hashtable();
    Hashtable offsetMap = new Hashtable();
    JFrame frame;
    String DRAG = "drag";
    String action;
    EventTarget actionNode;
    SVGPoint startPt;
    Element selElement = null;

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

    public SVGApplication12() {
        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));
        svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        DOMImplementation impl =
SVGDOMImplementation.getDOMImplementation();
        SVGDocument doc =
(SVGDocument)impl.createDocument(SVGDOMImplementation.SVG_NAMESPACE_URI,
"svg", null);

        p.add(zoomInButton);
        zoomInButton.setText("ZoomIn");
        p.add(zoomOutButton);
        zoomOutButton.setText("ZoomOut");   
        panel.add("North", p);
        JSVGScrollPane scroll = new JSVGScrollPane(svgCanvas);
        panel.add("Center", scroll);
        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(SVGDOMImplementation.SVG_NAMESPACE_URI, "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");
            rect.setAttributeNS(null, "stroke", "darkblue");
            rect.setAttributeNS(null, "stroke-width", "1");
            doc.getRootElement().appendChild(rect);

            Element rect1 =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect1.setAttributeNS(null, "id", "rec");
            rect1.setAttributeNS(null, "x", "100");
            rect1.setAttributeNS(null, "y", "100");
            rect1.setAttributeNS(null, "width", "20");
            rect1.setAttributeNS(null, "height", "20");
            rect1.setAttributeNS(null, "style", "fill:red; stroke:black");
            doc.getRootElement().appendChild(rect1);

            Element rect2 =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect2.setAttributeNS(null, "id", "rec");
            rect2.setAttributeNS(null, "x", "150");
            rect2.setAttributeNS(null, "y", "150");
            rect2.setAttributeNS(null, "width", "20");
            rect2.setAttributeNS(null, "height", "20");
            rect2.setAttributeNS(null, "style", "fill:red; stroke:black");
            doc.getRootElement().appendChild(rect2);

            Element rect3 =
doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "rect");
            rect3.setAttributeNS(null, "id", "rec");
            rect3.setAttributeNS(null, "x", "200");
            rect3.setAttributeNS(null, "y", "200");
            rect3.setAttributeNS(null, "width", "20");
            rect3.setAttributeNS(null, "height", "20");
            rect3.setAttributeNS(null, "style", "fill:red; stroke:black");
            doc.getRootElement().appendChild(rect3);

            EventTarget t = (EventTarget)svgRoot;
            t.addEventListener("mousemove", new OnMoveAction(), false);
            t.addEventListener("mousedown", new OnDownAction(), false);
            t.addEventListener("mouseup", new OnUpAction(), false);

            EventTarget t1 = (EventTarget)rect;
            t1.addEventListener("mousemove", new OnMoveAction(), false);
            t1.addEventListener("mousedown", new OnUpAction(), false);
            t1.addEventListener("mouseup", new OnUpAction(), false);

        } catch (Exception e) {}
    }

   private class OnDownAction implements EventListener {

       public void handleEvent(Event evt) {
           DOMMouseEvent elEvt = (DOMMouseEvent)evt;
           action = DRAG;
           selElement = (Element)elEvt.getTarget();
           actionNode = elEvt.getTarget();
           
           if(selElement.getAttribute("id").equals("bgrectangle")) {
               selElements = new Hashtable();
               return;
           }           
           
           if (elEvt.getCtrlKey()) {
               selElements.put(selElement, selElement);
           } else {
               selElements = new Hashtable();
               selElements.put(selElement, selElement);
           }
       
           SVGRectElement svgElement = (SVGRectElement)selElement;
           SVGLength lengthX = svgElement.getX().getBaseVal();
           SVGLength lengthY = svgElement.getY().getBaseVal();
           
           int cXPos = (int)lengthX.getValue();
           int cYPos = (int)lengthY.getValue();
           
           Enumeration enu = selElements.keys();

            while (enu.hasMoreElements()) {
                Element ele = (Element)enu.nextElement();
                SVGRectElement svgEle = (SVGRectElement)ele;
                int iXPos = (int)svgEle.getX().getBaseVal().getValue();
                int iYPos = (int)svgEle.getY().getBaseVal().getValue();
                Point p = new Point(cXPos - iXPos, cYPos - iYPos);
                selElements.put(ele, p);                
            }
       }
    }

    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) {
            if (actionNode == null)return;
            DOMMouseEvent elEvt = (DOMMouseEvent)evt;
            if (action == DRAG) {
                Enumeration enu = selElements.keys();
                while (enu.hasMoreElements()) {
                    Element e = (Element)enu.nextElement();
                    Point p = (Point)selElements.get(e);
                    int tx = elEvt.getClientX() - (int)p.getX();
                    int ty = elEvt.getClientY() - (int)p.getY();
                    SVGRectElement svgElement = (SVGRectElement)e;
                    SVGLength lengthX = svgElement.getX().getBaseVal();
                    SVGLength lengthY = svgElement.getY().getBaseVal();
                    lengthX.setValue(tx);
                    lengthY.setValue(ty);
                }
            }
        }
    }
}

Thanks,
Sudhakar


--
View this message in context: http://www.nabble.com/Multiple-Group-Elements-Selection-and-Moving-t1799038.html#a4973895
Sent from the Batik - Users forum at Nabble.com.


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


Mime
View raw message