CS4812: Java Lecture IX - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CS4812: Java Lecture IX

Description:

Implementing a double-buffered light-weight container in Java (JDK 1.1.x) ... The heavy-lifting associated with image production is peformed by the image package. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 19
Provided by: davidd1
Category:
Tags: cs4812 | java | lecture

less

Transcript and Presenter's Notes

Title: CS4812: Java Lecture IX


1
CS4812 JavaLecture IX
  • A Stay of Execution
  • or
  • The Broken Shaft

2
(Course Administration Note)
  • So far, students have completed
  • An aggressive P1, which required code above and
    beyond simple IO handling
  • A difficult P2, which covered numerous detailed
    topics
  • Event Handling
  • Graphics
  • GUI design/layout
  • Innovative subclass
  • Delays in the quarter make it unreasonable to
    assign a complete p3 on the eve of dead week.

3
(Course Administration Note)
  • Options
  • Skip p3 altogether, thereby rendering our
    discussion of graphics/buffering quite irrelevant
    to your CS4812 experience.
  • Assign p3, and curve it. (Read shaft.)
  • Rework p3 into a pass/fail lab with explicit
    steps, thereby giving exposure to lightweight
    components, etc.
  • Lessons Learned
  • Student Grading A good thing.
  • Its only 2 credits
  • Students learn better if the material, and not
    the grade, is made the focus of the course.

4
Implementing a double-buffered light-weight
container in Java (JDK 1.1.x)
  • Thus far, we have considered
  • Problems associated with heavy-weight components.
    (Solution light-weights!)
  • Problems associated with animation. (Solution
    double buffering/clipping areas)
  • The lab (read final) version of p3 will
    require the combination of these techniques.
  • Today, we cover some specific problems associated
    with images and image loading

5
Images Why?
  • Our light weights all have a paint() method
    useful for describing the look and feel of our
    component.
  • To make full use of the paint() method, we have
    to become familiar with image manipulation in
    Java.

6
Images What?
  • The AWT provides the java.awt.Image class.
    Images are handed off to other components for
    drawing.
  • For example, you never have an image draw itself
    rather, an image reference is drawn used by a
    graphics object
  • g.drawImage(Image, int, int, ImageObserver)

7
java.awt.Image
  • public abstract int getWidth(ImageObserver)
  • public abstract int getHeight(ImageObserver)
  • public abstract ImageProducer getSource()
  • public abstract Graphics getGraphics()
  • public abstract Object getProperty (String name,
    ImageObserver)
  • public abstract void flush()

8
The image Package
  • Most of the code for manipulating images resides
    not in the Image class, but in the java.awt.image
    package.
  • The heavy-lifting associated with image
    production is peformed by the image package.
    Most of the methods you will use are found in the
    Image class.

9
Image Producers/Consumers
  • Image production is decided async in Java.
  • Java handles image production with the
    java.awt.image.ImageProducer class.
  • The producer creates the bits making up an image,
    and updates the image as it loads.
  • The java.awt.image.ImageConsumer interface has
    but a single method
  • imageUpdate()
  • The producer calls this method to report updates
    in the progress of an image.

10
Image Consumers
  • Just about everything in the AWT is an image
    consumer, meaning it implements
    java.awt.image.ImageConsumer.
  • Component, for example, implements this
    interface, making every widget Image-aware.
    (Light and heavy weights alike!)
  • Thus, if a method takes an ImageObserver
    argument, just pass in a component

11
Loading images
  • Images are first obtained from the toolkit. An
    applet has a toolkit with factor settings
    applications need to call the static toolkit
    reference
  • APPLET
  • Image I getImage(getCodeBase(), sox.gif)
  • APPLICATION
  • Image I Toolkit.getDefaultToolkit().getImage(get
    CodeBase(), sox.gif)
  • (Note getDocumentBase() works as well)

12
Image Observer Methods
  • Numerous methods take an image observer argument
  • prepareImage (in Component Toolkit)
  • checkImage(in Component Toolkit)
  • drawImage (in Graphics)
  • getWidth/getHeight(in Image class)

13
Using Image Observers
  • Since image producers call the consumer
    imageUpdate method, we can use this method as a
    means of tracking media loading.
  • We can override imageUpdate to wait on any
    repainting until allbits are loaded
  • Consider
  • public boolean imageUpdate(Image image, int
    flags, int x, int y, int w, int h)
  • if (flags ALLBITS) !0) repaint()
  • return true

14
ImageObserver Constants
  • Other constant defined in ImageObserver include
  • ABORT--if aborted loaded
  • ALLBITS--if all bits loaded
  • ERROR--if error occurs
  • FRAMEBITS--another complete frame of a multiframe
    image is now available
  • HEIGHT
  • PROPERTIES--dimensions are known
  • SOMEBITS--more pixels available
  • WIDTH

15
  • import java.applet.import java.net.URL
  • import java.awt.Graphicsimport java.awt.Image
  • public class smoothie extends Applet
  • Image im
  • public void init()
  • super.init()
  • URL base getCodeBase()
  • im getImage(base, "sox.gif")
  • public void paint (Graphics g)
  • g.drawImage(im, 0,0, this)
  • public boolean imageUpdate(Image image, int
    flags,
  • int x, int y, int w, int h)
  • if ((flags ALLBITS)!0)
  • repaint()
  • //tryThread.sleep(500)
  • //catch(Exception e)
  • return true

16
Fun with Image Filters
  • The use of image filters allows us to crop images
    into smaller image objects
  • Consider
  • Image StripImage getImage(getCodeBase(),
    strip.gif)
  • ImageFilter f new CropImageFilter (x, y, w,
    h)
  • ImageProducer p new FilteredImageSource(StripI
    mage.getSource(), f)
  • Image Im createImage(p)
  • Also, look at RGBImageFilter class!

17
Other image filters Memory-sourced images
  • One can also specify a range of memory (or
    rather, an object) as an image source
  • myInts new intSIZE
  • MemoryImageSource m new MemoryImageSource(w,
    h, myInts, start, width)

18
Pixel-Grabbing
  • A complement to memory image sourcing is grabbing
    an images pixels and stuffing them into an
    array
  • pixels new intwh
  • PixelGrabber pig new PixelGrabber (image, x, y,
    w, h, pixels, 0, width)
  • trypig.grabPixels()
  • catch (InterruptedException e)
  • We can use pixel grabbers and memory image
    sources to manipulate and edit images on a very
    small level.
Write a Comment
User Comments (0)
About PowerShow.com