I'm new to Batik and used it for a prototype that displays waveforms and
measurements. I'd like to learn some "best practices" for Batik. It works
o.k., except for the following:
1. I wanted to have a big crosshair instead of the default cursor, so
I defined one in the static SVG document that I put all my data in.:
<g id="cursor" transform="translate(0,0)">
<line stroke-width=".1" style="fill:none;stroke:red" y1="-10"
y2="10" x1="0" x2="0"/>
<line stroke-width=".1" style="fill:none;stroke:red" x1="-10"
x2="10" y1="0" y2="0"/>
</g>
In my mousemotionlistener I change the translate(*,*) to whatever it needs
to be. This works, but it is kind of sluggish, and the SVG cursor is always
somewhat behind the system cursor:
public void moveCursor(MouseEvent e) {
try {
AffineTransform at = this.jsvgCanvas.getViewBoxTransform()
.createInverse();
Point2D p = new Point2D.Double(e.getX(), e.getY());
p = at.transform(p, null);
if (EcgApplet.cursorTransform == null) {
Element cursor = this.jsvgCanvas.getSVGDocument()
.getElementById("cursor");
EcgApplet.cursorTransform =
cursor.getAttributeNode("transform");
}
EcgApplet.cursorTransform.setValue("translate(" + p.getX() + ","
+ p.getY() + ")");
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex);
}
}
(If the design looks bad it's because it is, this prototype will be thrown
away)
2. Displaying the measurements (i.e. drawing about 84 lines and 24
texts) takes about 2-3 seconds on a 3GHz dual core machine.
The method below is called 12 times:
public void displayItem(GlobalAnalysisItem ai, int beat, int height) {
long t0 = System.currentTimeMillis();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
SVGDocument doc = jsvgCanvas.getSVGDocument();
int lArrow = 3;
int x1 = ai.getOnSet().intValue() / 2;
int x2 = ai.getOffSet().intValue() / 2;
int yBase = 100;
int yTop = -200;
int yLine = -height;
int yName = yLine - 2;
int yValue = yLine + 5;
String name = AnalysisItem.displayType(ai.getTypeID());
String value = ai.getValue().intValue() + "";
Element dimensioning1 = doc.getElementById("dimensioning");
Element dimensioning = jsvgCanvas.getSVGDocument()
.createElementNS(svgNS, "g");
// calipers
long t0a0 = System.currentTimeMillis();
dimensioning.appendChild(createLine(x1, yBase, x1, yTop));
long t0a = System.currentTimeMillis();
System.err.println("displayItemA:" + (t0a - t0a0));
dimensioning.appendChild(createLine(x2, yBase, x2, yTop));
// line
dimensioning.appendChild(createLine(x1, yLine, x2, yLine));
// left arrow
dimensioning.appendChild(createLine(x1, yLine, x1 + lArrow,
yLine + (lArrow / 2)));
dimensioning.appendChild(createLine(x1, yLine, x1 + lArrow,
yLine - (lArrow / 2)));
// right arrow
dimensioning.appendChild(createLine(x2, yLine, x2 - lArrow,
yLine + (lArrow / 2)));
dimensioning.appendChild(createLine(x2, yLine, x2 - lArrow,
yLine - (lArrow / 2)));
// name
dimensioning.appendChild(createText((x1 + x2) / 2, yName, name));
// value
dimensioning.appendChild(createText((x1 + x2) / 2, yValue, value));
dimensioning1.appendChild(dimensioning);
long t1 = System.currentTimeMillis();
System.err.println("displayItem:" + (t1 - t0));
}
Is there anything blatantly wrong with what I'm doing? What can I do
better?
Thanks,
Thomas