• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 208
  • Last Modified:

Loading image arrays

I'm supposed to be doing a crazy eights game for a school project but I am so stunned when it comes to java.

Anyhow, I need to create an image array for the cards but I find that the more I search for this info, the more confused I get. It would be greatly appreciated if someone could walk me through how to do this. If you have any other tips or anything for me, I'd love to hear what they are....I'm sure I could use lots of them.
0
liah00
Asked:
liah00
  • 15
  • 11
  • 8
2 Solutions
 
objectsCommented:
Easiest is tou create an array of ImageIcon's and initrialise them in a for loop.
0
 
objectsCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
objectsCommented:
Let me know if you have any q's or get stuck :)
0
 
liah00Author Commented:
Wow...I can't belive how fast that was! Thank you so much. I'll take a look at this stuff, play around and I'll let you know
0
 
objectsCommented:
no worries :)
0
 
aozarovCommented:
If you want to look at Java source code for that game and grab some ideas (about images loading, AI, ...)
you can use this  link http://www.inanutshell.us/projects/UNOSource.zip
the link was taken from http://www.inanutshell.us/html/projects.php
0
 
liah00Author Commented:
You sure have your work cut out for you if you continue helping me. Haha.  I'll see if I can explain myself a little better now that you've given me a bit of a boost. (Brace yourself)

I have a folder with 53 card images...52 cards plus an image for the card back. I somehow need to create the array to load the images. Since I don't want to type the names for 53 images, I'm assuming I'd have to loop it somehow (is this possible?). I'm not sure how to point to the folder and say "load all of these images" without typing each individual one out.

If I'm going about this the wrong way, feel free to say so. I'm always open for suggestions. We did a card game in class but all of the cards were in one image and it just didn't make sense to me to do it that way.
0
 
aozarovCommented:
You can do:
File file = new File("images_folder");
File.list();
to list the files in that directory
0
 
aozarovCommented:
Typo: file.list(); which returns an array of fileNames (see http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html)
0
 
aozarovCommented:
I am sure game above faces the same problems so you can always have a peek to see how it is done there ;-)
0
 
aozarovCommented:
Once you got the list of files then you can travers it and apply the link http://www.javaalmanac.com/egs/java.awt/LoadImage.html (which was given by objects) to load each individual file.
You can store those files in array and use them as you need.
0
 
liah00Author Commented:
Thanks, I'm checking it out. I tend to make things harder than they are.
0
 
objectsCommented:
Number your card images eg. card0.jpg, card1.jpg etc, and use something like the following to load them:

ImageIcon[] images = new ImageIcon[53];
for (int i=0; i<53; i++)
{
   images[i] = new ImageIcon(getClass().getResource("images/card"+i+".jpg"));
}

This assumes the images are in a subdirectory of the directory containing your class.
0
 
aozarovCommented:
The loading part is pretty simple, the games logic is that one that needs to concern you most ;-)
0
 
aozarovCommented:
The same concept but loading from any given folder:

File file = new File(<provide_the_folder_name_here>);
String[] files = file.list(); // list can also get FilenameFilter to consider only files with specific name
ImageIcon[] images = new ImageIcon[files.length];

for (int i = 0; i < images.length; i++)
       images[i] = new ImageIcon(files[i]);
0
 
objectsCommented:
> The same concept but loading from any given folder:

Problem with that approach is you do not know which image is for what card
Naming your card images appropriately removes this problem (and simplifies the code).
You also know you only know up front you want to load 53 images.
0
 
aozarovCommented:
Based on liah00 comments
"I have a folder with 53 card..." and "since I don't want to type the names for 53 images"
It seems that he knows which folder holds the images and what images are there (including name pattern).
He can always apply the FilenameFilter if need to.
0
 
liah00Author Commented:
ok, this could be a stupid question, but instead of ("images/card"+i+".jpg"),  could I not say ("images/card*"+i+".jpg")? Or does the * work with java?
0
 
aozarovCommented:
if you take ("images/card"+i+".jpg"), approach then why do you need the "*". you are inside a loop.
0
 
objectsCommented:
> could I not say ("images/card*"+i+".jpg")? Or does the * work with java?

No you can't, and you wouldn't want to.
The loop will handling looping thru the 53 images.
0
 
aozarovCommented:
is "card" not the full name of the image (without the number and the suffix)?
0
 
aozarovCommented:
If you used the list method with a FileNameFilter then you can write filter to match only files that prefix with "card"
0
 
liah00Author Commented:
no, there are 53 cards in the folder named "card0.gif" through "card53.gif"
0
 
objectsCommented:
thats 54 cards :)
0
 
liah00Author Commented:
Haha sorry..0-52
0
 
objectsCommented:
the code I posted earlier will load them fine for you then :)
0
 
aozarovCommented:
if you go with ("images/card"+i+".jpg") in that case then you may want to change it to:

("images/card"+(i < 10 ? "0" : "") + i+".jpg")
0
 
aozarovCommented:
Sorry, I though you prefix with "0" hence "01".
Forget what I just said.
0
 
liah00Author Commented:
Hahaha I love it when men fight over me (even if it's just for my points). And no...I'm not a guy.

Ok..so
>images[i] = new ImageIcon(getClass().getResource("images/card"+i+".jpg"));
 
what is this getClass() stuff? I'm geting errors with it:
"non-static method getClass() cannot be referenced from a static context"

I told you you had your work cut out for you ;) Java is, and always has been my enemy. I know so little and understand even less.
0
 
aozarovCommented:
change getClass() with the <name of your class>.class
0
 
aozarovCommented:
You are calling it from a static method (probably main, so you can't call getClass which is instance method)
0
 
liah00Author Commented:
Thanks guys. It took me a while to get my code sorted out, but I finally got it working. I'm sure you'll see me on here again before I finish this project.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 15
  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now