Weird Redraw Behavior on canvas

D

D-Dog

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);

}

//
-----------------------------------------------------------------------
 
D

dsjoblom

D-Dog said:
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!

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
 
K

Knute Johnson

D-Dog said:
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);

}

//

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.
 
D

D-Dog

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top