

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import objectdraw.*;

/*
 * Slide show designed to illustrate the use of arrays.
 */
public class Slides extends WindowController implements ActionListener {

    private static final int NUM_SLIDES = 8; // number of slides to be shown
    private static final double X_IMAGE = 50; // coordinates to display slides
    private static final double Y_IMAGE = 50;
    private int counter; // keeps track of which slide is showing
    private JButton next, previous;     // buttons to go to next and previous slides
    
    private VisibleImage[] slides; // array of drawable images for slides

    // Set up drawable images for display.  Only show first.  Create buttons for moving
    // through slides.
    public void begin() {
        new FilledRect(0,0,canvas.getWidth(),canvas.getHeight(),canvas).setColor(Color.BLUE);
        
        // Load the images
        Image[] images = new Image[NUM_SLIDES]; 
        images[0] = getImage("andrea.jpg");
        images[1] = getImage("bill.gif");
        images[2] = getImage("brent.jpg");
        images[3] = getImage("duane.jpg");
        images[4] = getImage("jeannie.jpg");
        images[5] = getImage("morgan.jpg");
        images[6] = getImage("steve.jpg");
        images[7] = getImage("tom.jpg");

        // Create (and hide) drawable images
        slides = new VisibleImage[NUM_SLIDES];
        for (int slideCounter = 0; slideCounter < NUM_SLIDES; slideCounter++) {
            // Create the visible image
            slides[slideCounter] = new VisibleImage(images[slideCounter], 
                                                   X_IMAGE, Y_IMAGE, canvas);
            slides[slideCounter].hide();
        }
            
        // Create next and previous buttons and add their 
        // listeners
        next = new JButton(">");
        next.addActionListener(this);
        previous = new JButton("<");
        previous.addActionListener(this);

        // Put the buttons on a panel below the canvas where
        // the slides appear.
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(previous);
        buttonPanel.add(next);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        this.validate();

        // Set up first slide
        counter = 0; 
        slides[counter].show();
    }

    // Depending on button clicked on, go to next or previous slide
    public void actionPerformed(ActionEvent event) {
        // Hide current slide
        slides[counter].hide();
        
        // get next (or previous) slide (wrapping if necessary)
        if (event.getSource() == next) {
            counter = (counter + 1) % NUM_SLIDES;
        } else {
            counter = (counter + NUM_SLIDES - 1) % NUM_SLIDES;
        }

        // Show the next one
        slides[counter].show();
    }
}
