Image.createImage definitely not working...
I have some incredibly simple code below because I was trying to see what the heck was wrong with createImage...
public class Viewer extends MIDlet
{
protected void startApp() throws MIDletStateChangeException
{
Image anImage;
try {
anImage = Image.createImage("/011.png");
}
catch (IOException e) {
e.printStackTrace();
}
}
protected void pauseApp(){ }
protected void destroyApp(boolean arg0) throws MIDletStateChangeException { }
}
It gives me an error and it is kind of strange. 011.png DOES exist and was created using PSP. I tried it in a /res folder and in the root folder of the project. I've read the forums here, but nothing has helped me so far. Can anyone tell me what could be going on here?
According to the Java 1.5 API (http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Image.html), there's no such method in the Image class. In fact, Image is an interface, so you couldn't invoke methods statically even if such a method DID exist. Therefore, your compiler error (if it indeed is a compile error) is caused by calling an undefined method.
However, given that I don't know what the MIDlet class is (because it's not in the Java 1.5 API at all), you may be using some package I've never heard of. In that case, check the API for that package.
I've got the same problem but with Sun ONE Studio 4 ME. I've already checked all the MIDP/CLDC Tutorials I could find, and they all give close to this example. (Image img = CreateImage("/img.png")) All of them finaly end up with img = (null). Me too have tried the \res folder, I suppose I am making a very dumb mistake, but I'm stuck with no ideas. Can't one of You gurus write a simple example? Pls!!
> anImage = Image.createImage("/011.png");
> 011.png DOES exist and was created using PSP.
> I tried it in a /res folder and in the root folder of
> the project.
Have you tried putting 011.png in the root directory of your hard drive? That's what a single / in front of a filename usually means. Try taking that slash out, or replacing it with "res/" if you want the images in a "res" folder. (Assuming that "res" folder is a subdirectory of your Java project directory.)
I too am unfamiliar with the Image.createImage method. What I always use in my applets is something like this:
Image image = getImage(getCodeBase(),"filename.jpg");
I think getImage is a method of the Applet class, but I don't remember.
I know that is an OLD post, but is still first on Google ranking today, so will be good to have a solution also at this time :)
I found this problem when i was working with an IDE (JBuilder) and the problem was that i needed to BUILD to classes target also the content of the RES folder (all resurces must be added to project, and set to COPY)
That will solve the problem in my case.
Bye
P鄐cal