Thursday 14 May 2015

Images in Java

  The developers of Java knew that working with images is a critical part of any modern programming language with a goal of implementing user interfaces that meet the criteria of users. Because Java is platform independent, though, it couldn't use any of the platform-specific formats, such as the Mac's PICT standard. Fortunately, there already are two platform-independent formats: GIF and JPEG. These formats are especially nice because they are compressed so that transmitting them takes less of the limited network bandwidth. The AWT supports both these compression formats, but it uses neither of them internally. Although you can read in GIF and JPEG files, they are converted into images, which are just bitmaps. All the work you do in Java with images is based on the Image class. For some strange reason, even though there is a special package for image-manipulation-related classes (java.awt.image), the Image class itself resides in the top-level java.awt package.
Image is an abstract class designed to support bitmapped images in a platform-independent manner. Although this class provides just the basics for working with images, it does have several methods you'll find useful.
Java uses a model of image producers and image consumers. Image producers generate pixels from a file or Image object, and image consumers use and/or display those pixels. Both ImageConsumer and ImageProducer are Java interfaces. AWT comes with ImageProducers for reading from local files and URLs, arrays in memory, and Image objects. It also comes with CropImageFilter, RGBImageFilter, and PixelGrabber, which implement the ImageConsumer interface.
Graphics, getGraphics()
Returns the graphics object for this image. You can use this to draw to the image using the drawing methods discussed in the Graphics object section, "Graphics Class."
int, getHeight(ImageObserver imgobs)
Returns the height of the image. The ImageObserver class monitors the construction of the image-as it's loaded over the Internet, for example. The ImageObserver is notified when the image is fully loaded into memory if it isn't at the time this method is called.
Object, getProperty(String prop_name, ImageObserver imgobs)
Returns the value of one of the image properties. The specific properties depend on the original image format. Three properties are supported by Java:
comments: A string full of information defined by the person who made the image file.
croprect: If a Java croprect filter is used, this holds the boundary of the original image.
filters: If Java image filters are used to make the image, they are listed here.
ImageProducer, getSource()
Returns an instance of ImageProducer, which serves as a source for the pixels in an image.
int, getWidth()
Returns the width of the image.


No comments:

Post a Comment