/*
 * Author: Paul Koshevoy
 * Program2.java  Wed Oct 8 12:30:00 1997
 */

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

public class Program2 extends Applet implements ActionListener, ItemListener
{
  Panel panel;
  Button ACC;  // increas thrust button
  Button DEACC;// decreas thrust button
  Button CCW;  // counter clock wise rotation button
  Button CW;   // clock wise rotation button
  Button FIRE; // fire a missile button
  Button IN;  // zoom in button
  Button OUT; // zoom out buttom
  Button RST;  // zoom in button
  Button NUKE; // zoom out buttom
  Choice SHIPS; // a choice menu to select the bitmap to use for the ship
  Choice BOMBS; // a choice menu to select the bitmap to use for the weapons
    
  MyCanvas canvas;
    
  public void init()
  {
    setBackground(Color.black);
    setForeground(Color.green);	
    setLayout(new FlowLayout());
	
    // Set up the panel to contain buttons, labels and text fields
    panel = new Panel(new GridLayout(3, 4));
        
    RST = new Button("R");     // Set up the reset button
    RST.setBackground(Color.black);
    RST.setForeground(Color.green);	
    RST.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(RST);             // add the reset button to the grid
    ACC = new Button(" up ");   // Set up the increas thrust button button
    ACC.setBackground(Color.black);
    ACC.setForeground(Color.green);	
    ACC.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(ACC);             // add the increase thrust button to the grid
    NUKE = new Button("NUKE");   // Set up the nuke button
    NUKE.setBackground(Color.black);
    NUKE.setForeground(Color.green);	
    NUKE.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(NUKE);             // add the nuke button to the grid

    SHIPS = new Choice();
    SHIPS.setBackground(Color.black);
    SHIPS.setForeground(Color.green);	
    SHIPS.addItemListener(this);
    panel.add(SHIPS);
    StringTokenizer TKNZR = new StringTokenizer(getParameter("ships"));
    while(TKNZR.hasMoreTokens())
    {
      try
      {
	SHIPS.addItem(TKNZR.nextToken());
      }
      catch(NullPointerException e)
      {
	System.out.println("error: SHIPS.addItem(...) failed with null pointer exception");
      }
    }
    try
    {
      SHIPS.select(0);
    }
    catch (IllegalArgumentException e)
    {
      System.out.println("error: no bitmaps for ship defined in HTML file");
    }

    CCW = new Button(" << ");   // Set up the counter clockwise steer button
    CCW.setBackground(Color.black);
    CCW.setForeground(Color.green);	
    CCW.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(CCW);             // add the counter clockwise steer button to the grid

    FIRE = new Button("fire");   // Set up the little red button
    FIRE.setBackground(Color.black);
    FIRE.setForeground(Color.green);	
    FIRE.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(FIRE);             // add the fire button to the grid

    CW = new Button(" >> ");   // Set up the clockwise steer button
    CW.setBackground(Color.black);
    CW.setForeground(Color.green);	
    CW.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(CW);             // add the clockwise steer button to the grid

	
    BOMBS = new Choice();
    BOMBS.setBackground(Color.black);
    BOMBS.setForeground(Color.green);	
    BOMBS.addItemListener(this);
    panel.add(BOMBS);
    TKNZR = new StringTokenizer(getParameter("weapons"));
    while(TKNZR.hasMoreTokens())
    {
      try
      {
	BOMBS.addItem(TKNZR.nextToken());
      }
      catch(NullPointerException e)
      {
	System.out.println("error: BOMBS.addItem(...) failed with null pointer exception");
      }
    }
    try
    {
      BOMBS.select(0);
    }
    catch (IllegalArgumentException e)
    {
      System.out.println("error: no bitmaps for weapons defined in HTML file");
    }

    IN = new Button(" IN ");   // Set up the zoom in button
    IN.setBackground(Color.black);
    IN.setForeground(Color.green);	
    IN.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(IN);             // add the zoom in button to the grid
    DEACC = new Button("down");   // Set up the increas thrust button button
    DEACC.setBackground(Color.black);
    DEACC.setForeground(Color.green);	
    DEACC.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(DEACC);             // add the increase thrust button to the grid
    OUT = new Button("OUT");    // Set up the zoom out button
    OUT.setBackground(Color.black);
    OUT.setForeground(Color.green);	
    OUT.addActionListener(this);// Tell the button to listen for the applet events
    panel.add(OUT);             // add the zoom out button to the grid
    panel.add(new Label(""));

    //	Dimension d = getSize();
    canvas = new MyCanvas(getImage(getDocumentBase(), 
				   getParameter("wallpaper")));

	
    canvas.set(getParameter(SHIPS.getSelectedItem()),
	       Integer.parseInt(getParameter(SHIPS.
					     getSelectedItem().
					     concat("_w"))),
	       Integer.parseInt(getParameter(SHIPS.
					     getSelectedItem().
					     concat("_h"))),
	       Integer.parseInt(getParameter(SHIPS.
					     getSelectedItem().
					     concat("_r"))),
	       Integer.parseInt(getParameter(SHIPS.
					     getSelectedItem().
					     concat("_c"))));
    // Add panel to the applet
    add(canvas);
    add(panel);
    //	add(SHIPS);
    //	add(BOMBS);
  }


