The Graphics Object

(java.awt.Graphics)
Related Material From SUN Tutorials:

Drawing Methods

Image Methods

Graphics Objects are also capable of displaying images:

Notice that drawImage requires an Image Object and an ImageObserver object. You can retrieve an image using getImage:

Typically, a getImage is performed in the init() method, and the image is displayed in the paint() method. There are six drawImage methods in the Graphics class

Example:

More about mouse events and MouseListeners later.

Note the this: who is observing me...loading this file...

Audio Clips

Java also has built-in methods to play audio clips. No need for the remote computer to have an audio player; Java will do the playing. (The remote computer does need audio-capable hardware though.)

Playing a clip

The easiest way to listen to an audio clip is through the play method:

play (URL soundDirectory, String soundfile);

or simply,

A common URL to use with the play() method is the directory where the HTML files are located. You can access that location through the Applet method getDocumentBase(): (this gets the absolute URL - of place of HTML file that loaded my applet)

For this to work, your class file and the cuckoo.au file must be in the same directory.

Looping an Audio Clip

You can also treat audio clips like images. You can load them up and draw them later.

To load an audio clip:

Once a clip is loaded, you can use one of three methods with it: play, loop, and stop.

To play the clip:

To start the clip playing and have it loop:

To stop a running clip:

Simple Looping Example:

Mouse Input

One of the most useful features Java supports is direct interactivity. Your application can pay attention to the mouse and react to changes to the mouse. (Physical events trigger)

The most common event is the click. This event is governed by the two methods: mousePressed() and mouseReleased(). Mouse methods are "heard" through MouseListeners so your code needs to implement the Listener. MouseListener is an interface in java.awt.event.

Things to try:

Create an applet that will display the duke image wherever you click the mouse. Make the html wrapper , compile and test.

Modify this to add a sound whenever you click the mouse

Finally: Common Applet Problems (and Their Solutions) This page also has links to Common Component Problems, Common Layout Problems, and Common Graphics Problems.