You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Peppe <pe...@gmail.com> on 2006/11/08 12:42:25 UTC
appendchild, error?
Hi, i have inserted a rect in my svgCanvas where, before, i load a file svg.
when i want to move an element i can move elements contained in loaded svg
file, instead the rect that i inserted no...why?
For my application i use a Japplet:
public class tesi extends JApplet {
final JPanel panel = new JPanel(new BorderLayout());
final String svgNS = "http://www.w3.org/2000/svg"; // @jve:decl-index=0:
public JPanel jContentPane = null;
public JButton jButton = null;
JSVGCanvas svgCanvas = new JSVGCanvas();
private JToggleButton muovi = null;
private JButton Circle = null;
String SCALE = "scale";
String DRAG = "drag";
String action;
EventTarget actionNode, actionTgt;
SVGPoint startPt;
boolean dragged = false;
public tesi() {
super();
}
public void init() {
this.setSize(600, 400);
this.setContentPane(getJContentPane());
}
public JPanel getJContentPane() {
if (jContentPane == null) {
label.setBounds(new Rectangle(78, 4, 137, 31));
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJSVGCanvas(), null);
jContentPane.add(label, null);
jContentPane.add(getZoomIN(), null);
jContentPane.add(getZoomOUT(), null);
jContentPane.add(getProva(), null);
jContentPane.add(getMuovi(), null);
jContentPane.add(getCircle(), null);
}
return jContentPane;
}
//in this method i load the svg file
public JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(10, 5, 59, 28));
jButton.setText("load");
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fc = new JFileChooser(".");
int choice = fc.showOpenDialog(panel);
if (choice == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
try{
svgCanvas.setURI(f.toURL().toString()); //carica
l'immagine dal file all'interno del canvas
}catch(IOException error){
error.printStackTrace();
}
}
}
});
}
return jButton;
}
//this method initializes JSVGCanvas
public JSVGCanvas getJSVGCanvas() {
svgCanvas.setBounds(new Rectangle(114, 54, 476, 333));
svgCanvas.getInteractors().add(rotateInteractor);
svgCanvas.getInteractors().add(zoomInteractor);
svgCanvas.setDocumentState(JSVGComponent.ALWAYS_DYNAMIC);
return svgCanvas;
}
//this method initializes circle
private JButton getCircle() {
if (Circle == null) {
Circle = new JButton();
Circle.setBounds(new Rectangle(5, 173, 92, 32));
Circle.setText("circle");
Circle.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
svgCanvas.getUpdateManager().getUpdateRunnableQueue().invokeLater(new
Runnable() {
public void run() {
SVGDocument doc = svgCanvas.getSVGDocument();
//svgCanvas.setDocument(doc);
SVGElement svgRoot = doc.getRootElement();
Element elem = doc.createElementNS(svgNS, "circle");
elem.setAttributeNS(null, "id", "bgcircle");
elem.setAttributeNS(null, "cx","50");
elem.setAttributeNS(null, "cy","100");
elem.setAttributeNS(null, "r", "10");
elem.setAttributeNS(null, "style", "fill:green;");
svgRoot.appendChild(elem);
}
});
}
});
}
return Circle;
}
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
+ ")");
}
}
}
}
}
--
View this message in context: http://www.nabble.com/appendchild%2C-error--tf2594737.html#a7237004
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