Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Load Images and Play Sounds in Java Applets

Embed Java applets that load images and play sounds into your Web pages.


advertisement
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.

Hello World
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).

The init() portion of the code calls a method called getDocumentBase(), which returns the full URL—up to the directory name—of 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 applet—hence the call to getDocumentBase().

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)



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date