xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peppe <peppe.de...@gmail.com>
Subject RE: insert a rect into JSVGCanvas
Date Wed, 08 Nov 2006 09:07:02 GMT

Thank you for answer. I use this code, wrote by another user to move my rect,
but there is a problem: rect create in my JSVGCanvas don't move but the
elements in svg file loaded yes...why? in my code i do
svgRoot.appendChild(elem)....:

public JButton getProva() {
		if (prova == null) {
			prova = new JButton();
			prova.setBounds(new Rectangle(6, 139, 92, 31));
			prova.setText("rect");
			prova.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {	
                   
JSVGCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
						public void run() {
								SVGDocument doc = JSVGCanvas.getSVGDocument();
	                			JSVGCanvas.setDocument(doc);
								SVGElement svgRoot = doc.getRootElement();
								Element elem = doc.createElementNS(svgNS, "rect");
								elem.setAttributeNS(null, "id", "bgrectangle");
								elem.setAttributeNS(null, "x","200");
								elem.setAttributeNS(null, "y","100");
								elem.setAttributeNS(null, "width", "50");
								elem.setAttributeNS(null, "height", "50");
								elem.setAttributeNS(null, "pointer-events", "fill");
								elem.setAttributeNS(null, "style", "fill:red;stroke:none"); 
								svgRoot.appendChild(elem);
						}
                    });
					JSVGCanvas.repaint();
				}

			});
		}
		return prova;
	}
	
	private SVGPoint localPt(Element elem, int x, int y) {
        SVGDocument svgDocument = JSVGCanvas.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 = JSVGCanvas.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
+ ")");
                }
            }
        }
    }

	/**
	 * This method initializes muovi	
	 * 	
	 * @return javax.swing.JToggleButton	
	 */
	private JToggleButton getMuovi() {
		if (muovi == null) {
			muovi = new JToggleButton();
			muovi.setBounds(new Rectangle(6, 178, 94, 30));
			muovi.setText("muovi");
			muovi.addItemListener(new MuoviItemListener());
		}
		return muovi;
	}

public class MuoviItemListener implements ItemListener { 
	public void itemStateChanged(ItemEvent e) {
		int status = e.getStateChange(); 
		if(status == ItemEvent.SELECTED){
			SVGElement svgRoot = JSVGCanvas.getSVGDocument().getRootElement();
			EventTarget t = (EventTarget)svgRoot;
			t.addEventListener("mousemove", new OnMoveAction(), false);
			t.addEventListener("mousedown", new OnDownAction(), false);
			t.addEventListener("mouseup", new OnUpAction(), false);
		}
	}
} 


-- 
View this message in context: http://www.nabble.com/insert-a-rect-into-JSVGCanvas-tf2578737.html#a7234967
Sent from the Batik - Users mailing list archive 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