I want Swing to arbitrarily select some images from a folder and display them.
I tried something like this, but images are not displayed.
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class RandomCards extends JFrame
{
RandomCards()
{
setLayout(new FlowLayout(FlowLayout.LEFT, 25, 10));
Map<Integer, String> hm = new HashMap<Integer, String>();
int noOfImage=3;
for(int i=0; i < noOfImage; i++)
{
hm.put(i, "resources/" + i + ".png");
}
double cardNumber = Math.floor(Math.random()*3) + 1;
add(new JLabel(hm.get(cardNumber)));
}
public static void main (String [] args)
{
RandomCards frame = new RandomCards();
frame.setSize(330, 150);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
source
share