//  KandyJump.java
//  KandyJump
//
// $Id: KandyJump.java,v 1.4 2007/11/26 02:54:19 dtj Exp $
//
//  Created by Derek Jones on 11/13/07.
//  Copyright (c) 2007. All rights reserved.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.lang.Boolean;
import java.util.Vector;

public class KandyJump extends JApplet implements WindowListener {
	
	public MainGame mainGame;
	public JFrame window;
	JButton playButton;
	public Vector labels = new Vector();
	
    public void init() {
        // set the default look and feel
        String laf = UIManager.getSystemLookAndFeelClassName();
        try {
            UIManager.setLookAndFeel(laf);
        } catch (UnsupportedLookAndFeelException exc) {
            System.err.println ("Warning: UnsupportedLookAndFeel: " + laf);
        } catch (Exception exc) {
            System.err.println ("Error loading " + laf + ": " + exc);
        }
		
		mainGame = new MainGame();
		mainGame.addKeyListener(mainGame);

		// Does the user want an applet or their own window?
		String windowed = this.getParameter("windowed");
		if (windowed == null  ||
			(new Boolean(windowed)).booleanValue() == false) {
			// in an applet
			getContentPane().add(mainGame);
			mainGame.start();
		} else {
			// in its own window
			window = new JFrame();
			window.getContentPane().add(mainGame);
			window.addWindowListener(this);
			
			labels.add(new JLabel("Instructions:"));
			labels.add(new JLabel("Move left and right with arrow keys."));
			labels.add(new JLabel("Space bar to jump."));
			labels.add(new JLabel("N for New Game."));
			labels.add(new JLabel("Get gumdrops, avoid apples."));
			labels.add(new JLabel("Points doubled when in the air."));
			
			getContentPane().setLayout(new FlowLayout());
			for (Object o : labels) {
				getContentPane().add((JLabel)o);
			}
			playButton = new JButton("Click to play KandyJump!");
			playButton.addActionListener(new AbstractAction() {
				public void actionPerformed (ActionEvent e) {
					window.setVisible(true);
					window.toFront();
					mainGame.start();
				}
			});
			getContentPane().add(playButton);
			window.setSize(400, 450);
			window.setResizable(false);
		}
		mainGame.setFocusable(true);
    }
	
    public void windowClosed(WindowEvent e) {
        System.out.println("Window just closed; should stop game.");
        System.out.println("This never seems to get called.");
    }
	
    public void windowOpened(WindowEvent e) {
        System.out.println("opened: Could start or resume game.");
    }
    
    public void windowIconified(WindowEvent e) {
        System.out.println("iconified: Could pause game.");
    }
    
    public void windowDeiconified(WindowEvent e) {
        System.out.println("deiconified: Could resume from pause.");
    }
    
    public void windowActivated(WindowEvent e) {
        System.out.println("Window was 'activated'");
    }
    
    public void windowDeactivated(WindowEvent e) {
        System.out.println("Window was 'deactivated'");
    }

	public void windowClosing(WindowEvent e) {
		System.out.println("Window is closing");
	}
}
