tags:

views:

35

answers:

1

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?

+1  A: 

you need to repaint each time you change the image.

Oh, and it should be done by the swing event handling thread:

Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
 new Runnable() {
  @Override
  public void run() {
    SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
        changeImage();
        repaint();
       }
    };
  }
}, 0, 5, TimeUnit.SECONDS);

UPDATE To correct a few other issues

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);
    }

    private void changeImage() {
        final BufferedImage img = nextImage();
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                image = img;
                repaint();
            }
        });
    }

    public void paint(Graphics g) {
        if (image != null) {
            g.drawImage(image, 0, 0, null);
        }
    }

    private BufferedImage nextImage() {
        String name = pictures.get(index);
        try {
            index++;
            index %= pictures.size();
            File input = new File(name);
            return ImageIO.read(input);
        } catch (IOException ie) {
            Logger.getLogger("").log(Level.SEVERE,
                    "No adds found in given path: " + name);
            return null;
        }
    }
}
Maurice Perry
How? I don't have the graphics object. Is it constant for the frame? Or may change once in a while?
Udi
You probably don't want to do the file I/O on the EDT.
Tom Hawtin - tackline
Thanks for the comments guys. Maurice - I used your code, and it starts great, but after 5 seconds (on the second iteration..) the frames is repainted all around creating a mess... Why is that?
Udi
Never mind - found the problem.. super.paint(g) was missing..Thank you guys, very helpful
Udi