// // another example of the Java AWT, showing loading and display of // an image. // see class definition for details. // import java.awt.* ; import java.io.* ; // // a ShowImage object is a DrawingFrame that displays an image // loaded from a file (GIF and JPEG formats are supported). // the initial size of the display is just big enough to // contain the image; if the window is resized, the image is // scaled to fit. // class ShowImage extends DrawingFrame { private Image frameImage ; ShowImage(String imageFile) { super("ShowImage") ; // start loading image. frameImage = getToolkit().getImage(imageFile) ; System.out.println("loading image ....") ; // wait for completion. waitForImage(frameImage) ; System.out.println("done") ; // get size of loaded image and use to set // initial size. int w = frameImage.getWidth(this) ; int h = frameImage.getHeight(this) ; setDrawingSize(new Dimension(w, h)) ; show() ; } // 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) { // position origin to avoid frame borders. translateDrawingArea(g) ; // get size of frame, minus borders. Dimension d = getDrawingSize() ; // draw scaled version of image. g.drawImage(frameImage, 0, 0, d.width, d.height, this) ; } } // updates onscreen image (no need to clear first, just paint). public void update(Graphics g) { System.out.println("update called") ; paint(g) ; } // // ---- main --------------------------------------------------- // // command-line argument: file from which to load image. // public static void main(String[] args) { if (args.length < 1) { System.out.println("Argument is filename") ; System.exit(-1) ; } File f = new File(args[0]) ; if (!f.exists() || !f.canRead()) { System.out.println("Filename not found") ; System.exit(-1) ; } ShowImage s = new ShowImage(args[0]) ; } }