Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: AWT
Expertise: Beginner
Mar 19, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Getting Rid of clipRect for paint()

When paint() is called, the clipRect of the Graphics obj may already be set to something smaller than the full drawable region of the object (if, for instance, it's a partial window-manager expose). What's the right way for working around this and being able to draw the entire object in paint()?

You're right, the clipping rectangle of the graphics context is set before a component's paint() method is called. In case of partial window updates, the clipping rectangle is set to cover only the area that needs to be redrawn. Since graphics commands have no effect outside the clipping rectangle of their graphics context, Java would normally update only the piece that needs to be refreshed. However, to bypass this partial update and make sure that the entire component is drawn every time, you can save the original graphics context during the first call to paint and then reuse it in subsequent calls. For example, the following program saves the graphics context when paint is first called and just recycles it.

import java.awt.*;

import java.io.*;

public class ClipTest {

        public static void main(String argv[]) {

                Frame f = new Frame("cliptest");

                f.setLayout(new BorderLayout());

                // add an X-drawing canvas

                DrawX s = new DrawX(180, 180);

                f.add("North", s);





class DrawX extends Canvas {

        int width, height;

        Graphics g = null;              // initially set the Graphics object to null

        // Draw a big X of the specfied width and height


        public DrawX(int w, int h) {

                width = w;

                height = h;


        public Dimension minimumSize() {

                return new Dimension(width, height);


        public Dimension preferredSize() {

                return minimumSize();


        public void paint(Graphics g1) {


                // print the boundaries of the current clipRect for comparison


                Rectangle r = g1.getClipRect();

                System.out.println("Paint: cliprect(" + r.x + ", " + r.y + ", "

                                        + r.width + ", " + r.height + ")" );


                // if this is the first time paint is called, remember the Graphics object

                // and use g to do all the drawing from this point on.


                if (g == null) 

                        g = g1.create();


                // just to make sure, print out the original clipRect boundaries also


                r = g.getClipRect();

                System.out.println("Paint: original(" + r.x + ", " + r.y + ", "

                                        + r.width + ", " + r.height + ")" );

                // draw a big X

                g.drawLine(0, 0, width, height);

                g.drawLine(0, height, width, 0);



DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date