I am trying to make a very simple program for a school assignment.
I simply need to display 3 random icons of playing cards.
All the icons are stored in an image file as 1.png-52.png
Is there anyway to do this with out identifying each individual image, adding it to an array and using math.random to select one.
If I had like 10 images I could understand using that method... but 52?

Any help would be greatly appreciated

amm

if php
$imagename = rand(1, 52) . ".png";
then load imagename

You can try Random object in java and concat string [number] + ".png"

Haha ..
to day. i try learning java sorry about php code

I could,... but wouldnt I still have to write 52 lines of very similar code to define each image file 1-52?


I know that I "could" do something like the following code ( times 10)
but what im asking is if there is an easier way

ImageIcon java = new ImageIcon("java.jpg");
ImageIcon 1 = new ImageIcon("1.png");
ImageIcon 2 = new ImageIcon("2.png.");
ImageIcon 3 = new ImageIcon("3.png.");
ImageIcon 4 = new ImageIcon("4.png.");
ImageIcon 5 = new ImageIcon("5.png.");
//constructing array
int[] images = new int[6];
images[0] = 1;
images[1] = 2;
images[2] = 3;
images[3] = 4;
images[4] = 5;
images[5] = 6;

Edited 7 Years Ago by geek_till_itMHZ: added code

Well if you think about it, all of the images share similarities in their filenames except for the numeric designation. You can determine that by using rand right? So you can loop 52 times, assigning that number to a predetermined string. Truebot showed us how to do it in PHP. You just need to transform that code into java. Your pseudo code would look something like:

string base = ".png";
for(int i = 0; i < 52;i++)
{
   int  number = randomly generated number between 1 - 52;
//ensure that all numbers returned are unique!
    string filename = (convert int number to string; add the string base to the end of it;)
  load / display filename;
}

That sounds like the purpose of this exercise. It sounds like your instructor wants you to be comfortable with creating strings on the fly. I'm assuming that you've been introduced to string functions like concat, etc. You should have enough course materials to be able to flesh out the psuedo code into full-fledged java.
One thing to note with the above code however, You will need to generate unique values. Typically random generators don't pay attention to the fact if they've already generated a value. You will need to play around with that.

u want display only 3 icon ?

why you prepare each of array la'
you just create 3 element of icon array
and random 3 number from 1 - 52
then you can create 3 image filename like "1.png" , "30.png" , "5.png"

that ok for display 3 icons ?

This article has been dead for over six months. Start a new discussion instead.