paint : Java Glossary
home P words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2008-02-06 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
paint
Actually do the drawing of some component. Does not erase the background first. The paint routine is not responsible for painting children, or scheduling their repainting. paints are triggered as the spirit moves the native GUI. It may call paint at any time. It may even call your paint routine several times, to paint a single component, each time with a different clipping region.

Anything you paint outside the clipping region will just be ignored. For efficiency, you should make some effort to avoid rendering large amounts of screen real estate outside it.

// Graphics g is passed to your paint as a parameter.
// get x, y, width and height of region we are actually painting
Rectangle r = g.getClipBounds();

Container.paint() only deals with painting the background of the Container. Container. paintComponents() paints each of the contents, and Container. paintAll() paint everything. Container. paintComponents() calls each contained flyweight component’s paint routine in turn. For efficiency, it bypasses the call if the component is definitely outside the clipregion. For heavyweight contained components, the native GUI handles repainting without AWT involvement. Container.update() behaves similarly. If a Component has zero size, paint won’t be called.

You can’t control whether components get painted by overriding paintComponents or paintAll. The components have a repainting life of their own separate from the Container. To hide them you must either remove or setVisible( false );

You might wonder why paint gets passed a Graphics object. Often paint is asked to restore only a small fraction of the entire image, since for example some obscuring window has been removed from a corner of the Component. When scrolling, the paint method needs to paint only a 4-pixel tall sliver of newly emerging material. The rest of the display is generated by bitblt of previously rendered material. The Graphics object has a built-in clip region, describing just the portion that needs to be redrawn. paint also might be called on the to do duty in printing. The AWT needed some easy way to redirect the output of paint from the screen to the printer. The Graphics object parameter does that. Similarly a paint routine might be asked to create an in-RAM non-displayed image that can later be rapidly blasted onto the screen with :

g.drawImage( img, x, y, this );

In animation, there are two kinds of repaint requests, ones you create to ask paint to do the next animation frame, which may require only a minor touch up of the image, or perhaps scrolling it a pixel, and ones from the operating system, required because your component that was occluded has just been uncovered, and require a complete paint of the component from scratch. How do you tell them apart? You can set some boolean in the component for paint to examine. Further paint can check that g.getClipBounds().equals(new Rectangle(this.getSize())) indicating a request to paint the entire image. The alternative for animation is to abandon the repaint mechanism and paint yourself directly with Component.getGraphics.

paintComponent

In JDK 1.2+ with Swing, override JComponent.paintComponent instead of paint. You may or may not want to call super. paintComponent. You want to explicitly set the opacity with setOpaque( true ); It is often easier just to try all four possibilities of calling/not calling super. paintComponent or not and setOpaque( true/false ) to see the effects than try to reason it out.

JPanel.paintComponent() just paints the JPanel’s content, the JPanel component itself. JPanel.paint() paints the panel, the panel’s children and the border.

Whenever you override paint or paintComponent, chances are you also need to implement getMinimumSize, getMaximumSize and getPreferredSize. I have found this more reliable than using the new setMinimumSize etc. methods. If you don’t, you will usually find your Component squeezed down to 0 × 0 pixels.

paintImmediately

paintImmediately(Rectangle r) is a tool you can use sparingly in Swing. It calls your own paint method immediately and makes it refresh that rectangle. You don’t override it. You call it to bypass waiting for the repaint queue logic.

Gaming

For games, you need high speed painting, and you are typically willing to go to any lengths to get it. Some of the techniques you can use are FSEM and VolatileImage. There you may setIgnoreRepaint( true ) and manually schedule all your repainting.

Learning More

Sun’s Javadoc on the paint method : available:

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] Visit DavidSuzuki.org
You are visitor number 22,350.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/paint.html J:\mindprod\jgloss\paint.html