Java Example Program/ Sample Source Code
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
public class ImageBackgroundApplet extends Applet {
private Image image;
public void init() {
getImage();
// getImageWithMediaTracker();
}
public void paint(Graphics g) {
int w = image.getWidth(null);
int h = image.getHeight(null);
g.drawImage(image, (400 - w) / 2, (400 - h) / 2, this); // centered
}
private void getImage() {
// get the image
image = getImage(getDocumentBase(), "images/draw.png");
}
private void getImageWithMediaTracker() {
// get image
image = getImage(getDocumentBase(), "images/draw.png");
// create and add Media Tracker
MediaTracker tracker = new MediaTracker(this);
// add the image to tracker and give it an id say 0
tracker.addImage(image, 0);
try {
// start loading the image with Id = 0
tracker.waitForID(0);
} catch (InterruptedException e) {
System.out.println("Error loading Image : " + e.getMessage());
}
}
} |
|
|