Week14 - Chapter 24 Image Fundamentals There are three...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 24 Image Fundamentals There are three common operations that occur when you work with images: creating an image loading an image, and displaying an image In Java, the Image class is used to refer to images in memory and to images that must be loaded from external sources. Creating an Image Object The createImage( ) method has the following two forms: The 1 st form returns an image produced by imgProd , which is an object of a class that implements the ImageProducer interface (there's not enough time this semester to talk about image producers). The 2 nd form returns a blank (empty) image that has the specified width and height. Here is an example: This creates an instance of Canvas , then calls the createImage( ) method to make an Image object. At this point, the image is blank. Image createImage(ImageProducer imgProd ) Image createImage(int width , int height ) Canvas c = new Canvas( ); Image test = c.create.Image(200, 100)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Loading an Image The other way to obtain an image is to load one using getImage( ) . When getImage is invoked, it launches a separate thread of execution in which the image is loaded (or downloaded from the Internet): The 1 st version returns an Image object that encapsulates the image found at the location specified by url. The 2 nd version returns an Image object that encapsulates the image found at the location specified by url and having the name specified by imageName . An Image is where the actual pixels to be displayed are stored. Every image has an associated graphics context (i.e., an object of class Graphics that enables drawing to be performed). Images are first manipulated internally within your applets, then painted to the screen subsequently with drawImage( ) . Image getImage(URL url ) Image getImage(URL url , String imageName )
Background image of page 2
Displaying an Image Once you have an image, you can display it by using drawImage( ) , which is a member of the Graphics class: This displays the image passed in imgObj with its upper-left corner specified by left , top . imgOb is a reference to a class that implements the ImageObserver interface. An image observer is an object that can monitor an image while it loads. It will be discuss later. Here is a sample applet that loads and displays a single image: import java.awt.*; import java.applet.*; import java.net.*; public class SimpleImageLoad extends Applet { Image img; URL coach; public void init( ) { try {coach = new URL("http://www.cs.sbcc.cc.ca.us/~rhd/"); } catch (MalformedURLException e) { showStatus("Exception: " + e.toString()); } img = getImage(coach, "coach2c.gif"); } public void paint(Graphics g) { g.drawImage(img, 0, 0, this); } } boolean drawImage(Image imgObj , int left , int top , ImageObserver imgOb )
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ImageObserver When the applet begins to run you can see the image gradually load. This is because the ImageObserver interface calls paint( ) as it receives image data. With ImageObserver , you can monitor the loading of an image. An applet can then use the time it takes to load the image to do other things in parallel. When the image has been fully loaded, the entire image is then painted to the screen.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/12/2009 for the course CS 120 taught by Professor Robertdependahl during the Fall '09 term at Santa Barbara City.

Page1 / 15

Week14 - Chapter 24 Image Fundamentals There are three...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online