Advertisement
StormWingDelta

Java Classes for reuse or ideas

Jan 7th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. //the main vector controlling class that can be very useful
  2. import java.awt.Shape;
  3.  
  4. /**
  5.  * Write a description of class BaseVectorShape here.
  6.  *
  7.  * @author (your name)
  8.  * @version (a version number or a date)
  9.  */
  10. //base vector shape class for polygonal shapes
  11. public class BaseVectorShape
  12. {
  13.     //variables
  14.     private Shape shape;
  15.     private boolean alive;
  16.     private double x,y;
  17.     private double velX,velY;
  18.     private double moveAngle, faceAngle;
  19.  
  20.     //accessor methods
  21.     public Shape getShape() { return shape; }
  22.     public boolean isAlive() { return alive; }
  23.     public double getX() { return x; }
  24.     public double getY() { return y; }
  25.     public double getVelX() { return velX; }
  26.     public double getVelY() { return velY; }
  27.     public double getMoveAngle() { return moveAngle; }
  28.     public double getFaceAngle() { return faceAngle; }
  29.    
  30.     //mutator and helper methods
  31.     public void setShape(Shape shape) { this.shape = shape; }
  32.     public void setAlive(boolean alive) { this.alive = alive; }
  33.     public void setX(double x) { this.x = x; }
  34.     public void incX(double i) { this.x += i; }
  35.     public void setY(double y) { this.y = y; }
  36.     public void incY(double i) { this.y += i; }
  37.     public void setVelX(double velX) { this.velX = velX; }
  38.     public void incVelX(double i) { this.velX += i; }
  39.     public void decVelX(double i) { this.velX -= i; }
  40.     public void setVelY(double velY) { this.velY = velY; }
  41.     public void incVelY(double i) { this.velY += i; }
  42.     public void decVelY(double i) { this.velY -= i; }
  43.     public void setFaceAngle(double angle) { this.faceAngle = angle;}
  44.     public void incFaceAngle(double i) { this.faceAngle += i; }
  45.     public void setMoveAngle(double angle) { this.moveAngle = angle; }
  46.     public void incMoveAngle(double i) { this.moveAngle += i; }
  47.  
  48.     //default constructor
  49.     BaseVectorShape()
  50.     {
  51.         setShape(null);
  52.         setAlive(false);
  53.         setX(0.0);
  54.         setY(0.0);
  55.         setVelX(0.0);
  56.         setVelY(0.0);
  57.         setMoveAngle(0.0);
  58.         setFaceAngle(0.0);
  59.        
  60.     }
  61.  
  62. }
  63.  
  64.  
  65. //Remember the these are separate classes and not to have the code in the same class
  66. //Player's ship class I'll let you figure out the other two sprite classes
  67. import java.awt.Polygon;
  68. import java.awt.Rectangle;
  69.  
  70. /**
  71.  * Write a description of class ShipClass here.
  72.  *
  73.  * @author (your name)
  74.  * @version (a version number or a date)
  75.  */
  76. //Ship class - polygonal shape of the player's ship
  77. public class ShipClass extends BaseVectorShape
  78. {
  79.     //define the ship polygon
  80.     private int[] shipx = { -6, -3 , 0, 3, 6, 0 };
  81.     private int[] shipy = { 6, 7, 7, 7, 6, -7 };
  82.    
  83.     //bounding rectangle
  84.     public Rectangle getBounds()
  85.     {
  86.         Rectangle r;
  87.         r = new Rectangle( (int)getX() - 6, (int)getY() - 6, 12, 12);
  88.         return r;
  89.     }
  90.    
  91.     ShipClass()
  92.     {
  93.         setShape(new Polygon(shipx, shipy, shipx.length));
  94.         setAlive(true);
  95.     }
  96. }
  97.  
  98.  
  99. //Java Applet this thing runs on
  100. //Will be posted in another file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement