Getting Rid of clipRect for paint()

When paint() iscalled, the clipRect of the Graphics obj may already be set to somethingsmaller than the full drawable region of the object (if, for instance, it’sa partial window-manager expose). What’s the right way for working aroundthis and being able to draw the entire object in paint()?

You’re right, the clipping rectangle of the graphics context is set before acomponent’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 oftheir graphics context, Java would normally update only the piece that needsto be refreshed. However, to bypass this partial update and make sure thatthe entire component is drawn every time, you can save the original graphicscontext during the first call to paint and then reuse it in subsequent calls. For example, the following program saves the graphics context when paint isfirst called and just recycles it.

import java.awt.*;import*;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);                f.pack();      ;        }}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);        }}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may