import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CardLayoutAnimation  extends JFrame implements ActionListener {
    private CardLayout  cardLayoutManager;

    public CardLayoutAnimation (String title) {
        super(title);
        cardLayoutManager = new CardLayout(0,0);
        setLayout(cardLayoutManager);

        // Add the 8 buttons, each with a different picture as the icon
        for (int i=0; i<4; i++) {
            JButton aButton = new JButton(new ImageIcon("Stick" + i + ".gif"));
            add(String.valueOf(i), aButton);
            aButton.addActionListener(this);
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(80, 120);
    }

    // Cause a 1/10 second delay when called
    private void delay() {
        try {
            Thread.sleep(100);
        }
        catch (InterruptedException e){ /* do nothing */ }
    }

    // Implements the listener behavior, e.g. go to next button in the stack
    public void actionPerformed(ActionEvent theEvent) {
        for (int i=0; i<4; i++) {
            cardLayoutManager.next(getContentPane());
            
            // Now redraw the window by updating its appearance
            update(getGraphics()); // a standard JComponent method
            
            delay();
        }
    }

    // Create main method to execute the application
    public static void main(String args[]) {
        new CardLayoutAnimation("CardLayout Animation").setVisible(true);
    }
} 