Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (564)
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  
  Applet Deployment - Images not showing  (Read 1009 times)
0 Members and 1 Guest are viewing this topic.
Offline N1G3L2

Senior Newbie





« Posted 2012-08-28 09:45:29 »

Hello everyone,

After hours of frustration, I have finally gotten my Java applet to display on my web page, but only adding to the frustration. The problem I am having now is none of my images are displaying. Only shapes, text and colors drawn by Java are showing up.

Images in my program are called like so:
Image treeimg = getImage(getDocumentBase(), "../Images/tree.png");

...which after researching, I found out can cause problems. If that is the case, can anyone give suggestions as how to modify this in the least painful way? I'm not even 100% sure that this is the problem.

I'm also pretty new to html but I'm pretty sure I have everything in the right folders and whatnot. The game loads fine, just no images. Maybe I need to call some parameters for the applet in the html file?

I have researched pretty thoroughly and have basically come up with nothing so any help would be very appreciated =)
Offline N1G3L2

Senior Newbie





« Reply #1 - Posted 2012-08-28 21:05:42 »

Dang.. Nobody?? Oh well.. I ended up figuring it out after messing around a bit.

After researching this topic I have found tons of posts with people asking the same question, none of them ever really seeming to find the solution to their problem. I would expect there to be resources for this topic out there somewhere, but after hours of searching I found absolutely nothing. When searching 'Applet deployment' basically all that comes up, is how to embed applets in a webpage... Great... I saw person after person asking 'Why do my images not show when run through the browser, but work fine when run in my IDE?' I then saw people throwing around very vague answers like 'add image parameters in html' or 'download this applet bundling program...'

The solution is extremely simple (at least with the problem I was having) and so I will post it here, to hopefully save someone the trouble I just went through. My Images folder for my project was a separate folder, which was called as a resource through the Java Build Path. This works fine when running the project through eclipse or command line, but does not show the images when run through a browser.

So I copied my images folder into the source folder and erased the old Images folder. Then adjusted my code to read from the correct path.

From:
Image treeimg = getImage(getDocumentBase(), "../Images/tree.png");

To:
Image treeimg = getImage(getDocumentBase(), "Images/tree.png");

It now shows my images on the browser. I guess the reason is, when exporting an applet, files in the source folder are automatically compressed into the Jar file. So my understanding is that it is best to keep resources along with source (for Applets) I guess? I am sure there are other ways, but after hours of fuming over this, this seems like the easiest solution and works fine.

Anyways, I just hope someone else can use this info because it doesn't seem to me that anyone else has taken the time to explain this very well. As long as you are putting everything in the correct path, this should work fine.
Offline jonjava
« Reply #2 - Posted 2012-08-28 22:02:57 »

Have a separate folder called "res"  (or whatever) and add it to your build path.

Project -> Properties -> Java Build Path -> Source -> Add folder.

This way its added to the jar when you export it.

To get at the files you should use an URL like this:

Yes just use
1  
URL url = this.getClass().getClassLoader().getResource("resources/Ball.png");


And to load an image from that url use

1  
ImageIO.read( url );

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline N1G3L2

Senior Newbie





« Reply #3 - Posted 2012-08-28 23:48:58 »

Have a separate folder called "res"  (or whatever) and add it to your build path.

Project -> Properties -> Java Build Path -> Source -> Add folder.

This is how I had it set up before. But I did not try adjusting my code to read from a URL. I just tried it this way and it seems to work fine also, so thank you for that =). Anybody know if one way is more reliable than the other? (URL or adding images to source folder) What I am thinking is (using URL), as long as the webpage loads, using URL would be able to find the images. Or (using source folder for images) as long as the applet loads fine the images should load fine. They seem like pretty equal solutions to me, but I am not 100% sure. Anyways I'm just glad I got it working now and thank you jonjava =)
Offline jonjava
« Reply #4 - Posted 2012-08-29 00:51:48 »

Using the URL is better. Actually you're not using the URL but you're using the ClassLoader and its getResource method gives you an URL (Uniform Resource Locator) object. The URL object is a flexible way to represent a path to something as opposed to hard coded full paths.

The ClassLoader automatically looks for your resource and tries to find it in a much more flexible way than absolute paths.

Some things take an URI (Uniform Resource Identifier) which you can simply get by using the URL's toURI() method.

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.

Grunnt (23 views)
2014-09-23 14:38:19

radar3301 (14 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (30 views)
2014-09-20 20:14:06

BurntPizza (34 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (108 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!