Displaying Images

To use Java to display images, you must first get the image, and then you must draw it. The Java Applet class provides methods for getting images, and the Java Graphics class provides methods for drawing them.

Java's Applet class provides two getImage() methods, listed here:

public Image getImage(URL url)
public Image getImage(URL url, String name)

In the first method listed, you provide a URL class. You can just type the URL surrounded by double quotation marks, or you can create a URL object and then pass it to the getImage() method. If you do the latter, be sure to import the URL class. The URL class is part of the java.net package.

Whichever way you pass the URL, the first method takes the whole path, including the filename ...

Get Platinum Edition Using XHTML™, XML, and Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.