Results 1 to 4 of 4
Thread: random images on the fly??
- 03-30-2009, 07:09 AM #1
Member
- Join Date
- Nov 2008
- Location
- SEPA
- Posts
- 4
- Rep Power
- 0
<Solution Found>random images on the fly??
Hello,
I tried to search but the search feature won't work for me...
So here I am. I posted comments in the middle of the code where I have my questions. The program needs to display a group of three randomly generated cards saved as png files named as such...1.png, 2.png...54.png.
I was wondering if there is a way to avoid coding each image as a private ImageIcon that I could use the randomly generated number and maybe somehow append it to a stringBuilder to get the file name to invoke. I found a lot of stuff about Applets but I am restricted to GUI and I don't know applets yet. I can make this work with a huge list of if else statements or create a switch (which now that I think of it may not work either...dang it)
So any help is appreciated. Thanks in advance.
import java.awt.*;
import javax.swing.*;
public class RandomCards extends JFrame
{
//I want to prevent coding all 54 png files
private ImageIcon card1 = new ImageIcon("resources/1.png");
private ImageIcon card2 = new ImageIcon("resources/2.png");
private ImageIcon card3 = new ImageIcon("resources/3.png");
private ImageIcon card4 = new ImageIcon("resources/4.png");
private ImageIcon card5 = new ImageIcon("resources/5.png");
private ImageIcon card6 = new ImageIcon("resources/6.png");
private ImageIcon card7 = new ImageIcon("resources/7.png");
private ImageIcon card8 = new ImageIcon("resources/8.png");
private ImageIcon card9 = new ImageIcon("resources/9.png");
private ImageIcon card10 = new ImageIcon("resources/10.png");
private ImageIcon card11 = new ImageIcon("resources/11.png");
private ImageIcon card12 = new ImageIcon("resources/12.png");
private ImageIcon card13 = new ImageIcon("resources/13.png");
private ImageIcon card14 = new ImageIcon("resources/14.png");
private ImageIcon card15 = new ImageIcon("resources/15.png");
private ImageIcon card16 = new ImageIcon("resources/16.png");
private ImageIcon card17 = new ImageIcon("resources/17.png");
private ImageIcon card18 = new ImageIcon("resources/18.png");
private ImageIcon card19 = new ImageIcon("resources/19.png");
private ImageIcon card20 = new ImageIcon("resources/20.png");
RandomCards()
{
setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10));
for (int i = 1; i < 4; i++)
{
double x = Math.floor(Math.random()*54) + 1;
if (x == 1)
add(new JLabel(card1));
else if (x == 2)
add(new JLabel(card2));
//else if for all 54 image files???
/*Is there a better way that I can
invoke the new private imageIcon
stuff above only for the random
numbers that I generate? Or do I
have to do this for all 54 files?
I could do a switch but I still
need to invoke all 54 files. I was
trying to find some way that I could
make the file name into a string
and print it in the command to invoke
only the image file that I want.
Any help would be great. Other wise
I will just insert all of the
files...1.png, 2.png, 3.png......54.png
and do an ass load of if else or a huge
switch. Thanks*/
}
}
public static void main (String [] args)
{
RandomCards frame = new RandomCards();
frame.setTitle("Program 13.9, 3 Random Cards");
frame.setSize(330, 150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);
}
}Last edited by FunHog; 03-30-2009 at 10:20 PM. Reason: Found a solution
- 03-30-2009, 10:19 PM #2
Member
- Join Date
- Nov 2008
- Location
- SEPA
- Posts
- 4
- Rep Power
- 0
OK so here is a solution to this...
Although I don't know HashMaps yet that is where I am going with this....
At the start of the program build a map of the images, and a number as the key. For example:
Map<Integer, String> images = new HashMap<Integer, String>();
int numImageFiles=54;
for(int i=0; i < numImageFiles; i++)
{
images.put(i, "resources/" + i + ".png");
}
//Then to get the image out by the random number
double cardNumber = Math.floor(Math.random()*54) + 1;
add(new JLabel(images.get(cardNumber)));
-
Rather than a HashMap, if your index is a simple int, I'd just create a simple array of String. It appears that you'd leave the first element of the array of the Array blank since there may not be a "0" image, but that's not too hard to manage.
- 03-31-2009, 05:24 AM #4
Member
- Join Date
- Nov 2008
- Location
- SEPA
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Random Div Background Images
By mchapple in forum New To JavaReplies: 2Last Post: 01-26-2009, 01:16 AM -
Random
By koolhoney in forum New To JavaReplies: 11Last Post: 09-30-2008, 03:39 AM -
Using Random
By razmyasdfg in forum CLDC and MIDPReplies: 1Last Post: 07-27-2008, 10:47 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks