| |
The previous example made the simple assumption that the images it displayed
had image files that were located in its current default working
directory. However, in real programs this is not a typical (or
particularly good) strategy.
An image file might be found in a number of places:
- In a directory on a local disk
- In a directory on a remote disk
- Somewhere in a network or on the Internet.
- Contained within a ZIP or JAR file, together with the Java program's
classes.
The Toolkit class actually has two overloaded getImage
methods:
Method |
Description |
public abstract Image getImage(String filename) |
Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or
PNG. |
public abstract Image getImage(URL url) |
Returns an image which gets pixel data from the specified URL. The pixel data referenced by the specified URL must be in one of the following formats: GIF, JPEG or PNG |
The first version of the method is the simplest to use, but tends to be
rather inflexible.
The second requires the use of a URL (Uniform Resource Locator), the form of
which may be familiar to you from the address field in your browser. The
most common forms of URLs are:
http://hostname[:port]/path
file:///filespec
- and several other forms
A URL is represented in Java by the URL class, which allows to specify the
location of an image file on a disk, on a network or the Internet, or within a
ZIP or JAR file.
A very common usage is to specify the location of an image file relative to the
location of a Java class file within the program that is displaying the
image. Here's an example of this usage:
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 java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
class ImageURLDisplayPanel extends JPanel
{
public ImageURLDisplayPanel()
{
Toolkit tk = Toolkit.getDefaultToolkit();
URL remoteURL = null;
URL localURL = null;
try
{
// Image file from the Internet
remoteURL = new URL("http://members.lycos.co.uk/pinkwall/memor.jpg");
// Image file located relative to the current class file
Class mainClass = ImageURLDisplay.class;
localURL = mainClass.getResource("../images/monalisa.jpg");
}
catch (Exception e)
{
e.printStackTrace();
}
// Get the images
m_dali = tk.getImage(remoteURL);
m_davinci = tk.getImage(localURL);
// Track them until loaded
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(m_dali, 0);
tracker.addImage(m_davinci, 1);
try
{
tracker.waitForID(0);
tracker.waitForID(1);
}
catch (InterruptedException ex)
{
}
}
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 Dali's Persistence of Memory
int cx = in.left + 5;
int cy = in.top + 5;
g.drawImage(m_dali, cx, cy, this);
// Draw DaVinci's Mona Lisa
cx += m_dali.getWidth(this) + 5;
g.drawImage(m_davinci, cx, cy, this);
}
private Image m_dali;
private Image m_davinci;
}
class ImageURLDisplayFrame extends JFrame
{
public ImageURLDisplayFrame()
{
setTitle("ImageURLDisplay");
setSize(650, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.add( new ImageURLDisplayPanel() );
}
}
public class ImageURLDisplay
{
public static void main(String[] args)
{
ImageURLDisplayFrame frame = new ImageURLDisplayFrame();
frame.setVisible(true);
}
}
|
which results in the following:
Again, assuming that the image files are found in the proper places.
|