Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  trouble loading images in Applet  (Read 1550 times)
0 Members and 1 Guest are viewing this topic.
Offline duncanIdaho

Junior Member




invert mouse


« Posted 2004-05-26 00:06:33 »

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(TM) 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"
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
            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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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!
Offline duncanIdaho

Junior Member




invert mouse


« Reply #1 - Posted 2004-05-26 01:13:21 »

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

Wrong: <PARAM name="splash_image" value="splash/images/tgen_logo2.jpg">
Correct:  <PARAM name="splash_image" value="splash\\images\\tgen_logo2.jpg">

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

Cheers!
Offline SpuTTer

Senior Member


Medals: 1


Lazy Middle Class Intellectual


« Reply #2 - Posted 2004-05-26 06:32:17 »

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.

Sacramento Volleyball
"Whitty phrase goes here."
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #3 - Posted 2004-05-27 19:03:09 »

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.

Offline duncanIdaho

Junior Member




invert mouse


« Reply #4 - Posted 2004-05-27 19:10:07 »

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?
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #5 - Posted 2004-05-27 19:24:53 »

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.

Offline duncanIdaho

Junior Member




invert mouse


« Reply #6 - Posted 2004-05-27 19:42:31 »

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

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.
Offline tom
« Reply #7 - Posted 2004-05-27 23:42:18 »

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.


Might be the ImageIO bug. You could try the following workaround in the second block:

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

Offline D.t.O

Junior Member




Psych'd about Java Games


« Reply #8 - Posted 2004-05-27 23:56:41 »

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 :-/

Enjoy.
Regards,
     - D.t.O
Offline duncanIdaho

Junior Member




invert mouse


« Reply #9 - Posted 2004-05-27 23:56:44 »

Quote


Might be the ImageIO bug. You could try the following workaround in the second block:

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


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  
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline duncanIdaho

Junior Member




invert mouse


« Reply #10 - Posted 2004-05-27 23:59:14 »

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 :-/


The thing is, the slashes I'm talking about are in the HTML of the page that launches my applet Huh  But I suppose if I was writing a JSP and used File.separatorChar I'd get the correct slash and things would be cool.
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #11 - Posted 2004-05-28 02:52:21 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (37 views)
2014-04-15 18:08:23

BurntPizza (33 views)
2014-04-15 03:46:01

UprightPath (48 views)
2014-04-14 17:39:50

UprightPath (31 views)
2014-04-14 17:35:47

Porlus (48 views)
2014-04-14 15:48:38

tom_mai78101 (69 views)
2014-04-10 04:04:31

BurntPizza (129 views)
2014-04-08 23:06:04

tom_mai78101 (229 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (200 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!