// // another example of the Java AWT, showing loading and display of // an image. // see class definition for details. // // (applet version) // import java.awt.* ; import java.awt.image.* ; import java.io.* ; import java.applet.* ; import java.net.* ; // // a ShowImageApplet object is an Applet that displays an image // loaded from a file (GIF and JPEG formats are supported). // public class ShowImageApplet extends Applet { private Image frameImage ; private int imageWidth = -1 ; private int imageHeight = -1 ; public void init() { // start loading image System.out.println("init called") ; String imageName = getParameter("image") ; System.out.println("image name = " + imageName) ; URL codeBase = getCodeBase() ; System.out.println("code base = " + codeBase) ; frameImage = getImage(getCodeBase(), getParameter("image")) ; if (frameImage == null) { // "should" never happen, but may if we // trip over a browser bug System.out.println( "Something is wrong -- " + "getImage() returned null") ; System.exit(-1) ; } System.out.println("loading image ....") ; // wait for completion. waitForImage(frameImage) ; System.out.println("done") ; } // waits for image to finish loading. private void waitForImage(Image im) { MediaTracker tracker = new MediaTracker(this) ; // add desired image to list of things the // MediaTracker is tracking, with id = 0. tracker.addImage(im, 0) ; try { // wait for loading of image we just // added to MediaTracker's list. tracker.waitForID(0) ; } catch(InterruptedException e) { } } // creates onscreen image by scaling image from file. public void paint(Graphics g) { System.out.println("paint called") ; if (frameImage != null) { // get size of applet area Dimension appletDims = getSize() ; int w = frameImage.getWidth(this) ; int h = frameImage.getHeight(this) ; int xOffset = (appletDims.width - w) / 2 ; int yOffset = (appletDims.height - h) / 2 ; System.out.println("offsets for image = " + xOffset + ", " + yOffset) ; // draw image g.drawImage(frameImage, xOffset, yOffset, this) ; } } // updates onscreen image (no need to clear first, just paint). public void update(Graphics g) { System.out.println("update called") ; paint(g) ; } }