I am trying to create a panel with changing pictures. This is my panel:
public class AdvertisementPanel extends JPanel {
private BufferedImage image;
private ArrayList<String> pictures;
private int index = 0;
public AdvertisementPanel(String... pics) {
pictures = new ArrayList<String>(Arrays.asList(pics));
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
new Runnable() {
@Override
public void run() {
changeImage();
}
}, 0, 5, TimeUnit.SECONDS);
}
public void paint(Graphics g) {
g.drawImage(image, 0, 0, null);
}
private void changeImage() {
String name = pictures.get(index);
try {
File input = new File(name);
image = ImageIO.read(input);
index++;
index %= pictures.size();
} catch (IOException ie) {
Logger.getLogger().log(Level.SEVERE,
"No adds found in given path: " + name);
}
}
I have a frame that holds the panel, but no pictures are shown. Tried to repaint periodically from the frame - caused some funny, yet unwanted results... Any ideas why? What am I doing wrong? How should I refresh the frame's components?