/**
* @author Amanda Gorski
* @date July 7, 2002
*
* Requires Mover.java, run it there
*/
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.applet.*;

public class Ship extends JComponent implements Runnable
{
    private volatile int x;
    private volatile int y;
    private volatile boolean dir_x; //true equals right
    private volatile boolean dir_y; //true equals up
    private volatile boolean stopped;
    private int bound_x;
    private int bound_y;
    private int speed;
    Image picture;
    Image back;
    public Ship (int xx, int yy)
    { //constructor, sets up array
	x = 150;
	y = 82;
	dir_x = true;
	dir_y = true;
	bound_x = xx;
	speed = 7;
	bound_y = (yy);
	picture = Toolkit.getDefaultToolkit ().getImage ("ship.gif");
	back = Toolkit.getDefaultToolkit ().getImage ("comet.jpg");
    }


    public void run ()
    { //run method, starts when "sort" is pressed
	moveShip ();
    }


    public void stopShip ()
    {
	stopped = true;
    }


    public void moveShip ()
    {
	stopped = false;

	while (!stopped)
	{

	    drawShip ();
	    if (dir_x) //moving right
	    {
		if (x < (bound_x - 30))
		    x += speed + 2;
		else
		{
		    dir_x = false;
		    x += 4;
		}
	    }
	    else //moving left
	    {
		if (x > 5)
		    x -= speed;
		else
		{
		    dir_x = true;
		    x += speed;
		}
	    }

	    if (dir_y) //moving up
	    {
		if (y < (bound_y - 30))
		    y += speed;
		else
		    dir_y = false;
	    }
	    else //moving down
	    {
		if (y > 5)
		    y -= speed;
		else
		{
		    dir_y = true;
		    y += 2;
		}
	    }


	    //sleep for a bit after painting array
	    try
	    {
		Thread.sleep (50);
	    }
	    catch (InterruptedException x)
	    {
		;
	    }

	}
    }

    public void drawShip ()
    {
	repaint ();
    }

    /** draw the ship on the screen
    */
    public void paint (Graphics g)
    {

	g.drawImage (back, 0, 0, this);
	g.drawImage (picture, x, y, this);
    }
}

