Results 1 to 10 of 10
  1. #1
    Daunlessk is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Loading Images Issue

    Hello. I am new to the forums. Figure this will be a great resource for help when I'm stuck. I'm new at java. Anyway. I have a class called imageLoader that creates a constructor that upon being initialized, will perform the loadImages() methid, which simply loads all images and goes through setting animation frames and times.

    This works fine for the most part. However, my problem is that when I want to call the imageLoader to get a particular image, I'm not sure a) how to tell it exactly which image to return and b) when I tell it to return bg, it SAYS bg cannot be resolved to a variable even though it was initilized with a loadImage(string fileName) call in the loadImages() method earlier. Why can it not see that?

    Thanks for the help!

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Loading Images Issue

    would be helpful to see your code..

  3. #3
    Daunlessk is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Loading Images Issue

    public class ImageLoader
    {
    private Animation spriteAnim;
    private long elapsedTime;

    public ImageLoader()
    {
    this.loadImages();
    }

    public void loadImages()
    {
    Image bg = loadImage("images/wallpaper.jpg");

    //extra code for other images and animations
    }

    public Image getBg()
    {
    return bg;
    }

    }

    How can I also make a call to getImage(Image image) will return the Image I supplied. For example, if I call ImageLoader.getImage(bg) it will return the bg image, or if I call ImageLoader.getImage(sprite) it will return sprite? Basically so I don't have to make a get____() for each particular image that I will ever need to call.

  4. #4
    Daunlessk is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Loading Images Issue

    I guess though that my first question, that may answer anything else is why does it tell me that bg is not a variable when it was declared in loadImages() as an Image?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Loading Images Issue

    Quote Originally Posted by Daunlessk View Post
    I guess though that my first question, that may answer anything else is why does it tell me that bg is not a variable when it was declared in loadImages() as an Image?
    You've answered your own question -- it's because it is declared in loadImages(). Because of this, it is local to this method and only visible inside of this method.

  6. #6
    Daunlessk is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Loading Images Issue

    Quote Originally Posted by Fubarable View Post
    You've answered your own question -- it's because it is declared in loadImages(). Because of this, it is local to this method and only visible inside of this method.
    Okay thanks! I fixed it with a simple intialization outside the method. Here is my second question. I'd like to change the getbg() to a getImage(image) call so its the same code, but i can input which image to get instead of created a get method for each picture needed. What is a good simple way I can make a call to getImage without doing something like this below?

    Java Code:
    public Image getImage(int image)
    	{
    		if (image == 1)
    		{
    			return bg;
    		}
                    else if (image == 2)
                    {
                            return someRandomImage;
                    }
    		else
    		{
    			return someRandomImage2;
    		}
    	}
    I see that with the more images I have, the more ridiculous this list would be, and seems a bit mundane to basically assign every single image a number. Not only would I have to memorize which number of the image i'm trying to call, the getImage method would be a realllllly long block of code if I had 70 pictures or whatever.
    Last edited by Daunlessk; 11-06-2012 at 12:05 AM.

  7. #7
    Daunlessk is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Loading Images Issue

    Any suggestions please??

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Loading Images Issue

    Put the Images in a collection of some type, be it an ArrayList if you want to get the Image based on a contiguous int, or a HashMap if you wish to get the images using a String or other object as the retrieval key.

  9. #9
    Daunlessk is offline Member
    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    Default Re: Loading Images Issue

    I think a hashmap is exactly what I was looking for. Thank you for pointing me in the right direction!

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Loading Images
    By wired-in=p in forum New To Java
    Replies: 9
    Last Post: 02-09-2012, 12:53 AM
  2. Problems with loading images in a jar
    By Reskaillev in forum New To Java
    Replies: 2
    Last Post: 10-10-2011, 09:34 PM
  3. Loading Images
    By TacoManStan in forum Java Gaming
    Replies: 9
    Last Post: 09-23-2011, 12:18 AM
  4. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 10:33 AM

Posting Permissions

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