|
Java is also making
it easy to deal with images. It contains classes and methods to load and
display images in Applets. JPEG and GIF formatted images are supported
by Java. We are just providing the URL of the image.
To load an image,
we can use more than one way:
If you know the exact
URL for the image you wish to load, you can load it like this:
URL imageURL = new
URL("http://www.prenhall.com/logo.gif");
Image img = getImage(imageURL);
You can compress this
into one line as follows
Image img = getImage(new
URL( "http://www.prenhall.com/logo.gif"));
The getImage() method
is provided by java.applet.Applet so this works inside applets. Outside
of applets you can use the getImage method from the default toolkit instead,
like this:
URL imageURL = new
URL("http://www.cs.bilkent.edu.tr/bilkent.gif");
Image img = Toolkit.getDefaultToolkit.getImage(imageURL);
If you don't know the
exact URL of the Image but you do know its name and that it's in the same
directory as the applet, you can use an alternate form of getImage that
takes a URL and a filename. Use the applet's getCodeBase() method to return
the URL to the applet directory like this:
Image img = getImage(getCodeBase(),
"test.gif");
The getCodeBase() method
returns a URL that points to the directory where the applet came from.
Finally if the image
file is stored in the same directory as the HTML file, use the same getImage
method but pass it getDocumentBase() instead. This returns a URL that points
at the directory which contains the HTML page in which the applet is embedded.
Image img = getImage(getDocumentBase(),
"test.gif");
If an image is loaded
from the Internet, it may take some time for it to be fully downloaded.
Most of the time you don't need to worry about this. You can draw the image
as soon as you've connected it to a URL. Java will update it as more data
becomes available without any further intervention on your part.
Load all the images
your applet needs in the init() method. In particular you do not want to
load them in the paint() method. If you do they will be reloaded every
time your applet repaints itself.
Once you loaded the
image, you can draw it by:
public void paint(Graphics
g) {
g.drawImage(img,
0, 0, this);
}
To wait for an image
to be completed, we use imageUpdate() method which repaints the applet
for each update on the image. Also, we use ImageObserver class, which has
fields that are used to monitor image loading task.
Java also allows playing
audio. The audio file must be 8-bit, mono, mu-law encoded .au file. Audio
can be played inside an applet only. Besides this restrictions, this more
than nothing !
We are giving the
URL of the sound file and it starts playing. If we want to play the sound
continuously, then we assign this URL to an audio clip and use loop() method.
To stop the sound, we just use stop() of audio class.
AudioClip ac
= getAudioClip(new URL( "http://www.URL.com/music.au"));
Then,
continuously plays the
clip given in URL.
When you're ready
to stop playing the clip, invoke its stop() method like this
That's all.
|