Chapter 1 included a simple applet named Slideshow, which implemented the timed sequential display of several images. In this section, I convert Slideshow into a Java Bean, first changing its design to make it more generic:
The SlideshowEvent class is defined by the following code.
package coyote.bean;
import java.util.*;
import java.awt.*;
//
//
// SlideshowEvent
//
//
public class SlideshowEvent extends EventObject
{
//-------------------------
// Fields
//-------------------------
private Image m_image;
//-------------------------
// Constructors
//-------------------------
public SlideshowEvent(Object obj, Image img)
{
super(obj);
m_image = img;
}
//-------------------------
// Properties
//-------------------------
public Image getEventImage()
{
return m_image;
}
}
And a SlideshowListener class will implement this interface.
package coyote.bean;
//
//
// SlideshowListener
//
//
public interface SlideshowListener
{
void handleSlideshowEvent(SlideshowEvent event);
}
The new class, SlideshowBean, follows.
package coyote.bean;
// Import class packages
import java.applet.*;
import java.awt.*;
import java.beans.*;
import java.util.*;
public class SlideshowBean
extends Canvas
implements Runnable
{
//-------------------------
// Fields
//-------------------------
// Properties
private long m_delay = 5000;
private int m_nImages;
private Image m_Images[];
private int m_nCurImage;
private int m_nImgWidth;
private int m_nImgHeight;
private Vector m_listeners;
// Thread support
Thread m_Slideshow = null;
//-------------------------
// Constructors
//-------------------------
public SlideshowBean()
{
m_nImages = 0;
m_nImgWidth = 0;
m_nImgHeight = 0;
m_nCurImage = 0;
m_listeners = new Vector();
start();
}
public SlideshowBean(Image [] imgs)
{
m_listeners = new Vector();
setImages(imgs);
start();
}
protected void finalize()
{
stop();
}
//-------------------------
// Thread methods
//-------------------------
private void start()
{
m_Slideshow = new Thread(this);
m_Slideshow.start();
}
public void stop()
{
if (m_Slideshow != null)
{
m_Slideshow.stop();
m_Slideshow = null;
}
}
//-------------------------
// Properties
//-------------------------
public long getDelay()
{
return m_delay / 1000L;
}
public void setDelay(long sec)
{
m_delay = sec * 1000L;
}
public Image [] getImages()
{
return m_Images;
}
public void setImages(Image [] imgs)
{
m_Images = imgs;
m_nImages = imgs.length;
m_nImgWidth = m_Images[0].getWidth(this);
m_nImgHeight = m_Images[0].getHeight(this);
m_nCurImage = 0;