Home : Course Map : Chapter 6 : Java :
Course Map
Chapter 6

  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Text Draw 
  Demo 10
  Demo 11

  Demo 1
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
  Demo 4
  Demo 5   Demo 6
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Topic Index
     Course Guide
     What's New

We will introduce basic image handling here and discuss this and other aspects of images in Java in greater detail in Chapter 11.

For basic image operations you deal with images as instances of the class


(In Chapter 11 we will discuss the more capable BufferedImage subclass of Image.)

As of Java 1.4 you can load and draw image files encoded as JPEG, GIF and PNG. With Java 5.0 you can also load bitmap formats BMP and WBMP.

To load an image in an applet, you can use one of the overloaded getImage() methods for loading from a URL:

  Image img = getImage("http://www.myschool.edu/anImage.gif");

or :

  Image img = getImage(getCodeBase(),"anImage.gif");

where the Applet class method getCodeBase() provides the URL for the location of the applet's class file. The file name in the second argument is appended to this URL. (See Chapter 13 for information about the java.net.URL class.)

For an application, use

  Image img = Toolkit.getDefaultToolkit().getImage(URL or file path);

The Toolkit is a class in the java.awt package that provides various resources and tools for the display system. One of the Toolkit methods is getImage() that functions much like the getImage() method in the Applet class. It is overloaded to take either a String filename parameter specifying the location of the image file or a URL parameter identifying the image file.

Before calling getImage(), one must have a reference to the Toolkit instance in use. The static method Toolkit.getDefaultToolkit() returns a reference to that Toolkit.

You can obtain an image from a JAR file by using the getResource() static method from the Class class:

  URL url = this.getClass().getResource("anImage.gif");
  Image img = Toolkit.getDefaultToolkit().getImage(url);


  URL url = YourClass.class.getResource("anImage.gif");
  Image img = Toolkit.getDefaultToolkit().getImage(url);

Note: Image is an abstract class so you are actually loading an instance of a concrete subclass of Image with the getImage() methods above. However, you always treat the object as an instance of Image, i.e. you only have access to its methods.

Drawing Images

To draw an image, use the drawImage() method in the Graphics context:

   g.drawImage(img, x, y, this);

Here, img is a reference to an instance of Image, x and y are the coordinates of the position of the top left corner of the image. The third argument is an ImageObserver reference, which is used internally as a callback to inform a program about the status of the image loading. The Component class implements the ImageObserver interface so you can just put a "this" reference for the argument as default. We discuss image loading below and in Chapter 11.

The following applet demonstrates the essentials of obtaining an image, obtaining its dimensions, and displaying it on a JPanel.

import javax.swing.*;
import java.awt.*;

/** Demonstrate drawing an image. **/
public class ImageApplet extends JApplet
  public void init () {
    Container content_pane = getContentPane ();

    // Grab the image.
    Image img = getImage (getCodeBase (), "liftoff.jpg");

    // Create an instanceof DrawingPanel
    DrawingPanel drawing_panel =  new DrawingPanel (img);

    // Add the DrawingPanel to the content pane.
    content_pane.add (drawing_panel);
  } // init

} // class ImageApplet

class DrawingPanel extends JPanel
  Image img;

  DrawingPanel (Image img)
  { this.img = img; }

  public void paintComponent (Graphics g) {
   super.paintComponent (g);

   // Use the image width & height to find the starting point
   int imgX = getSize ().width/2 - img.getWidth (this);
   int imgY = getSize ().height/2
- img.getHeight (this);

   //Draw image centered in the middle of the panel    
   g.drawImage (img, imgX, imgY, this);

  } // paintComponent

// DrawingPanel


Image Loading

The tricky part about images in Java is that they may not load instantly. The getImage() method returns immediately after it is invoked so that a program does not hang while waiting for an image to download over a potentially slow Internet line. The image loading, in fact, does not even begin until the program attempts to access (e.g. obtain the dimensions of the image) or to draw the image.

This usually does not present a problem when loading one or two small or medium sized images but could become a problem when loading several large images or many images, as in the case of an animation, over a slow link.

There are several techniques to monitor the image loading and signal when it is fully ready. We will discuss these more in Chapter 11: Java : Image Loading. We only note here that in the above example, the getWidth(), getHeight(), and drawImage() methods required an ImageObserver argument reference. The image loading machinery uses this reference to "callback" via the imageUpdate() method of the ImageObserver interface.

The callbacks occur periodically during the loading and provide information about the status of the loading.

The Component class implements the ImageObserver interface and overrides the imageUpdate() method. (The "this" references in the above example points to the JPanel object.) You can override this method yourself to monitor the image loading but usually a MediaTracker object, which we will discuss in Chapter 11: Java : Image Loading, provides a simpler approach.

References & Web Resources

Latest update: Nov 8, 2006

Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3

Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
22 23 24

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.