×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    34

    createImage and passing strings to it!

    This has to be a classic beginners problem, but I can't solve it!

    I need to load 10 or so image files. I used to have this:

    try{
    myimage = Image.createImage( "/graphics.gif");
    }catch(IOException ioex){
    dPrint("Exception " + ioex + " attempting to load " + sFileName);
    }

    and it worked. But it's a bit tacky having 10 of those, so I parameterized it, so I pass in a string. Declaration:

    public Image loadImage(String sFileName)

    then an init:

    Image imTemp=null; // this is needed to prevent `possibly unused var `errors

    and the offending line:

    imTemp = Image.createImage(sFileName);

    It seems the filename is being passed in properly, but all I get assigned to imTemp is a null.

    What am I doing wrong? I just want a function where i pass in a filename as a string, and have it return an image containing the result of createImage.
    Last edited by Poldie; 2006-05-23 at 00:20.

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    49

    Re: createImage and passing strings to it!

    Some handset does'nt support gif image, try image in png format again.

  3. #3
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: createImage and passing strings to it!

    Hi Poldie,
    If you want to create 10 images you must have 10 image objects.Otherwise the last image that you create will be there in the imageObject myimage...

    Thanks
    Soku

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    34

    Re: createImage and passing strings to it!

    Quote Originally Posted by badgunnokia
    Some handset does'nt support gif image, try image in png format again.
    I'm using the emulator, not my phone. My phone (n70) does support gif.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    34

    Re: createImage and passing strings to it!

    Quote Originally Posted by soku123
    Hi Poldie,
    If you want to create 10 images you must have 10 image objects.Otherwise the last image that you create will be there in the imageObject myimage...

    Thanks
    Soku
    I want to have the method return an image, which I then run createimage on again to store in an array of images. All I want the function to do is to perform createimage on a filename and return a temporary image which i pass out as the return value.

  6. #6
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: createImage and passing strings to it!

    Hi Poldie,
    check by adding the /res/to the image name.I suppose you are keeping the images in the res folder of your project.

    Thanks
    Soku

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    34

    Re: createImage and passing strings to it!

    Quote Originally Posted by soku123
    Hi Poldie,
    check by adding the /res/to the image name.I suppose you are keeping the images in the res folder of your project.

    Thanks
    Soku
    Thanks, but there's nothing wrong with the location, or file format. It works when I hard code the filename and load 1 image, and now I want to change the function so that I can load a file whose filename is passed in as a string, and I want the function to return the image which has just been loaded.

  8. #8
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: createImage and passing strings to it!

    Hi Poldie,
    your CreateImage() creates an immutable image.

    The Image class has two forms: mutable and immutable. The immutable image is created when another image (or the resource containing the image) is supplied to the createImage() method; while a mutable image is created when only the image dimensions are supplied.

    Thanks
    Soku

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    12

    Re: createImage and passing strings to it!

    Hi Poldie,
    What you are trying to do looks ok so I would
    like to see a bit more code and then put print
    statements in to see what is going on.
    Regards
    Don

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    34

    Re: createImage and passing strings to it!

    Quote Originally Posted by donscales
    Hi Poldie,
    What you are trying to do looks ok so I would
    like to see a bit more code and then put print
    statements in to see what is going on.
    Regards
    Don
    I think I've got it working. There's something funny about the way my old copy of Paint Shop Pro saved my gifs. I loaded them all into Paint and then saved them again and those ones work fine. I did a file diff between them and there are some difference, but they're binary, so god knows what. It's not the classic `save as a jpg but give the filename the gif extension` bug which catches people out sometimes though!

    I've reworked my code substantially in the process - I've got my image loader working now, and I convert the loaded image to a sprite immediately, so I don't have two calls to createImage any more. I have, however, lost the transparency I had before, so I'll have to arse around with a graphics program a little later on.

    In case you're wondering, I'm basically writing the game Vexed/Puzznic in J2me. It's out there on sourceforce, only it's in C or C++ or something, but I'm not using any of their code/logic, apart from the logic to decrypt the level data. This is my first java program, and it's going ok so far. I might release the source at some point so people can pick apart my code and tell me how to do it properly!

    Thanks for your help, perhaps I'll ask some more stupid questions later on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×