Advanced Java Screen Update Techniques SD’98 - Session 4406 by bns26590


									Advanced Java Screen Update

SD’98 - Session 4406

Ted Faison
Faison Computing Inc.
Painting with the 2D API
  – Everything is just a Shape
    • The Shape interface and its main methods
       – boolean contains(Point2D)
       – Rectangle getBounds()
       – PathIterator getPathIterator(AffineTransform)
       – boolean Intersects(Rectangle2D)

    • GeneralPath, Bezier Curves
    • Rectangles, Polygons, Ellipses, Arcs
    • Text
2D Drawing Examples
 – Drawing a geometric shape
 – Filling a shape
 – Drawing the outline of a shape
Flicker and repaint ( )
  – What repaint() does
    • Erases component background
    • Sets graphics context color
    • Calls paint()
  – Flicker is produced by the background
  – Example
Minimizing Flicker
• Tips
  – Call paint() directly, instead of repaint(),
    if the component has no transparent
  – Don't erase areas you're going to paint
Moving components on the
• SetLocation causes the AWT to erase
  the old image and draw the new one.
     • Can cause flicker, since no optimizations are

• Use a silhouette
     • a lightweight outline XORed to the screen
     • Extremely fast
     • Example:
Double Buffering
• An example

• Using BufferedImage
  – gives control over transparency and
  – examples
Shaping the area painted:
Clipping Regions
• Using Graphics.setClip
  – JDK 1.1 supported only rect
    • Graphics.setClip(int, int, int, int);
  – JDK 1.2 also supports arbitrary shapes
    • Graphics2D.setClip(Shape)
Improving Screen Update Speed
with Clipping
 – Use rectangular clipping areas only
 – Examples
   • Rectangles:
   • Images: ImageMovingWithClipping .java
   • Shapes:
      – uses coordinate transformations to move
Coordinate Transformations
• Functions that map a point in one space
  to a point in another space
• Represented using a 3x3 matrix
• Transformations require multiplying
  each pixel by the transformation matrix
• Positive angles rotate the X+ axis
  towards the Y+ axis
• Can be used to invert axes, bend
  images, distort space arbitrarily
Coordinate Transformations

a11 a12 a13   x    a11x a12y a13       x’
a21 a22 a23   y = a21x a22y a23    =   y’
 0   0 1      1     0    0    1        1
Affine Transforms
• Maintain straightness and parallelism
• Translation
     • setToTranslation(double dx, double, dy);
     • used to support graphics scrolling

  User Space                  Device Space
Affine Transforms
• Rotation
  – Rotating about the origin
     • setToRotation(double theta);

   User Space                  Device Space
Affine Transforms
 – Rotation about an arbitrary point
    • SetToRotation(theta, x, y);

     (x, y)                         (x, y)

  User Space                  Device Space
Affine Transforms
• Shearing
    • setToShear(double sh, double sy)

  User Space                Device Space
Affine Transforms
• Scaling
     • setToScale(double sx, double sy)
     • anisotropic vs isotropic scaling

  User Space                 Device Space
Affine Transforms
• Handling transformed images with
  offscreen buffers
  – Examples
Batch Painting
• A technique to speed up rendering
• Used internally by the Graphics2D
• Batch: a block of contiguous pixels
  with the same Paint object
Drawing Patterns
• Patterns require BufferedImage and
  TexturePaint objects
  – Example
• Filling Shapes with Patterns
  – Example:
  – Example:
• Use Double Buffering to minimize flicker
• Use Coordinate Transformations to
  scroll and zoom
• Everything is a Shape, with support for
  stroking, gradient filling and pattern
• Batch painting internally increases
  screen update speed

To top