/* Test platform for analyzing Image load problems using MediaTracker methods. Two .jpg files are loaded in a separate Thread (no real need for this); Once complete, repaint() is called to display the two images. Two applet parameters specify the file names of the graphics files, which should be placed in the DocumentBase(). Maybe someone can come up with a "damaged" file to see what will happen? Diagnostic info on the image load operations is written to System.out (Java Console). Jim Henry 3/8/03 */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.net.*; //***************************************************************************** public class MediaTest extends Applet implements Runnable, ActionListener { Thread runner; MediaTracker tracker; Button loadB; Image im1, im2; String imageName1, imageName2; boolean okToShow = false; //--------------------------------------------------------- public void init() { loadB = new Button("Load"); add(loadB); loadB.addActionListener(this); imageName1 = getParameter("image1"); imageName2 = getParameter("image2"); } //--------------------------------------------------------- public void actionPerformed(ActionEvent e) { if (e.getSource() == loadB) { if (runner == null) { runner = new Thread(this); runner.start(); } } } //--------------------------------------------------------- public void run() { Object[] errArray; tracker = new MediaTracker(this); try { // change these filenames to cause some errors... im1 = getImage(new URL(getDocumentBase(), imageName1)); im2 = getImage(new URL(getDocumentBase(), imageName2)); } catch (MalformedURLException e) { System.out.println("getImages failed"); } tracker.addImage(im1, 0); tracker.addImage(im2, 0); try { tracker.waitForID(0); } catch (InterruptedException ie) { System.out.println("waitForID: " + ie.getMessage()); } // Now check to see if all Images tracked by this MediaTracker have // finished loading. If there is an error while loading or scaling an image, // then that image is considered to have finished loading. // So it does NOT detect any errors. // Since we just did a waitForID(), this will always be true...? if (tracker.checkAll()) System.out.println("checkAll() returns true"); else System.out.println("checkAll() returns false"); // Now check the status of all the Images loaded. isErrorAny() returns // true if any of the images had an error. Inside the 'true' branch, // further checks are made as to the status of the loads. if (tracker.isErrorAny()) { System.out.println("isErrorAny returns true: there was an error"); // statusAll() Calculates and returns the bitwise inclusive OR of // the status of all media that are tracked by this media tracker. // This is ANDed with the static final ERRORED to see if an ERROR // ocurred. A "true" argument says "start loading any images that // are not yet being loaded." "false" says not to. // Similarly for the other three status conditions. // if any image had an error... if ((tracker.statusAll(false) & MediaTracker.ERRORED) != 0) { System.out.println("ERRORED condition true"); } else { System.out.println("ERRORED condition false"); } // if any image was aborted... if ((tracker.statusAll(false) & MediaTracker.ABORTED) != 0) { System.out.println("ABORTED condition true"); } else { System.out.println("ABORTED condition false"); } // if the load operation is complete (errors or not)... if ((tracker.statusAll(false) & MediaTracker.COMPLETE) != 0) { System.out.println("COMPLETE condition true"); } else { System.out.println("COMPLETE condition false"); } // if images are still loading... if ((tracker.statusAll(false) & MediaTracker.LOADING) != 0) { System.out.println("LOADING condition true"); } else { System.out.println("LOADING condition false"); } // Now look to see which Images caused the error(s): getErrorsID() specifies // the MediaTracker group id. It returns an array of references to Objects // that had problems. errArray = tracker.getErrorsID(0); System.out.println("Number of errors is: " + errArray.length); // display info for each "bad" image: Java internal ID and our reference name. // Note that the Object array refs == our Image refs for (int i = 0; i < errArray.length; i++) { System.out.println("Problem image is: " + errArray[i].toString()); if (errArray[i] == im1) System.out.println("im1 had a problem"); if (errArray[i] == im2) System.out.println("im2 had a problem"); } } else System.out.println("isErrorAny() returns false: no errors"); // go ahead and show images regardless of errors. okToShow = true; repaint(); } //--------------------------------------------------------- public void paint(Graphics g) { if (okToShow) { g.drawString("In paint(): images ARE loaded", 50, 50); // If any of these did not load successfully, they will not display, // but no other problem seem to occur. im1 and im2 are not null, but // they might not have successfully loaded. g.drawImage(im1, 20, 70, this); g.drawImage(im2, 100, 260, this); // Just playing with stretching... /* int offset = 100; g.drawImage(im2, offset, offset, offset + im2.getWidth(this)*2, offset + im2.getHeight(this)*2, 0, 0, im2.getWidth(this), im2.getHeight(this), this); */ } else { g.drawString("In paint(): images NOT yet loaded: click on Load", 50, 50); } } }