/* (@)JImageSlidePanel.java */
/* Copyright 2009 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.*;
/**
* TODO: Javadoc me!
*
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
* @since Playground-3.8
*/
public class JImageSlidePanel extends JPanel implements ActionListener {
private int repeatInterval = 1000;
private final List<Image> images = new ArrayList<Image>();
private final Timer slideTimer = new Timer(1000, this);
private final MediaTracker mediaTracker = new MediaTracker(this);
private int currentImageIndex = -1;
private Image currentImage;
private final ImageObserver imgObs = new ImageObserver() {
public boolean imageUpdate(
Image img,
int infoflags,
int x,
int y,
int w,
int h) {
repaint();
return false;
}
};
public void setImages(Image... images) {
final Image[] old = getImages();
if (slideTimer.isRunning()) {
slideTimer.stop();
}
for (Image image : this.images) {
mediaTracker.removeImage(image);
}
this.images.clear();
this.images.addAll(Arrays.asList(images));
for (int i = 0; i < images.length; i++) {
mediaTracker.addImage(images[i], i);
}
mediaTracker.checkAll(true);
currentImageIndex = -1;
currentImage = null;
if (!slideTimer.isRunning() && images.length > 1) {
slideTimer.setInitialDelay(repeatInterval);
slideTimer.setDelay(repeatInterval);
slideTimer.start();
}
firePropertyChange("images", old, images); //$NON-NLS-1$
showNextImage();
}
public Image[] getImages() {
return images.toArray(new Image[images.size()]);
}
public void actionPerformed(ActionEvent e) {
showNextImage();
}
private int nextImageIndex() {
final int count = images.size();
for (int i = (currentImageIndex + 1) % count, j = 0; j < count; j++, i =
(currentImageIndex + j + 1) % count) {
if (mediaTracker.checkID(currentImageIndex)) {
return i;
}
}
return -1;
}
private void showNextImage() {
final int imageIndex;
if (images.isEmpty()) {
currentImage = null;
currentImageIndex = -1;
} else if ((imageIndex = nextImageIndex()) != -1) {
System.out.println("next image: " + imageIndex);
currentImage = images.get(currentImageIndex = imageIndex);
} else if (currentImageIndex == -1) {
currentImage = images.get(currentImageIndex = 0);
}
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
final Image img = currentImage;
if (img != null) {
final Insets insets = getInsets();
g.drawImage(img, insets.left, insets.top, imgObs);
}
}
/**
* Test main method.
*
* @param args ignored
* @throws MalformedURLException
*/
public static void main(String[] args) throws MalformedURLException {
final JImageSlidePanel slidePanel = new JImageSlidePanel();
final Toolkit tk = Toolkit.getDefaultToolkit();
final String[] urlSpecs =
{
"http://upload.wikimedia.org/wikipedia/commons/d/d9/Barns_grand_tetons_YCbCr_separation.jpg",
"http://upload.wikimedia.org/wikipedia/commons/3/3e/Phalaenopsis_JPEG.png",
"http://www.java-forum.org/avatars/ebenius.gif?dateline=1235377736",
"http://www.java-forum.org/avatars/sparrow.gif?dateline=1241546043",
"http://www.java-forum.org/avatars/faetzminator.gif?dateline=1239030115", };
final Image[] images = new Image[urlSpecs.length];
for (int i = 0; i < urlSpecs.length; i++) {
images[i] = tk.getImage(new URL(urlSpecs[i]));
}
slidePanel.setImages(images);
final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
contentPane.add(slidePanel);
final JFrame f = new JFrame("Test Frame: JImageSlidePanel"); //$NON-NLS-1$
f.setContentPane(contentPane);
f.setSize(640, 480);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
}