xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thomas.dewe...@kodak.com
Subject Re: Multiple Group Elements Selection and Moving
Date Sun, 18 Jun 2006 13:17:45 GMT
Hi Sudhakar,

     I think you are making things more complex then they need to be:

    int startX, startY;

   public void mouseDown(Event evt) {
       DOMMouseEvent mEvt = (DOMMouseEvent)evt;
       startX = mEvt.getClientX();
       startY = mEvt.getClientY();
    }

   public void mouseMove(Event evt) {
    DOMMouseEvent mEvt = (DOMMouseEvent)evt;
       currX = mEvt.getClientX();
       currY = mEvt.getClientY();

    Iterator i = moveGroups.iterator();
    while (i.hasNext()) {
        Element e = (Element)i.next();
        Element p = (Element)e.getParentNode();
        SVGPoint sp = localPt(p, startX, startY);
        SVGPoint cp = localPt(p, currX,  currY);

        float dx = cp.getX()-sp.getX();
        float dy = cp.getY()-sp.getY();
        e.setAttributeNS(null, "transform",
                         "translate("+dx+","+dy+")");
    }
   }


Sudhakar S <yessudhakar@hotmail.com> wrote on 06/18/2006 04:07:30 AM:

> 
> Hi Thomas,
> 
> Herewith i am attaching my sample code for your reference. Here i am
> calculating the distance between last selected element and other 
selected
> elements which will be added while moving the selected group elements. 
But
> still it's not moving properly. Please clarify 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 SVGApplication10 {
> 
>     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) {
>         SVGApplication10 app = new SVGApplication10();
>     }
> 
>     public SVGApplication10() {
>         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);
>         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 rect1Grp =
> doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "g");
>             rect1Grp.setAttributeNS(null, "id", "rect1Grp");
> 
>             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");
>             rect1Grp.appendChild(rect1);
>             doc.getRootElement().appendChild(rect1Grp);
> 
>             Element rect2Grp =
> doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "g");
>             rect2Grp.setAttributeNS(null, "id", "rect2Grp");
>             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");
>             rect2Grp.appendChild(rect2);
>             doc.getRootElement().appendChild(rect2Grp);
> 
> 
>             Element rect3Grp =
> doc.createElementNS(SVGDOMImplementation.SVG_NAMESPACE_URI, "g");
>             rect3Grp.setAttributeNS(null, "id", "rect3Grp");
> 
>             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");
>             rect3Grp.appendChild(rect3);
>             doc.getRootElement().appendChild(rect3Grp);
> 
>             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 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;
>     }
> 
>     SVGPoint getPosition(Element selElement) {
>        SVGLocatable recLoc = (SVGLocatable)selElement;
>        SVGMatrix mat =
> recLoc.getTransformToElement((SVGElement)selElement.getParentNode());
>        SVGPoint cPt =
> svgCanvas.getSVGDocument().getRootElement().createSVGPoint();
>        SVGRect bbox = recLoc.getBBox();
>        cPt.setX(bbox.getX());
>        cPt.setY(bbox.getY());
>        cPt = cPt.matrixTransform(mat);
>        return cPt;
>    }
> 
>    private class OnDownAction implements EventListener {
> 
>         public void handleEvent(Event evt) {
> 
>             DOMMouseEvent elEvt = (DOMMouseEvent)evt;
>             action = DRAG;
>             selElement = (Element)elEvt.getTarget();
> 
> 
> 
if(!((Element)selElement.getParentNode()).getAttribute("id").endsWith("Grp"))
>                 return;
> 
>             actionNode = elEvt.getTarget();
> 
>             startPt = localPt((Element)selElement.getParentNode(),
> elEvt.getClientX(), elEvt.getClientY());
> 
>             if (elEvt.getCtrlKey()) {
>                 selElements.put(selElement.getParentNode(),
> selElement.getParentNode());
>             } else {
>                 selElements = new Hashtable();
>                 selElements.put(selElement.getParentNode(),
> selElement.getParentNode());
>             }
> 
>             SVGPoint cPoint =
> getPosition((Element)selElement.getParentNode());
>             Enumeration enu = selElements.elements();
> 
>             while (enu.hasMoreElements()) {
>                 Element ele = (Element)enu.nextElement();
>                 SVGPoint nPoint = getPosition(ele);
>                 Point p = new Point((int)(cPoint.getX() - 
nPoint.getX()),
> (int)(cPoint.getY() - nPoint.getY()));
>                 offsetMap.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;
>             Element selE = 
(Element)((Element)selElement).getParentNode();
> 
>             SVGPoint pt = localPt((Element)selE.getParentNode(),
> elEvt.getClientX(), elEvt.getClientY());
>             float currentX = pt.getX();
>             float currentY = pt.getY();
> 
>             if (action == DRAG) {
> 
>                 Enumeration enu = selElements.elements();
>                 while (enu.hasMoreElements()) {
>                     Element ele = (Element)enu.nextElement();
> 
>                     int offX = 0;
>                     int offY = 0;
> 
>                     if (offsetMap.containsKey(ele)) {
>                         Point p = (Point)offsetMap.get(ele);
>                         offX = (int)p.getX();
>                         offY = (int)p.getY();
>                     } 
> 
>                     float dx = currentX - startPt.getX() - offX; // +
> _offsetX);
>                     float dy = currentY - startPt.getY() - offY; //+
> _offsetY);
> 
>                     ele.setAttribute("transform", "translate(" + dx + ", 
" +
> dy + ")");
>                 }
>             } 
> 
>         }
>     }
> }
> 
> Thanks,
> Sudhakar
> --
> View this message in context: 
http://www.nabble.com/Multiple-Group-Elements-
> Selection-and-Moving-t1799038.html#a4921349
> 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
> 


---------------------------------------------------------------------
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