Hi all,
I’m writing a vector based draw program. I use Shape as common type for my draw elements: Rectangle, Oval, and GeneralPath for freehand drawing. User can also move, rotate and resize such elements in wysiwyg fashion.
When he drags the mouse to modify an element, I update the canvas.
My two problems are:
[] the amount of cpu it takes to render elements to the canvas (30% CPU on win2k AthlonXP1900+ 512Mb)
[] the gc that pauses every 4 seconds the rendering process (I’m using jdk 1.4.1_03)
Can you please help me ?
Here is the rotation code:
public final void rotate(Drawable d, double a) {
DrawableState state = d.state;
Rectangle actualWorkingBounds = state.actualWorkingBounds;
state.rotation += a;
trans.setToIdentity();
trans.rotate(a, state.center.getX(), state.center.getY());
d.working.transform(trans);
actualWorkingBounds.setRect(d.working.getBounds());
state.location.setLocation(actualWorkingBounds.getX(), actualWorkingBounds.getY());
}
where d.working is a GeneralPath and trans is an AffineTransform.
Here is the rendering code (inside a Canvas subclass):
public final void repaintNow(Rectangle clip) {
int x, y, w, h;
Graphics2D g = buffer.createGraphics();
g.setClip(clip.x,clip.y,clip.width,clip.height);
g.setColor(Color.white);
g.fillRect(clip.x,clip.y,clip.width,clip.height);
g.setRenderingHints(renderingHints);
for(int k = size; k >= 0 ; k--) {
if(controller.getTotalBounds(draws[k]).intersects (clip)) {
draws[k].paint(g);
}
}
g.dispose();
getGraphics().drawImage(buffer, 0, 0, null);
try{ Thread.sleep(14); } catch (Exception exn){}
}
where buffer is a BufferedImage created with:
buffer = getGraphicsConfiguration().createCompatibleImage(dimension.width, dimension.height);
When user drags I call the rotate code and the repaintNow one.
Thanks A LOT guys.