CS 4812: Java - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS 4812: Java

Description:

Effective for p3, a new augmentative' grading system will be used. If you turn in a solid draft of your program ... Image production is decidedly async in Java. ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 18
Provided by: davidd1
Category:
Tags: decidedly | java

less

Transcript and Presenter's Notes

Title: CS 4812: Java


1
CS 4812 Java
xemacs
  • Lecture VI
  • A Few Notes on Being
  • Productive In Java

2
Administrative Announcement
  • Effective for p3, a new augmentative grading
    system will be used.
  • If you turn in a solid draft of your program by
    the 19th, you get X points added to your final
    grade for p3.
  • The exact value of the additional points will be
    (100 - the lowest grade for p2.) Details to
    follow.
  • So, by starting early, you pick up some insurance.

3
Image Production and Consumption in Java
  • Many of the lightweights created in p2 used
    images, which relied on the paint() method to
    render the component.
  • Some of you experienced delays in loading the
    images, or in getting the images to appear.
  • To make full use of the paint() method, we have
    to become familiar with image manipulation in
    Java.

4
Images What?
  • To allow for image manipulation, the AWT provides
    the java.awt.Image class. Images dont draw
    themselves. Instead, Images are handed off to
    other components for drawing.
  • For example, you cant have an Image invoke
    drawImage on itself instead, an Image
    reference is used by a graphics object
  • g.drawImage(Image, int, int, ImageObserver)

5
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()
  • getScaledInstance(int, int, int)

6
Field Indices for hints argument to
getScaledInstance(int, int, int)
  • java.awt.Image.SCALE_AREA_AVERAGING
  • Use the Area Averaging image scaling algorithm.
  • java.awt.Image.SCALE_DEFAULT
  • Use the default image scaling algorithm.
  • java.awt.Image.SCALE_FAST
  • Choose an image scaling algorithm that gives
    higher priority to scaling speed than smoothness
    of the scaled image.
  • java.awt.Image.SCALE_REPLICATE
  • Use the ReplicateScaleFilter image scaling
    algorithm.
  • java.awt.Image.SCALE_SMOOTH
  • Choose an image scaling algorithm that gives
    higher priority to image smoothness than scaling
    speed.

7
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 performed by the Image package.
    Most of the useful methods are found in the Image
    class.

8
Image Producers/Consumers
  • Image production is decidedly 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 .

9
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
  • g.drawImage(myImage, x, y, myComponet)

10
Loading images
  • Image are first obtained from the toolkit. An
    applet has a toolkit with factory 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)

11
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)

12
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

13
ImageObserver Constants
  • Other constants 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

14
  • 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(50)
    catch(Exception e)
  • return true

15
Fun with Image Filters
  • The use of an ImageFilter allows us to crop an
    Image 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!

16
Other Image filters Memory-sourced Image
  • 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)

17
Pixel-Grabbing
  • A complement to memory Image sourcing is grabbing
    an Image s 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