Drawing Images on the Screen (AWT)

When images are painted on the screen in parts, the slowness of the process may become apparent. Therefore images are often first drawn on an off-screen copy, and then copied to the screen in one operation. This double buffering is done automatically in Swing.

The Rolodex paints two consecutive images next to each other. Clipping is always automatic.

Image offScrImage = null;
    Graphics offScrGC = null;

    public void paint(Graphics g) {
        // ...
    
        offScrImage = createImage(appWidth, appHeight);
        offScrGC = offScrImage.getGraphics();
        offScrGC.setColor(Color.lightGray); // default background
        // ...
        offScrGC.clearRect(0, 0, appWidth, appHeight);
        // ...
        // frameNum is the framenumber; assume direction LEFT
        offScrGC.drawImage(image[i], -frameNum, 0, this);
        offScrGC.drawImage(image[i+1], appWidth - frameNum, 0, this);
        // ...
        // now draw on screen
        g.drawImage(OffScrImage, 0, 0, this);
        // ...
    }