Struct Problem

Hey guys,

i had draw many graphics and testet thread an many other things. But i’ve got everytime the same problem.

I can just get graphics like *.jpg or *.png about these:

Image tmg = getToolkit().getImage("c://background.png");

But how can I get graphics if I had 2 package for example package main and images. So how can I get these graphics without the absolute path???

I’d need someone to verify if this is correct or not but …

You don’t have to specify absolute paths in your code. If you used:

Image img = getToolkit().getImage(“background.png”);

that would look in the current directory for background.png. If you have something in a package you would use “mypackage/background.png” or maybe “mypackage.background.png” (im not certain on the syntax)

I tried it with the realtive path

Image img = getToolkit().getImage("background.png"); 

But it didn’t work… I home I will try the package solution.

But must i be package or can it be folders?

is this an applet? If so add a “/” so


Image tmg = getToolkit().getImage("/background.png");

and the image must be in same dir as you are running your applet

No, its an Java application. But one other question…

If i put the graphics in an own package and I want that other people play the game, they can edit the graphics.

How can I prevent that people edit it. Can I decode graphics an if yes how?

it is kinda difficult to protect the images of your game. Even if you apply some sort of cryptography into the images, someone could take a screenshot of your game to steal your images/sprites/tiles.

Does anybody knows something about this subject?

[quote]loading images with toolkit is poo. If you’re using Java 1.4+, you need to be using ImageIO! Assuming background.jpg is in the same directory as the class is, you can do…


BufferedImage bg = ImageIO.read(getClass().getResource("background.jpg"));

[/quote]
You don’t want to do it that way. Have you never heard of the ImageIO bug?

This is more like it:


BufferedImage bg = ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("/background.jpg")));

i wouldn’t worry too much about people editing the graphics. if you compile it all into a .jar file, it’s likely no one will bother.