For school I need to make a game dealing [X] random cards.
I used this code to buildup and display my images.
There is only one thing going wrong. When displaying the project, only the last card is
displayed.
Is there any way I can show these images next to each other?
public static void main(String[] args) throws FileNotFoundException, IOException {
Cards cards = new Cards();
int dealSize = 6;
deal = cards.getShuffledCards(dealSize);
System.out.println("Deal of 4 randomly picked cards " + deal);
f = new JFrame();
for(int i=0; i < dealSize; i++) {
card = deal.get(i).toString();
f.getContentPane().add(setLabel(card));
}
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocation(200,200);
f.setVisible(true);
}
public static JLabel setLabel(String fileLocation) throws FileNotFoundException, IOException {
InputStream file = new BufferedInputStream(
new FileInputStream([DIRECTORY TO IMAGES]" + fileLocation));
BufferedImage image = ImageIO.read(file);
label = new JLabel(new ImageIcon(image));
return label;
}
You would probably have to make an arbitrary grid, in which one card can occupy each part. Like a GridLayout or GridBagLayout.
From what I can see by skimming your code, you are basically overwriting the last element you put, for each iteration.