ava applets can be embedded into a Web page to produce colorful, interactive Web sites. In this article, you will learn to write Java applets that load images and play sounds to give your Web users a true multimedia experience.
To start, write a "Hello world" message onto an applet window, which will allow the user to make a picture appear at any point by simply clicking on that point. See the code for this message in Listings 1 (Java) and 2 (HTML).
init() portion of the code calls a method called
getDocumentBase(), which returns the full URLup to the directory nameof the applet itself. The applet can then load other files that are stored with it, such as an image.
The image filename is passed in as a parameter, so the applet has to call
getParameter() to get the filename. Then
getImage(), a method built in to the Applet class, is called.
GetImage() takes two parameters, a base URL and the image filename, and concatenates them together to give the full URL to the image itself. Because you will store the image in the same directory as the applet on either your hard drive or a Web server, the base URL of the image will be the same as that of the applethence the call to
GetImage() returns an object of type java.awt.Image, which can hold and display images. Using
getImage() will load a picture into the Image object for later use.
(See Sidebar: Parameter Passing in Applets)