import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

/** DemoTimer - program to demonstrate using a timer.<p>
*
* Screen snaps (launch and take off)...<br>
* <center><img src="DemoTimer-1.gif">
* &nbsp;&nbsp;&nbsp;
* <img src="DemoTimer-2.gif"></center><p>
*
* @see <a href="DemoTimer.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoTimer
{
   public static void main(String[] args)
   {
      // use look and feel for my system
      try {
         UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {}

      DemoTimerFrame frame = new DemoTimerFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoTimer");
      frame.pack();
      frame.show();
   }
}

class DemoTimerFrame extends JFrame implements KeyListener, ActionListener
{
   final Image MOVING_SPACESHIP = (new ImageIcon("spaceship-1.gif")).getImage();
   final int M_WIDTH = MOVING_SPACESHIP.getWidth(this);
   final int M_HEIGHT = MOVING_SPACESHIP.getHeight(this);

   final Image IDLE_SPACESHIP = (new ImageIcon("spaceship-2.gif")).getImage();
   final int I_WIDTH = IDLE_SPACESHIP.getWidth(this);
   final int I_HEIGHT = IDLE_SPACESHIP.getHeight(this);

   final int PANEL_WIDTH = 150;
   final int PANEL_HEIGHT = 400;

   final Color GROUND_COLOR = new Color(153, 102, 51);
   final Color SKY_COLOR = new Color(204, 236, 255);
   final Color TEXT_COLOR = Color.yellow;

   JTextField countdown;
   JTextField instructions;

   Timer t;
   PaintPanel pp;
   int count;
   int delay;
   int xPos;
   int yPos;

   public DemoTimerFrame()
   {
      t = new Timer(1000, this);

      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      instructions = new JTextField();
      instructions.setEditable(false);
      instructions.setBorder(null);
      instructions.setHorizontalAlignment(JTextField.CENTER);
      instructions.setBackground(new Color(153, 102, 51));
      instructions.setForeground(Color.yellow);
      Dimension d = instructions.getPreferredSize();
      instructions.setPreferredSize(new Dimension(PANEL_WIDTH, d.height));
      instructions.setMaximumSize(new Dimension(PANEL_WIDTH, d.height));

      countdown = new JTextField();
      countdown.setEditable(false);
      countdown.setHorizontalAlignment(JTextField.CENTER);
      countdown.setFont(new Font("sansserif", Font.PLAIN, 24));
      countdown.setBorder(null);
      countdown.setBackground(GROUND_COLOR);
      countdown.setForeground(TEXT_COLOR);
      d = countdown.getPreferredSize();
      countdown.setPreferredSize(new Dimension(PANEL_WIDTH, d.height));
      countdown.setMaximumSize(new Dimension(PANEL_WIDTH, d.height));

      pp = new PaintPanel();
      pp.setBackground(SKY_COLOR);
      pp.setPreferredSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
      pp.setMaximumSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));

      initLaunchPad();

      // -------------
      // add listeners
      // -------------

      this.addKeyListener(this);

      // -----------------
      // layout components
      // -----------------

      // add components to panels

      JPanel bottomPanel = new JPanel(new BorderLayout());
      bottomPanel.add(countdown, "North");
      bottomPanel.add(instructions, "South");
      bottomPanel.setMaximumSize(bottomPanel.getPreferredSize());

      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
      contentPane.add(pp);
      contentPane.add(bottomPanel);

      // make paint panel this JFrame's content pane

      this.setContentPane(contentPane);
   }

   // -------------
   // other methods
   // -------------

   public void initLaunchPad()
   {
      t.stop();
      instructions.setText("Press SPACEBAR");
      count = 5;
      countdown.setText("");
      xPos = (PANEL_WIDTH / 2) - (I_WIDTH / 2);
      yPos = PANEL_HEIGHT - I_HEIGHT;
      pp.setSpaceship(IDLE_SPACESHIP, xPos, yPos);
   }

   // -----------------------------
   // implement KeyListener methods
   // -----------------------------

   public void keyReleased(KeyEvent ke) {}
   public void keyTyped(KeyEvent ke) {}
   public void keyPressed(KeyEvent ke)
   {
      if (ke.getKeyCode() == KeyEvent.VK_SPACE && count == 5)
      {
         instructions.setText("");
         countdown.setText("" + count);

         t.setInitialDelay(1000);
         t.setDelay(1000);
         t.start();
      }

      else if (ke.getKeyChar() == 'r')
      {
         initLaunchPad();
      }

      else if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
         System.exit(0);
   }

   // -------------------------------
   // implement ActionListener method
   // -------------------------------

   public void actionPerformed(ActionEvent ae)
   {
      --count;
      if (count > 0)
      {
         countdown.setText("" + count);
      }
      else if (count == 0)
      {
         countdown.setText("" + count);
         xPos = (PANEL_WIDTH / 2) - (M_WIDTH / 2);
         yPos = PANEL_HEIGHT - M_HEIGHT + 10;
         pp.setSpaceship(MOVING_SPACESHIP, xPos, yPos);
         t.stop();
         t.setInitialDelay(50);
         t.setDelay(50);
         t.restart();
      }
      else
      {
         yPos -= 20;
         pp.setSpaceship(MOVING_SPACESHIP, xPos, yPos);
      }

      if (yPos < -(M_HEIGHT))
      {
         t.stop();
         countdown.setText("");
         instructions.setText("Press R to reset");
      }
   }

   // -----------
   // inner class
   // -----------

   /** simple paint panel class
   */
   class PaintPanel extends JPanel
   {
      private final Image CLOUDS = (new ImageIcon("clouds-1.gif")).getImage();
      private Image img;
      private int x;
      private int y;

      PaintPanel()
      {
         img = null;
         x = 0;
         y = 0;
      }

      public void paintComponent(Graphics g)
      {
         super.paintComponent(g);
         Graphics2D g2 = (Graphics2D)g;
         g2.drawImage(CLOUDS, 10, 10, this);
         g2.drawImage(CLOUDS, 100, 60, this);
         g2.drawImage(CLOUDS, -50, 90, this);
         g2.drawImage(img, x, y, this);
      }

      public void setSpaceship(Image imgArg, int xArg, int yArg)
      {
         img = imgArg;
         x = xArg;
         y = yArg;
         repaint();
      }
   }
}