  // Some code to extract an integer value from a TextField
  // This function is not mine, I've got it from the sample file
  public int getValue(TextField textField)
  {
    int f;
    try
    {
      f = (int) Double.valueOf(textField.getText()).doubleValue();
    }
    catch(java.lang.NumberFormatException e)
    {
      f = 0;
    }
    return f;
  }     


  // Event handling code
  public void actionPerformed(ActionEvent event)
  {
    if (event.getSource() == ACC)
    {
      canvas.ufo_accel();
    }
    else if (event.getSource() == DEACC)
    {
      canvas.ufo_decel();
    }
    else if (event.getSource() == CCW)
    {
      canvas.ufo_rotate_ccw();
    }
    else if (event.getSource() == CW)
    {
      canvas.ufo_rotate_cw();
    }
    else if (event.getSource() == IN)
    {
      canvas.zoom_in();
    }
    else if (event.getSource() == OUT)
    {
      canvas.zoom_out();
    }
    else if (event.getSource() == FIRE)
    {
      canvas.fire_missile(getParameter(BOMBS.getSelectedItem()),
			  Integer.parseInt(getParameter(BOMBS.
							getSelectedItem().
							concat("_w"))),
			  Integer.parseInt(getParameter(BOMBS.
							getSelectedItem().
							concat("_h"))),
			  Integer.parseInt(getParameter(BOMBS.
							getSelectedItem().
							concat("_r"))),
			  Integer.parseInt(getParameter(BOMBS.
							getSelectedItem().
							concat("_c"))));
    }
    else if (event.getSource() == NUKE)
    {
      canvas.nuke_em_all();
    }
    else if (event.getSource() == RST)
    {
      canvas.reset();
    }
  }
  /*    
	public boolean action(Event event, Object whatAction)
	{
	if (!(event.target instanceof Choice))
	return false;

	Choice whichChoice = (Choice) event.target;
	if (whichChoice == SHIPS)
	canvas.remap_ship(getParameter(SHIPS.getSelectedItem()),
	Integer.parseInt(getParameter(SHIPS.getSelectedItem().concat("_w"))),
	Integer.parseInt(getParameter(SHIPS.getSelectedItem().concat("_h"))),
	Integer.parseInt(getParameter(SHIPS.getSelectedItem().concat("_r"))),
	Integer.parseInt(getParameter(SHIPS.getSelectedItem().concat("_c"))));
	return true;
	}
  */

    
  public void itemStateChanged(ItemEvent event)
  {
    if (event.getSource() == SHIPS)
    {
      canvas.remap_ship(getParameter(SHIPS.getSelectedItem()),
			Integer.parseInt(getParameter(SHIPS.getSelectedItem().
						      concat("_w"))),
			Integer.parseInt(getParameter(SHIPS.getSelectedItem().
						      concat("_h"))),
			Integer.parseInt(getParameter(SHIPS.getSelectedItem().
						      concat("_r"))),
			Integer.parseInt(getParameter(SHIPS.getSelectedItem().
						      concat("_c"))));
    }
  }
}

