/*
 * Author: Paul Koshevoy
 * Program3.java  Wed Oct 29 14:10:00 1997
 */

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

public class Program3 extends Applet implements ActionListener
{
    Panel panel;
    Button UP;   // increas thrust button
    Button DOWN; // decreas thrust button
    Button LEFT;
    Button RIGHT;
    Button RESET;
    Label fuel;
    Label speed;
    Label Vx;
    Label Vy;
    
    Space   canvas;
    Ship    spaceship;
    Terrain terrain;
    Speedometer speedometer;
    FuelGauge fuelgauge;
    
public void init()
    {
	setForeground(Color.green);
	setBackground(Color.black);
        setLayout(new FlowLayout());
	
        // Set up the panel to contain buttons, labels and text fields
        panel = new Panel(new GridLayout(3, 3));
        
        panel.add(new Label(""));  // fill in empty space
        UP = new Button(" up ");   // Set up the increas thrust button
	UP.setForeground(Color.green);
	UP.setBackground(Color.black);
        UP.addActionListener(this);// Tell the button to listen for the applet events
        panel.add(UP);             // add the increase thrust button to the grid
        panel.add(new Label(""));  // fill in empty space

        LEFT = new Button("left");    // Set up the left thrust button
	LEFT.setForeground(Color.green);
	LEFT.setBackground(Color.black);
        LEFT.addActionListener(this); // Tell the button to listen for the applet events
        panel.add(LEFT);              // add the button to the grid
        RESET = new Button("reset");  // Set up the reset button
	RESET.setForeground(Color.green);
	RESET.setBackground(Color.black);
        RESET.addActionListener(this);// Tell the button to listen for the applet events
        panel.add(RESET);             // add the button to the grid
        RIGHT = new Button("right");  // Set up the right thrust button
	RIGHT.setForeground(Color.green);
	RIGHT.setBackground(Color.black);
        RIGHT.addActionListener(this);// Tell the button to listen for the applet events
        panel.add(RIGHT);             // add the button to the grid

        panel.add(new Label(""));    // fill in empty space
        DOWN = new Button("down");   // Set up the decreas thrust button
	DOWN.setForeground(Color.green);
	DOWN.setBackground(Color.black);
        DOWN.addActionListener(this);// Tell the button to listen for the applet events
        panel.add(DOWN);             // add the button to the grid
        panel.add(new Label(""));    // fill in empty space

        canvas = new Space(this, getImage(getDocumentBase(), getParameter("wallpaper")), 7.0E+22, 1.0E+6);
	while(canvas.state==1)
	    {}
	spaceship = new Ship("default", canvas);
	terrain = new Terrain(canvas, 8, 2, (int)(canvas.height/4), (int)(canvas.height/4),
			      10, canvas.height-50);
	speedometer = new Speedometer(spaceship, canvas);
	fuelgauge = new FuelGauge(spaceship, canvas);
	canvas.set_ship_terrain(spaceship, terrain, speedometer, fuelgauge);
	canvas.start();

	Panel subpanel_1 = new Panel(new FlowLayout());
	speed = new Label("   0");
	speed.setForeground(Color.green);
	speed.setBackground(Color.black);
	Label temp_l = new Label("speed : ");
	temp_l.setForeground(Color.green);
	temp_l.setBackground(Color.black);
	subpanel_1.add(temp_l);
	subpanel_1.add(speed);
	Vx = new Label("   0");
	Vx.setForeground(Color.green);
	Vx.setBackground(Color.black);
	temp_l = new Label(" V(x) : ");
	temp_l.setForeground(Color.green);
	temp_l.setBackground(Color.black);
	subpanel_1.add(temp_l);
	subpanel_1.add(Vx);
	Vy = new Label("   0");
	Vy.setForeground(Color.green);
	Vy.setBackground(Color.black);
	temp_l = new Label(" V(y) : ");
	temp_l.setForeground(Color.green);
	temp_l.setBackground(Color.black);
	subpanel_1.add(temp_l);
	subpanel_1.add(Vy);
	temp_l = new Label(" maximum safe landing speed: ");
	temp_l.setForeground(Color.green);
	temp_l.setBackground(Color.black);
	subpanel_1.add(temp_l);
	temp_l = new Label(""+speedometer.safe);
	temp_l.setForeground(Color.green);
	temp_l.setBackground(Color.black);
	subpanel_1.add(temp_l);
	

	Panel subpanel_2 = new Panel(new GridLayout(1, 3));
	fuel = new Label(""+(int)(fuelgauge.fuel/fuelgauge.width*100));
	fuel.setForeground(Color.green);
	fuel.setBackground(Color.black);
	temp_l = new Label("fuel : ");
	temp_l.setForeground(Color.green);
	temp_l.setBackground(Color.black);
	subpanel_2.add(temp_l);
	subpanel_2.add(fuel);
	temp_l = new Label("%");
	temp_l.setForeground(Color.green);
	temp_l.setBackground(Color.black);
	subpanel_2.add(temp_l);

// Add panel to the applet
	add(canvas);
	add(subpanel_1);
	add(speedometer);
	add(subpanel_2);
	add(fuelgauge);
        add(panel);
    }

public void update_gauges()
    {
	speed.setText(""+speedometer.speed);
	Vx.setText(""+(int)(spaceship.Velocity.X*speedometer.scale));
	Vy.setText(""+(int)(spaceship.Velocity.Y*(-speedometer.scale)));
	fuel.setText(""+(int)(fuelgauge.fuel/fuelgauge.width*100));
    }
    
// 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() == UP)
	    {
		if(fuelgauge.fuel>0)
		    {
			fuelgauge.decrement();
			spaceship.up();
		    }
	    }
	else if(event.getSource() == DOWN)
	    {
		if(fuelgauge.fuel>0)
		    {
			fuelgauge.decrement();
			spaceship.down();
		    }
	    }
	else if(event.getSource() == LEFT)
	    {
		if(fuelgauge.fuel>0)
		    {
			fuelgauge.decrement();
			spaceship.left();
		    }
	    }
	else if(event.getSource() == RIGHT)
	    {
		if(fuelgauge.fuel>0)
		    {
			fuelgauge.decrement();
			spaceship.right();
		    }
	    }
	else if(event.getSource() == RESET)
	    {
		spaceship.reset();
		terrain.reset(8, 2, (int)(canvas.height/4), (int)(canvas.height/4));
		canvas.reset();
		fuelgauge.reset();
	    }
    }
}

