UrbanTwitch
asked on
Java Client Applet - Storing it AppData
If you're using Windows (and why?), you'll know when you play Java Applet games that they store their data locally to load faster when you play them again and again.
For example - take this image:
See the jar files? Inside those folders are also mp3 files.
This is the game: http://minecraft.net/game/
How do I do this for my java applet?
I have tried google but alas -- no help.
Thank you.
For example - take this image:
See the jar files? Inside those folders are also mp3 files.
This is the game: http://minecraft.net/game/
How do I do this for my java applet?
I have tried google but alas -- no help.
Thank you.
ASKER
oh so then how would i tell the jar file to download those inside the jar file instead from the internet?
theres no need to have them available anywhere but the jar so you don't need to specify which to use
To get the URL of a resource in a jar use the Class.getResource() method
http://helpdesk.objects.com.au/java/how-to-get-url-to-a-resource-in-same-directory-as-a-class
To get the URL of a resource in a jar use the Class.getResource() method
http://helpdesk.objects.com.au/java/how-to-get-url-to-a-resource-in-same-directory-as-a-class
ASKER
So where would I put the new URL i formed... inside replacing the tile%.png?
you already use getResource()
tiles[i] = ImageIO.read(getClass().ge tResource(
String.format("line_tile/t %d.png", i)));
tiles[i] = ImageIO.read(getClass().ge
String.format("line_tile/t
ASKER
wait wait wait *scratches head*... so I am already using it?
so in your case you need to include the line_tile (and any other resources) in your jar
ASKER
So it would be like this.
Correct?
Correct?
URL tile[i] = MyClass.class.getResource(String.format("line_tile/t%d.png", i));
yes
ASKER
Wait... so then how do I draw a URL? :S
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I changed
Image[] tiles;
to
URL[] tiles;
I then added import java.net.URL; with the other imports
and then I have this:
tiles = new URL[NUM_TILES];
for (int i = 0; i < NUM_TILES; i++) {
tiles[i] = tileGen.class.getResource( String.for mat("line_ tile/t%d.p ng", i));
//tiles[i] = ImageIO.read(getClass().ge tResource( String.for mat("line_ tile/t%d.p ng", i)));
}
and now I am getting this:
Image[] tiles;
to
URL[] tiles;
I then added import java.net.URL; with the other imports
and then I have this:
tiles = new URL[NUM_TILES];
for (int i = 0; i < NUM_TILES; i++) {
tiles[i] = tileGen.class.getResource(
//tiles[i] = ImageIO.read(getClass().ge
}
and now I am getting this:
C:\wamp\www\mystikrpg\tileGen.java:994: cannot find symbol
symbol : method drawImage(java.net.URL,int,int,tileGen.GamePanel)
location: class java.awt.Graphics
g.drawImage(tiles[index], 32 * col, 32 * row, this);
^
1 error
Tool completed with exit code 1
ASKER
Wait I think i found it out:
This look good?
This look good?
tiles = new Image[NUM_TILES];
for (int i = 0; i < NUM_TILES; i++) {
Toolkit tk = this.getToolkit();
tiles[i] = tk.getImage(this.getClass().getResource(String.format("line_tile/t%d.png", i)));
}
you didn't need any code change :)
> so in your case you need to include the line_tile (and any other resources) in your jar
> so in your case you need to include the line_tile (and any other resources) in your jar
ASKER
OK so now it loads up a lot faster. Around 10 seconds or so.
(we're getting here! ... time for a new Q!)
(we're getting here! ... time for a new Q!)
For resurces like images/audio its often a good idea to jar them up so its just one download