AP Java Assignment 9
Space Ship

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"

Background: Inheritance and abstract classes

In this assignment we will start to replicate the old video game Asteroids. You will write an applet 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.

Steps to completing this assignment

  1. Start with the "fill in the blank" Applet class we used for animation in assignment 4
  2. Since we will be controlling the space ship with the keyboard, our 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: For now, you can leave them as empty stubs
  3. The 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
    }
  4. Write a SpaceShip class that extends the Floater class. You will need to write:
  5. Declare an instance of the 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.
  6. Write the code that allows you to control the space ship with the keyboard. You must include the ability to rotate left, rotate right, accelerate, and enter "hyperspace"
  7. Finally, add a 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.