//    File:  SlideShow.java - display a "slide show" of images in the pix subdirectory

//    HTML parameters:
//      delay  = dwell time in milliseconds
//      images = image file names, .gif or .jpg; must be in this, or child subdirectory

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;        // for JApplet and Timer

public class SlideShow extends JApplet {

    public void init() {
        codeBase = getCodeBase();
        setBackground(Color.WHITE);

        images = getParameter("images");
        next();

        String sDelay = getParameter("delay");
        int nDelay = DELAY;
        if (sDelay != null)
            nDelay = Integer.parseInt(sDelay);
        if (0 < nDelay && nDelay < 10000)
            DELAY = nDelay;
        timer = new Timer(DELAY, new TimerActionListener());
        timer.start();
    }

    public void paint(Graphics g) {
        showStatus("");
        imageWidth   = image.getWidth(this);
        imageHeight  = image.getHeight(this);
        windowWidth  = getSize().width;
        windowHeight = getSize().height;
        /* temp code for use in adjusting size and aspect ratio...
        float xRatio = ((float) imageWidth)  / windowWidth;
        float yRatio = ((float) imageHeight) / windowHeight;
        float fRatio = Math.max(xRatio, yRatio);
        */
        float fRatio = 1.0F;
        g.drawImage(image, 0, 0, (int) fRatio * windowWidth, (int) fRatio * windowHeight,
                           0, 0, imageWidth,  imageHeight,  this);
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, windowWidth-1, windowHeight-1);
        g.setColor(Color.WHITE);
        g.drawRect(1, 1, windowWidth-3, windowHeight-3);
        g.setColor(Color.BLACK);
        g.drawRect(2, 2, windowWidth-5, windowHeight-5);
    }

    /**
     * Get the next image.  The image URLs are obtained
     * from the "images" attribute.  You can specify a list
     * of them by separating the images file names by '|'s. <p>
     * Note that the URL is constructed relative to the
     * getDocumentBase(), that is because the URL is obtained
     * from within the document.
     */

    private void next() {
        url = images;
        if (images.indexOf('|') >= 0) {
            int start = index;
            if ((index = images.indexOf('|', index)) < 0) {
                url = images.substring(start);
                index = 0;
            } else {
                url = images.substring(start, index++);
            }
            url = url.trim();
        }
        if (url.length() > 0) {
            image = getImage(codeBase, url);
            repaint();
        } else
            next();
    }

    private class TimerActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            next();
        }
    }

    static final long serialVersionUID = 0;
    private java.net.URL codeBase;
    private Image image;
    private Timer timer;
    private int windowWidth = 0, windowHeight = 0;
    private int imageWidth    = 0, imageHeight  = 0;
    private String images, url;
    private int index = 0;
    private int DELAY = 1500;
}
