import java.applet.Applet;
import java.awt.*;

// This appears in Core Web Programming from
// Prentice Hall Publishers, and may be freely used
// or adapted. 1997 Marty Hall, hall@apl.jhu.edu.

/** An example of CardLayout. The right side of the
 *  window holds a Panel that uses CardLayout to control
 *  four possible sub-panels (each of which is a
 *  CardPanel that shows a picture of a playing card).
 *  The buttons on the left side of the window manipulate
 *  the "cards" in this layout by calling methods in
 *  the right-hand panel's layout manager.
 * @see CardPanel
 */

public class CardDemo extends Applet {
  private Button first, last, previous, next;
  private String[] cardLabels = { "Jack", "Queen",
                                  "King", "Ace" };
  private CardPanel[] cardPanels = new CardPanel[4];
  private CardLayout layout;
  private Panel cardDisplayPanel;
  
  public void init() {
    setBackground(Color.white);
    setLayout(new BorderLayout());
    addButtonPanel();
    addCardDisplayPanel();
  }

  private void addButtonPanel() {
    Panel buttonPanel = new Panel();
    buttonPanel.setLayout(new GridLayout(9, 1));
    Font buttonFont =
      new Font("Helvetica", Font.BOLD, 18);
    buttonPanel.setFont(buttonFont);
    for(int i=0; i<cardLabels.length; i++)
      buttonPanel.add(new Button(cardLabels[i]));
    first = new Button("First");
    last = new Button("Last");
    previous = new Button("Previous");
    next = new Button("Next");
    buttonPanel.add(new Label("------------",
                              Label.CENTER));
    buttonPanel.add(first);
    buttonPanel.add(last);
    buttonPanel.add(previous);
    buttonPanel.add(next);
    add("West", buttonPanel);
  }

  private void addCardDisplayPanel() {
    cardDisplayPanel = new Panel();
    layout = new CardLayout();
    cardDisplayPanel.setLayout(layout);
    String cardName;
    for(int i=0; i<cardLabels.length; i++) {
      cardName = cardLabels[i];
      cardPanels[i] =
        new CardPanel(cardName, getCodeBase(),
                      "images/" + cardName + ".gif");
      cardDisplayPanel.add(cardName, cardPanels[i]);
    }
    add("Center", cardDisplayPanel);
  }

  public boolean action(Event event, Object object) {
    if (event.target == first)
      layout.first(cardDisplayPanel);
    else if (event.target == last)
      layout.last(cardDisplayPanel);
    else if (event.target == previous)
      layout.previous(cardDisplayPanel);
    else if (event.target == next)
      layout.next(cardDisplayPanel);
    else
      layout.show(cardDisplayPanel, (String)object);
    return(true);
  } 
}

