Please let me know if you need more clarification.final String pat.pdf
1. Please let me know if you need more clarification.
final String path = "/opt/"; path should be proper
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Example extends JPanel implements ActionListener {
private static final int N = 4;
private final List list = new ArrayList();
private final Timer timer = new Timer(1000, this);
Example(String image1,String image2,String image3) {
this.setLayout(new GridLayout(N, N));
BufferedImage bi = null;
BufferedImage bi1 = null;
BufferedImage bi2 = null;
try {
bi = ImageIO.read(new File(image1));
bi1 = ImageIO.read(new File(image2));
bi2 = ImageIO.read(new File(image3));
} catch (IOException e) {
e.printStackTrace();
}
2. list.add(new JLabel(new ImageIcon(bi)));
list.add(new JLabel(new ImageIcon(bi1)));
list.add(new JLabel(new ImageIcon(bi2)));
createPane();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
timer.start();
}
private void createPane() {
this.removeAll();
for (JLabel label : list) add(label);
this.validate();
}
@Override
public void actionPerformed(ActionEvent e) {
Collections.shuffle(list);
createPane();
}
public static void main(String[] args) {
final String path = "/opt/";
final ArrayList list = new ArrayList();
for (int i=1; i<52; i++) {
list.add(new Integer(i));
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
Collections.shuffle(list);
String image1[]={"","",""};
for (int i=0; i<3; i++) {
image1[i] = path + list.get(i) +".jpg";
System.out.println(image1[i]);
}
3. new Example(image1[0],image1[1],image1[2]);
}
});
}
}
Solution
Please let me know if you need more clarification.
final String path = "/opt/"; path should be proper
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Example extends JPanel implements ActionListener {
private static final int N = 4;
private final List list = new ArrayList();
private final Timer timer = new Timer(1000, this);
Example(String image1,String image2,String image3) {
this.setLayout(new GridLayout(N, N));
BufferedImage bi = null;
BufferedImage bi1 = null;
BufferedImage bi2 = null;
try {
4. bi = ImageIO.read(new File(image1));
bi1 = ImageIO.read(new File(image2));
bi2 = ImageIO.read(new File(image3));
} catch (IOException e) {
e.printStackTrace();
}
list.add(new JLabel(new ImageIcon(bi)));
list.add(new JLabel(new ImageIcon(bi1)));
list.add(new JLabel(new ImageIcon(bi2)));
createPane();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setVisible(true);
timer.start();
}
private void createPane() {
this.removeAll();
for (JLabel label : list) add(label);
this.validate();
}
@Override
public void actionPerformed(ActionEvent e) {
Collections.shuffle(list);
createPane();
}
public static void main(String[] args) {
final String path = "/opt/";
final ArrayList list = new ArrayList();
for (int i=1; i<52; i++) {
list.add(new Integer(i));
}
EventQueue.invokeLater(new Runnable() {
@Override
5. public void run() {
Collections.shuffle(list);
String image1[]={"","",""};
for (int i=0; i<3; i++) {
image1[i] = path + list.get(i) +".jpg";
System.out.println(image1[i]);
}
new Example(image1[0],image1[1],image1[2]);
}
});
}
}