Image Display Example
Home ] Up ] [ Image Display Example ] Finding Image Files ] Using ImageIO ]

 

 

Here's an example of displaying images.

The trickiest thing about displaying images in Java is figuring out how to find the image file, and figuring out what class to use to access that image.

In a Java application, we use the Toolkit class's getImage method to load the image.  Untrusted Java applets are not allowed to access the local browser user's disks;  they are only allowed to access files from the host from whence the Java applet was launched. Consequently, applets have some convenience methods that are usually used to access images.  Those images are usually located in some standard place on the host, relative to the location of the HTML page file in which the applet is embedded.

It is important to realize that image loading is an asynchronous process.  Image loading is not guaranteed to be complete when the getImage method returns.  The reason for this is that images can be large, and we might be loading them from a slow source, such as over a network or from the Internet.

As a result, we need to determine whether the image loading has completed before we assume that we can start displaying that image.  We use the MediaTracker class to accomplish this.

package swingExamples;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MediaTracker;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

class ImageDisplayPanel extends JPanel
{
  public ImageDisplayPanel()
  {
    // Load the images
    Toolkit t = Toolkit.getDefaultToolkit();
    m_image1 = t.getImage("Neonhd.gif");
    m_image2 = t.getImage("Marqhd.gif");
    m_image3 = t.getImage("Bleye.gif");
    MediaTracker tracker = new MediaTracker(this);
    tracker.addImage(m_image1, 0);
    tracker.addImage(m_image2, 1);
    tracker.addImage(m_image3, 2);
    try
    {
      tracker.waitForID(0);
      tracker.waitForID(1);
      tracker.waitForID(2);
    }
    catch (InterruptedException ex)
    {
      // Ignore this possibility
    }
    // Once we get here, the images should be loaded
  }
  
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);
    
    Dimension d = getSize();
    Insets in = getInsets();
    int clientWidth = d.width - in.right - in.left;
    int clientHeight = d.height - in.bottom - in.top;
    
    // Draw Neon image
    int cx = in.left;
    int cy = in.top;
    g.drawImage(m_image1, cx, cy, this);
    
    // Draw Marquee image
    cy += m_image1.getHeight(this) + 10;
    int image2Width = m_image2.getWidth(this);
    int image2Height = m_image2.getHeight(this);
    cx = (clientWidth - image2Width)/2 + in.left;
    g.drawImage(m_image2, cx, cy, this);
    
    // Draw Bloodshot eye image
    int image3Width = m_image3.getWidth(this);
    int image3Height = m_image3.getHeight(this);
    cx = (cx - image3Width)/2;
    cy += (image2Height - image3Height)/2;
    int cx3 = cx;
    int cy3 = cy;
    g.drawImage(m_image3, cx, cy, this);
    
    // Copy Bloodshot eye image multiple times.
    cx = in.left;
    cy += image2Height + 20;
    for (int i = 0; i <= clientWidth/image3Width; i++)
    {
      g.copyArea(cx3, cy3,
          image3Width, image3Height,
          i * image3Width - cx3, cy - cy3);
    }
  }
  
  private Image m_image1, m_image2, m_image3;
}

class ImageDisplayFrame extends JFrame
{
  public ImageDisplayFrame()
  {
    setTitle("ImageDisplay");
    setSize(500, 300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contentPane = getContentPane();
    contentPane.add( new ImageDisplayPanel() );
  }
}

public class ImageDisplay
{
  public static void main(String[] args)
  {
    ImageDisplayFrame frame = new ImageDisplayFrame();
    frame.setVisible(true);
  }
}

which produces the following:

Assuming the appropriate images are found in the proper locations on disk.

 

This page was last modified on 02 October, 2007