LoaderDemo.java

Download this source file

 Download image.gif

// Loader demo - shows how to load resources from either a JAR file
// or from a directory, and shows the resulting URLs.
//
// Written 4/2003 by Wayne Pollock, Tampa Florida USA.

import java.applet.*;
import java.awt.*;
import java.net.URL;

public class LoaderDemo extends Applet
{
   private TextArea ta;

   public  void init ()
   {
      Toolkit tk = Toolkit.getDefaultToolkit();
      Class me = null;;
      String myName = "";
      URL imageURL1 = null, imageURL2 = null;
      Image im1 = null, im2 = null;

      setLayout( new BorderLayout() );
      ta = new TextArea();
      ta.setFont( new Font( "Monospaced", Font.PLAIN, 14 ) );
      add( ta, "Center" );

      try
      {
         me = getClass();
         myName = me.getName();
         if ( ! myName.toLowerCase().endsWith( ".class" ) )
            myName = myName + ".class";

         imageURL1 = new URL( getCodeBase(), "image.gif" );
         im1 = tk.getImage( imageURL1 );

         imageURL2 = me.getResource( "image.gif" );
         im2 = tk.getImage( imageURL2 );
      }
      catch ( Exception e )
      { ta.append( e.toString() );  }

      ta.append( "URL of " + myName + " is: "
         + me.getResource( myName ) + "\n\n" );

      ta.append( "URL of \"image.gif\" "
         + "via getCodeBase() is: " + imageURL1  + "\n\n" );

      ta.append( "URL of \"image.gif\" "
         + "via getResource() is: " + imageURL2  + "\n\n" );
   }
}

Send comments and questions to pollock@acm.org.
Valid HTML 4.01!   Valid CSS!   CAST: Bobby WorldWide Approved 508   CAST: Bobby WorldWide Approved AAA