Home : Course Map : Chapter 11 : Java : Spplements :
Reducing Flicker 2 : Clipping
JavaTech
Course Map
Chapter 11

Introduction
Image Class
Image Loading
  Demo 1 Demo 2  
Pixels/Transparency
  Demo 3
Pixel Handling
  Demo 4  
Demo 5
Exercises

    Supplements
Java 2D Imaging
BufferedImage
Creating Buf.Image
Pixel Handling
  Demo 1 Demo 2
Filters
  Convolutions
     Demo 3
  AffineTransforms
     Demo 4
  LookupTable
     Demo 5 Demo 6
  Rescale
     Demo 7
  Color Conversion
     Demo 8
  Custom
     Demo 9
Exercises
Java Adv Imaging
AWT Flicker:
  Override Update
     Demo 1  Demo 2
  Clipping
     Demo 3
  Double Buffer
     Demo 4

     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

When the image moves, only a fraction of the total display in the red rectangle area below, is changed. No point in redrawing the whole image.

Clipping is the technique to do limit the redrawing to just a chosen part of the image.

The graphics context can be told by the clipRect() method to only accept changes in the clipped area.

The only hard part is calculating the red outlined area above, regardless of which way the mouse moves. Study the clip() method below to see how this is done.

Clipping_Applet7.java
Resources: Apollo16Lander.jpg

import java.awt.*;
import java.awt.event.*;

// Extend the Update_Applet7 class

public class Clipping_Applet7 extends Update_Applet7
{
  int newX, newY;
  int imageWd, imageHt;
 
  public void mouseDragged( MouseEvent e )
  {
    newX=e.getX();
    newY=e.getY();
    repaint();
  }

 // Here we do the math to get the clip region
  void clipToAffectedArea( Graphics g,
      int oldX, int oldY, int nextX, int nextY,
      int width, int height) {
    int x = Math.min( oldX, nextX );
    int y = Math.min( oldy, nextY );
    int w = ( Math.max( oldY, nextX )
                + width ) - x;
    int h = ( Math.max( oldY, nextY )
                + height ) - y;
    g.clipRect( x, y, w, h );
  }

  // Override update. Keep track here of
  // position of the image.
  public void update( Graphics g )
  {
    int lastX = currentX, lastY = currentY;
    currentX = newX;
    currentY = newY;
    clipToAffectedArea( g,
         lastX, lastY,currentX,
         currentY, imageWd, imageHt );
    paint( g );
  }
}
 

 

References & Web Resources

Latest update: March 8, 2006

              Tech
Fractals
Fractal Drawing
   Demo 1
Fractal Draw Code
Fractal Images
  Demo 2
Image Processing
  Demo 3
Histogram Image
  Demo 4
Exercises

           Physics
Calibration/SysError
SimWithCal/SysErr
  Demo 1
Analysis
  Demo 2
Examples

Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.