Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Weird Redraw Behavior on canvas

Thread view: 
D-Dog - 05 Jul 2006 14:36 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.