<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);
}
}