trouble loading images in Applet

UPDATE: both methods currently work in the AppletViewer but not Internet Exploder

I am having a heck of a time loading images in my applet. Since the project has just begun, I’m launching from a directory on my computer. Using JRE version 1.4.2 Java HotSpot™ Client VM, No proxy.

The first problem I noticed is that Applet.getDocumentBase() is returning the wrong thing. It’s supposed to return a URL to the directory that the HTML page lives in, but no, it returns a URL to the HTML page itself. Fine, I’ll cull the real base out of the URL. I put together a real document base URL and try Applet.getImage().

It never worked. I draw the image and there’s just nothing there. I switched my code around to use a MediaTracker ala nonnus29’s game tutorial ( thanks! ) but now I just know that I have errors. I’m really stuck on this one. How do I find out what the error was? Why is my documentBase screwed up?

Perhaps even more interesting is what happens when I load the image from the jar. Using my old fade-in Splash Screen code I got some weird results. It opens the image and “reads” it without error, but the image loaded is mostly screwed up. You can tell it came from my image file, but more than half of it looks like stretched out pixels. But it fades in nicely! Sure glad to see my rendering loop and bufferstrategy work cause not much else is.

this always prints “Error loading Image”

            URL docBase = getDocumentBase();
        String docPath = docBase.toString();
        if ( !docPath.endsWith("/") ) {
              // java gave us the file name, not the actual base
              // maybe this only happens with files
              int pos = docPath.lastIndexOf('/');
              docPath = docPath.substring(0, pos ); 
        }
        URL realDocBase = null;
        try {
              realDocBase = new URL( docPath );
              
        } catch (MalformedURLException mfux ) {
              mfux.printStackTrace();
              throw new RuntimeException( "cannot open images" );
              
        }
        System.out.println( realDocBase );
          MediaTracker t = new MediaTracker (this);
          image = getImage(realDocBase,getParameter("splash_image" ));
          t.addImage(image,0);
          try  {
              t.waitForAll();
          }
          catch (InterruptedException e) {}
          if (t.isErrorAny())  {
              System.out.println("Error loading Image");
          }

And this loads a broken image from the jar

private void loadImage() {
          BufferedImage background = null;
          URL imageURL = getClass().getClassLoader().getResource( "logo2.jpg");
          try {
              background = ImageIO.read( imageURL );

          } catch ( IOException e ) {
              System.err.println( "couldn't load image" );
              e.printStackTrace();  //To change body of catch statement use Options | File Templates.
          }
          compatible = graphicsConfig
            .createCompatibleImage(background.getWidth(), background.getHeight(),
                Transparency.BITMASK);

          backBuffer = graphicsConfig
                    .createCompatibleImage(background.getWidth(), background.getHeight(),
                        Transparency.BITMASK);

          Graphics g = null;
          try {
              g = compatible.getGraphics();
              g.drawImage(background, 0, 0, null);
          } finally {
              if (g != null)
                  g.dispose();
          }
          background = null;

     }

Thanks!

Gross! The problem I was having with Applet.getImage(…) was that I wasn’t using forward slashes!

Wrong:
Correct:

I still have no idea why I couldn’t load from the jar, but now it doesn’t really matter.

Cheers!

From your statement, it looks like the problem was that you WERE using forward slashes.

/ == forwardslash

\ == backslash

SpuTTer == anal about back and forward slashes

Feel free to slash me.

But you should be using forward slashes. The CORRECT path should have “blah/happy/joy”

Backward slashes are only used on DOS/WINDOWs. Everyone else, thus the web, and all URLs and everything in something universal like an APPLET tag should use normal forward slashes.

Yup. It must be that I’m running off a directory in XP using Internet Explorer.

Any thoughts on the jar’d image decoding all chunky?

I have only seen bugs in reference to ImageIO loading from a JAR. Something about peeking into the input stream to see how many bytes are available and getting a low number because the next block hasn’t been decompressed from the JAR… thus ImageIO screws up.

You aren’t using ImageIO so it could be something completely different.

[quote]You aren’t using ImageIO so it could be something completely different.
[/quote]
Actually I am, in the second block of code. That same code always works from a doubleclick jar which contains the jpg, but it seems that something is different when it’s run in the browser. Again, it works in the appletviewer, so I suppose it’s the plugin JVM or IE generally mucking with me.

[quote]Actually I am, in the second block of code. That same code always works from a doubleclick jar which contains the jpg, but it seems that something is different when it’s run in the browser. Again, it works in the appletviewer, so I suppose it’s the plugin JVM or IE generally mucking with me.
[/quote]
Might be the ImageIO bug. You could try the following workaround in the second block:


background = ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("logo2.jpg") )); 

Regarding forward & backward slashes…
Try something like java.io.File.separator (a String) or java.io.File.separatorChar (a primitive char).

In case you still care :-/

I tried this (with the right filename ), but background remains null after the try and then the code blows up. Wild. Well, I’m off 'til Tuesday celebrating my new Junior Member status all weekend! woohoo!

/duncan

[quote]Regarding forward & backward slashes…
Try something like java.io.File.separator (a String) or java.io.File.separatorChar (a primitive char).

In case you still care :-/
[/quote]
The thing is, the slashes I’m talking about are in the HTML of the page that launches my applet ??? But I suppose if I was writing a JSP and used File.separatorChar I’d get the correct slash and things would be cool.

The issue is likely that you aren’t running a real web server to test this… just the FILE:// protocol and likely IE is going to require windows path separators.