Using pictures in applets
Created Nov 11, 2011
Just like "adding" sound files to an applet you can add pictures. Now I want to explain to you how to do this by adding a background picture to our "ball bounce" applet. Again we have to declare an instance variable, an Image object called backImage:
-
Image backImage;
Then we load a picture to our applet and store it in the object reference backImage (import java.applet.* and java.net.*!!). This works very similar to loading sound files, please add these lines to your init() - method:
-
// load file Land.jpg
backImgage = getImage (getCodeBase (), "Land.jpg");
As a last step we have to paint this picture in the paint() - method of the applet:
-
// drawing the image: g.drawImage (name, x- value, y - value, frame)
g.drawImage (backImage, 0, 0, this);
Sourcecode download
Take a look at the applet
Next chapter
Handling mouse events