Hi,
I'm using a canvas to draw a simple polygon that is slightly
transparent using a double buffer. I'm also clipping the area around
the polygon. Below is some code that I'm using. For the most part
everything works fine, until paint is called through something like an
expose event (I'll just take a window to obscure the canvas, then move
it). The polygon gets plotted over again making it brighter. Obscure
the window again, it gets brighter. It will keep getting brighter each
time paint is called. However, if I initiate a repaint() call
manually, which causes the code to go through update() first, it plots
correctly. For some reason it's related to the clipping because if I
disable the clipping code, it works correctly. New to java, so bear
with me. Thanks for any help!
Dennis
//
-----------------------------------------------------------------------------------
public void update(Graphics g) {
if (thisImage == null) {
thisImage = createImage(getSize().width, getSize().height);
}
Graphics thisG = thisImage.getGraphics();
thisG.setColor(getBackground());
thisG.fillRect(0,0,getSize().width,getSize().height);
thisG.setColor(getForeground());
thisG.setFont(getFont());
paint(thisG);
g.drawImage(thisImage,0,0,this);
}
// -------------------------------------------------------------------
pubic void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// Setup clipping area
Rectangle2D r = new Rectangle2D.Float();
r.setRect(xoffset,yoffset,xAxis, yAxis);
g2.setClip(r);
g2.clip(r);
// call the custom Polygon plotting routine
plotPoly(g2, plottingPoints, numPoints, new Color(200,200,200,50));
g2.setClip(null);
}
//
-----------------------------------------------------------------------
dsjoblom@abo.fi - 05 Jul 2006 15:29 GMT
> Hi,
> I'm using a canvas to draw a simple polygon that is slightly
[quoted text clipped - 9 lines]
> disable the clipping code, it works correctly. New to java, so bear
> with me. Thanks for any help!
It is supposed to work this way. See
http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callback
and read about the difference between update and paint. The reason (I
guess, I haven't really done any debugging since your code can't be run
directly) your code works without the clipping code is that your code
will paint over the whole component if the clip is not set.
My suggested solution to this problem is that your code probably
doesn't even need to use update. Just put all your painting code in
paint. AWT is also considered quite old school today, so you may want
to move over to Swing directly if you are new to Java.
Regards,
Daniel Sjöblom
Knute Johnson - 05 Jul 2006 16:30 GMT
> Hi,
> I'm using a canvas to draw a simple polygon that is slightly
[quoted text clipped - 52 lines]
> //
> -----------------------------------------------------------------------
Painting needs to occur in paint() not in update(). update() normally
clears the background and calls paint(). If you really want to use an
image to double buffer, override update() so that all it does is call
paint() and in paint() just draw the image. Do your rendering to the
buffer image somewhere else.

Signature
Knute Johnson
email s/nospam/knute/
D-Dog - 05 Jul 2006 19:07 GMT
I moved the rendering to a separate routine and just redrew the image
in paint and that seemed to do the trick. Thank you very much for your
help!
Dennis