// File: TestAwt15.java (Module 9) // // Author: Rahul Simha // Created: October 13, 1998 // // Image tracking via MediaTracker. import java.awt.*; import java.net.*; class NewFrame extends Frame { // Constructor. public NewFrame (int width, int height) { // Set the title and other frame parameters. this.setTitle ("Gif image"); this.setResizable (true); this.setBackground (Color.cyan); this.setSize (width, height); // Show the frame. this.setVisible (true); } // Override paint(): public void paint (Graphics g) { Toolkit tk = Toolkit.getDefaultToolkit (); try { URL u = new URL ("http://www.seas.gwu.edu/~simha/canyon.jpeg"); Image img = tk.getImage (u); // Create an instance of MediaTracker and // pass a Frame instance as parameter (via "this"). MediaTracker mt = new MediaTracker (this); // Add the image instance to the tracker. mt.addImage (img, 1); // We will use a "wait" (clock) cursor while the // image is loading. this.setCursor (Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR)); // Ask the MediaTracker to wait. try { mt.waitForID (1); } catch (InterruptedException e) { System.out.println (e); } // Now the image has been loaded. // Return to default cursor. this.setCursor (Cursor.getPredefinedCursor (Cursor.DEFAULT_CURSOR)); // Draw the complete image. g.drawImage (img, 0, 0, this); } catch (MalformedURLException e) { System.out.println (e); } } // "paint" } // End of class "NewFrame" public class TestAwt15 { public static void main (String[] argv) { NewFrame nf = new NewFrame (300, 200); } }