Each time we run this code it displays 3 random cards out of 54. I want to call the cards1(); method(that displays 3 random cards) on mouse click. each time I click in frame 3 random cards should be displayed. Could any body please help?
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.*;
public class Cards extends JFrame {
public static void main(String[] args) {
JFrame frame = new Cards();
frame.setTitle("Cards");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Cards() {
// Create array for cards
cards1();
}
public void cards1() {
ImageIcon[] images = new ImageIcon[54];
for (int i = 1; i < images.length; i++) {
images[i] = new ImageIcon("Drawables//Images//" + i + ".png");
}
// Get random number between 1 & 54... three times
int[] threeRandoms = new int[3];
Random ran = new Random();
for (int i = 0; i < threeRandoms.length; i++) {
threeRandoms[i] = ran.nextInt(54);
}
// Labels with gridLayout
setLayout(new GridLayout(1, 4, 5, 5));
add(new JLabel(images[threeRandoms[0]]));
add(new JLabel(images[threeRandoms[1]]));
add(new JLabel(images[threeRandoms[2]]));
}
}