Java Reference
In-Depth Information
the paint() method. Figure 14.16 shows how the applet looks when displayed
in a browser.
The getImage() method returns immediately and does not actually look
for the image file at the given URL until the browser attempts to draw the
image on the screen. At that time, the URL is resolved, and the image is
downloaded. Any problems with the URL will not be realized until you
attempt to display the image.
import java.applet.*;
import java.awt.*;
import java.net.*;
public class ImageDemo extends Applet
{
private Image image;
private AppletContext context;
public void init()
{
context = this.getAppletContext();
String imageURL = this.getParameter(“image”);
if(imageURL == null)
{
imageURL = “default.jpg”;
}
try
{
URL url = new URL(this.getDocumentBase(), imageURL);
image = context.getImage(url);
}catch(MalformedURLException e)
{
e.printStackTrace();
context.showStatus(“Could not load image!”);
}
}
public void paint(Graphics g)
{
context.showStatus(“Displaying image”);
g.drawImage(image, 0, 0, 200, 84, null);
g.drawString(“www.javalicense.com”, 35, 100);
}}
The following imagedemo.html file embeds the ImageDemo applet, and
assigns the image parameter to a JPEF file named LogoD.jpg. Note that the
way the URL is initialized in the init() method, the image file needs to be in the
same root directory as the imagedemo.html document. This page is shown in
Figure 14.16.
Search WWH ::




Custom Search