Start by creating a shuffled list...
private List<Icon> list = new ArrayList<Icon>();
private List<Icon> shuffled;
//...
list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\e.jpg"));
list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\d.jpg"));
list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png"));
list.add(new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\f.jpg"));
shuffled = new ArrayList<Icon>();
update();
Each time your timer ticks and calls actionPerformed
, pop the first element of the shuffled
list until nothing is left...
if (shuffled.isEmpty()) {
shuffled.addAll(list);
// Prevent the current image from been selected next...
shuffled.remove(label.getIcon());
Collections.shuffle(shuffled);
}
Icon icon = shuffled.remove(0);
label.setIcon(icon);
This will remove the possibility of an image been allowed to be displayed multiple times in a row. Your random value calculation should have been more like int i1 = (r.nextInt(list.size()));
anyway...
Notes:
ImageShuffle
doesn't need to extend from JPanel
, you not adding anything to it. This means you can get rid of this.setLayout(new GridLayout(1, 0));
and f.add(this);
and the program should still run