Hi, i want to insert a shape into JSVGCanvas in position where mouse is
clicked in all jsvgcanvas...so i wrote this code but i can to insert shape
only on root element of my svg file:
public JButton getProva() {
prova = new JButton();
prova.setText("rect");
prova.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
e) {
if(muovi.isSelected() == true) {
move = false;
muovi.setSelected(false);
}
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
public void run() {
SVGElement svgRoot =
doc.getRootElement();
EventTarget target =
(EventTarget)svgRoot;
target.addEventListener("click", new mouseClicked(), false);
}
});
//svgCanvas.repaint();
}
});
return prova;
}
private class mouseClicked implements EventListener {
public void handleEvent(Event evt) {
doc = svgCanvas.getSVGDocument();
SVGElement svgRoot = doc.getRootElement();
DOMMouseEvent eleEvt = (DOMMouseEvent)evt;
Node node = ((Element)eleEvt.getTarget());
startPt = ptInitial((Element)node,eleEvt.getClientX(),
eleEvt.getClientY());
Element rect = doc.createElementNS(svgNS, "rect");
rect.setAttributeNS(null, "id", "bgrectangle");
rect.setAttributeNS(null, "x",""+
startPt.getX());
rect.setAttributeNS(null, "y",""+
startPt.getY());
rect.setAttributeNS(null, "width", "50");
rect.setAttributeNS(null, "height", "50");
rect.setAttributeNS(null, "style",
"fill:red;stroke:none");
svgRoot.appendChild(rect);
}
}
private SVGPoint ptInitial(Element elem, int x, int y){
doc = svgCanvas.getSVGDocument();
SVGMatrix matrix = ((SVGLocatable)elem).getScreenCTM();
SVGMatrix matrixIn = matrix.inverse();
SVGPoint pt = doc.getRootElement().createSVGPoint();
pt.setX(x);
pt.setY(y);
pt = pt.matrixTransform(matrixIn);
return pt;
}
--
View this message in context:
http://www.nabble.com/insert-a-shape-via-mouse-tf2622130.html#a7316539
Sent from the Batik - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]