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

VIEWS: 5 PAGES: 20

									Advanced Java Screen Update
Techniques

SD’98 - Session 4406

Ted Faison
ted.faison@computer.org
Faison Computing Inc.
www.faisoncomputing.com
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
   • DrawingRectangles.java
 – Filling a shape
   • FillingRectangles.java
 – Drawing the outline of a shape
   • TextOutline.java
Flicker and repaint ( )
  – What repaint() does
    • Erases component background
    • Sets graphics context color
    • Calls paint()
  – Flicker is produced by the background
    erasure
  – Example
    • RectWithFlicker.java
Minimizing Flicker
• Tips
  – Call paint() directly, instead of repaint(),
    if the component has no transparent
    areas
  – Don't erase areas you're going to paint
    over
Moving components on the
screen
• SetLocation causes the AWT to erase
  the old image and draw the new one.
     • Can cause flicker, since no optimizations are
       applied

• Use a silhouette
     • a lightweight outline XORed to the screen
     • Extremely fast
     • Example: UsingSilhouettes.java
Double Buffering
• An example
  – OffScreenImage.java

• Using BufferedImage
  – gives control over transparency and
    blending
  – examples
       – OffScreenBufferedImage.java
       – OffScreenBufferedImageWithTransparency.java
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: RectMovingWithClipping.java
   • Images: ImageMovingWithClipping .java
   • Shapes: ShapeMovingWithClipping.java
      – uses coordinate transformations to move
        shape
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
    • OffScreenTransformedImage.java
    • ScalingImages.java
    • RotatingImages.java
    • ShearingImages.java
Batch Painting
• A technique to speed up rendering
• Used internally by the Graphics2D
  context
• Batch: a block of contiguous pixels
  with the same Paint object
Drawing Patterns
• Patterns require BufferedImage and
  TexturePaint objects
  – Example PatternedBackgrounds.java
• Filling Shapes with Patterns
  – Example: FillingShapeWithPattern.java
  – Example: FillingTextWithPattern.java
Conclusion
• 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
  filling
• Batch painting internally increases
  screen update speed

								
To top