paint : Java Glossary

Actually do the drawing of some component. Does not erase the background first. The paint routine calls lower level routines paintComponent, paintChildren, printComponent and printChildren. paints are triggered as the spirit moves the native GUI (Graphic User Interface). 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 (Advanced Windowing Toolkit) 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.


In Java version 1.2 or later 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(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.


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

Oracle’s Javadoc on paint package : available:

This page is posted
on the web at:

Optional Replicator mirror
on local hard disk J:

Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.
Your face IP:[]
You are visitor number