Dear all,
I'm always hitting the java.util.ConcurrentModificationException when
I try to move a box and an arrow at the same time. Would anyone please
give me some suggestions? Is it a known bug? Should I create a bug
report?
I have made a very simple test case, where if you drag the second box,
the program will throws you the ConcurrentModificationException from
org.apache.batik.gvt.UpdateTracker.getDirtyAreas()
Source code in the attachement.
Thanks a lot in advance.
Kind regards,
Qian
/*
* TestConcurrent.java
*
* Created on September 26, 2007, 10:14 AM
*/
package testsvg;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* @author qianli
*/
public class TestConcurrent extends javax.swing.JFrame {
public Document doc;
private Element svgRoot;
private JSVGCanvas svgCanvas;
public String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
public int x;
public int y;
ElementHandler eHandler ;
/** Creates new form TestConcurrent */
public TestConcurrent() {
initComponents();
svgCanvas = new JSVGCanvas();
svgCanvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
doc = impl.createDocument(svgNS, "svg", null);
svgRoot = doc.getDocumentElement();
svgCanvas.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
svgCanvasMouseMoved(evt);
}
public void mouseDragged(java.awt.event.MouseEvent evt) {
svgCanvasMouseDragged(evt);
}
});
eHandler = new ElementHandler(this);
svgCanvas.setDocument(doc);
jPanelMain.add(svgCanvas, java.awt.BorderLayout.CENTER);
this.pack();
}
private void svgCanvasMouseMoved(java.awt.event.MouseEvent evt) {
x = evt.getX();
y = evt.getY();
}
private void svgCanvasMouseDragged(java.awt.event.MouseEvent evt) {
x = evt.getX();
y = evt.getY();
System.out.println("Mouse moved x="+x+" y="+y);
eHandler.updateElementLocation();
}
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jPanelMain = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanelMain.setLayout(new java.awt.BorderLayout());
jPanelMain.setPreferredSize(new java.awt.Dimension(500, 500));
getContentPane().add(jPanelMain, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestConcurrent().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanelMain;
// End of variables declaration//GEN-END:variables
}
/*
* ElementHandler.java
*
* Created on September 26, 2007, 10:44 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package testsvg;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* @author qianli
*/
public class ElementHandler {
Element e1;
Element e2;
Element basicLine;
Element arrowHead;
TestConcurrent main;
/** Creates a new instance of ElementHandler */
public ElementHandler(TestConcurrent main) {
this.main = main;
Element svgRoot = main.doc.getDocumentElement();
e1 = main.doc.createElementNS(main.svgNS, "rect");
e1.setAttributeNS(null, "x", "0");
e1.setAttributeNS(null, "y", "0");
e1.setAttributeNS(null, "width", "50");
e1.setAttributeNS(null, "height", "50");
svgRoot.appendChild(e1);
e2 = main.doc.createElementNS(main.svgNS, "rect");
e2.setAttributeNS(null, "x", "200");
e2.setAttributeNS(null, "y", "100");
e2.setAttributeNS(null, "width", "50");
e2.setAttributeNS(null, "height", "50");
svgRoot.appendChild(e2);
String endArrow1 = (0-10)+" "+(0-20);
String endArrow2 = (0+10)+" "+(0-20);
String d = "M 0 0 L "+endArrow1+" M 0 0 L "+endArrow2+" z";
arrowHead = main.doc.createElementNS(main.svgNS, "path");
arrowHead.setAttributeNS(null, "d", d);
arrowHead.setAttributeNS(null, "stroke", "blue");
svgRoot.appendChild(arrowHead);
basicLine = main.doc.createElementNS(main.svgNS, "path");
basicLine.setAttributeNS(null, "stroke", "blue");
updateArrowPath() ;
svgRoot.appendChild(basicLine);
}
public void updateElementLocation(){
int x = main.x;
int y = main.y;
e2.setAttributeNS(null, "x", (x)+"");
e2.setAttributeNS(null, "y", (y)+"");
updateArrowPath() ;
}
private void updateArrowPath() {
String d = "";
if(e1 != null && e2 != null){
int x1 = Integer.parseInt(e1.getAttributeNS(null, "x"));
int y1 = Integer.parseInt(e1.getAttributeNS(null, "y"));
int width1 = Integer.parseInt(e1.getAttributeNS(null, "width"));
int height1 = Integer.parseInt(e1.getAttributeNS(null, "height"));
int x2 = Integer.parseInt(e2.getAttributeNS(null, "x"));
int y2 = Integer.parseInt(e2.getAttributeNS(null, "y"));
int width2 = Integer.parseInt(e2.getAttributeNS(null, "width"));
int height2 = Integer.parseInt(e2.getAttributeNS(null, "height"));
int arrowX1 = x1+(width1/2);
int arrowY1 = y1+(height1/2);
String arrowStartPoint = arrowX1+" "+arrowY1;
String arrowEndPoint = x2+" "+y2;
d = "M "+arrowStartPoint+" L "+arrowEndPoint+" z";
basicLine.setAttributeNS(null, "d", d);
double angle = calculateRotateValue(arrowX1, arrowY1, x2, y2);
arrowHead.setAttributeNS(null, "transform", "translate("+arrowEndPoint+") rotate("+angle+")");
}
}
private double calculateRotateValue(int x1, int y1, int x2, int y2){
double oppositeLength = java.lang.Math.abs(x2-x1);
double adjacentLength = java.lang.Math.abs(y2-y1);
double radian = java.lang.Math.atan(oppositeLength/adjacentLength);
double angle = radian/2/java.lang.Math.PI*360;
System.out.println("oppositeLength="+oppositeLength+" adjacentLength="+adjacentLength+" oppositeLength/adjacentLength="+(oppositeLength/adjacentLength)+" radian="+radian+" angle="+angle);
double turningAngle = 0.0;
if(x2>x1 && y2>y1){
turningAngle = 360-angle;
}else if(x2>x1 && y2<y1){
turningAngle = 180+angle;
}else if(x2<x1 && y2>y1){
turningAngle = angle;
}else if(x2<x1 && y2<y1){
turningAngle = 180-angle;
}
return turningAngle;
}
}---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]