import java.awt.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

        // Display each player's `name' and card value in a vertical box.
        // An area at the bottom of the frame is available for displaying
        // a message when a pile is won, or the game is won.
class CardDisplay extends JPanel {
    public CardDisplay(int numberOfPlayers){
        setLayout(new BorderLayout());
        setBackground(Color.white);

        Box playerPanel = new Box(BoxLayout.X_AXIS);
        playerPanel.setBackground(Color.white);

        // Each player has a label for representing their card.
        cards = new JLabel[numberOfPlayers];

        for(int i = 0; i < numberOfPlayers; i++){
            JLabel label = new JLabel("Player "+i,JLabel.CENTER);
            label.setFont(labelFont);
            label.setBorder(new EtchedBorder(EtchedBorder.LOWERED));

            // Two characters for the card's value.
            JLabel card = new JLabel("XX",JLabel.CENTER);
            card.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
            card.setFont(cardFont);
            cards[i] = card;

            // Combine the header and card in a panel.
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            panel.setBackground(Color.white);
            panel.add(label,"North");
            panel.add(cards[i],"Center");

            playerPanel.add(panel);
        }
        clear();
        add(playerPanel,"Center");
        add(getReportArea(),"South");
        getFrame().pack();
    }


    public void showCard(Card c,int player){
        int value = c.getValue();
        JLabel card = getCards()[player];
        card.setText("");
        delay();
        // Always use two characters for the card.
        String valueString = value < 10?" "+value:""+value;
        card.setText(valueString);
        delay();
    }

        // Clear both the card and reporting areas.
    public void clear(){
        JLabel[] cards = getCards();
        for(int i = 0; i < cards.length; i++){
            cards[i].setText("  ");
        }
        getReportArea().setText("");
        delay();
    }

        // Show the given string in the reporting area.
    public void report(String s){
        getReportArea().setText(s);
        delay();
    }

    private void delay(){
        try{
            final long sleepTime = 1000;
            Thread.sleep(sleepTime);
        }
        catch(Exception e){
        }
    }

    private JLabel[] getCards(){
        return cards;
    }

    private JTextField getReportArea(){
        return reportArea;
    }

    private ComponentFrame getFrame(){
        return frame;
    }

    // Font information for the different display areas.
    private static final int cardFontSize = 32;
    private static Font cardFont = new Font("Courier",Font.BOLD,cardFontSize);
    private static final int reportFontSize = 12;
    private static Font reportFont = new Font("SansSerif",Font.BOLD,reportFontSize);
    private static final int labelFontSize = 20;
    private static final Font labelFont = new Font("Courier",
                                                   Font.BOLD,labelFontSize);
    // Initialised in the constructor.
    private final JLabel[] cards;

    private final JTextField reportArea = new JTextField("");
    {
        reportArea.setFont(reportFont);
    }

    private final ComponentFrame frame = new ComponentFrame("A Card Game");
    {
        frame.addComponent(this);
    }
}
