Press '4' to rotate left, '5' to accelerate, '6' to rotate right, '2' for hyperspace
You may need to "click" on the applet to give it the "focus"
abstract classesapplet
that has a
space ship that can be controlled with the keyboard. You will need to write a SpaceShip class. Your
SpaceShip class
will extend the Floater class, an abstract class that represents all things that float in space.
An abstract class is an "incomplete" class. It has abstract methods--methods
that have no body.
When your class extends an abstract class, you must write the methods that are needed to
"complete"
the class.
Applet class we used for animation
in assignment 4
Applet class will need to
implement KeyListener. Remember, if you are implementing more than one interface, just
use commas. The KeyListener interface requires 3 methods:
- public void keyTyped(KeyEvent ke){}
- public void keyPressed(KeyEvent ke){}
- public void keyReleased(KeyEvent ke){}
Floater class has already been written for you. You will need to include it as part of your program:
abstract class Floater
{
public void accelerate (double dAmount)
{
//Accelerates the floater in the direction it is pointing
//(myPointDirection)
//convert the current direction the floater is pointing to radians
double dRadians =myPointDirection*(Math.PI/180);
//change coordinates of direction of travel
myDirectionX += ((dAmount) * Math.cos(dRadians));
myDirectionY += ((dAmount) * Math.sin(dRadians));
}
void rotate (int nDegreesOfRotation)
{
//rotates the floater by a given number of degrees
myPointDirection+=nDegreesOfRotation;
}
public void move (int nScreenWidth, int nScreenHeight)
{
//Moves the floater towards the coordinates
//myDirectionX and myDirectionY
//move the floater in the current direction of travel
myCenterX += myDirectionX;
myCenterY += myDirectionY;
//wrap around screen
if(myCenterX >nScreenWidth)
myCenterX = 0;
else if (myCenterX<0)
myCenterX = nScreenWidth;
if(myCenterY >nScreenHeight)
myCenterY = 0;
else if (myCenterY < 0)
myCenterY = nScreenHeight;
}
public void draw (Graphics g)
{
//Draws the floater at the current position
g.setColor(myColor);
//convert degrees to radians for sin and cos
double dRadians = myPointDirection*(Math.PI/180);
//Declare new array to hold positions of rotated coordinates
int[] naXCorRotated = new int[nCorners + 1];
int[] naYCorRotated = new int[nCorners + 1];
//rotate and translate the coordinates of the floater using current direction
for(int nI = 0; nI < nCorners; nI++)
{
naXCorRotated[nI] = (int)((naXCorners[nI]* Math.cos(dRadians)) - (naYCorners[nI] * Math.sin(dRadians))+myCenterX);
naYCorRotated[nI] = (int)((naXCorners[nI]* Math.sin(dRadians)) + (naYCorners[nI] * Math.cos(dRadians))+myCenterY);
}
//Set last coordinates equal to first coordinates to close figure
naXCorRotated[nCorners] = naXCorRotated[0];
naYCorRotated[nCorners] = naYCorRotated[0];
//draw unfilled polygon
g.fillPolygon(naXCorRotated,naYCorRotated,nCorners+1);
}
abstract public void setX(int nX);
abstract public int getX();
abstract public void setY(int nY);
abstract public int getY();
abstract public void setDirectionX(double dX);
abstract public double getDirectionX();
abstract public void setDirectionY(double dY);
abstract public double getDirectionY();
abstract public void setPointDirection(int nDegrees);
abstract public double getPointDirection();
protected int[] naXCorners;
protected int[] naYCorners;
protected int nCorners; //the number of corners, a triangular floater has 3
protected Color myColor;
protected double myCenterX, myCenterY; //holds center coordinates
protected double myDirectionX, myDirectionY; //holds x and y coordinates of the vector for direction of travel
protected double myPointDirection; //holds current direction the ship is pointing in degrees
}
SpaceShip class that extends the Floater class. You will need to write:
abstract methods in the Floater class
SpaceShip class in the Applet. For now, just draw
the space ship. Make sure you can see it and you are happy with its shape before you go to the next step.
Star class that creates a random number of stars in random positions
You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.
Submit the URL of your applet with the attached Java code (the .java file(s)) in an email message
to mrsimon@lycos.com.