Browse Source

creation of the Rotational Element class

pull/1/head
jrtechs 8 years ago
parent
commit
516b20d2dc
3 changed files with 67 additions and 18 deletions
  1. +2
    -2
      src/tanks/DrawableElement.java
  2. +0
    -9
      src/tanks/DrawableObject.java
  3. +65
    -7
      src/tanks/RotationalElement.java

+ 2
- 2
src/tanks/DrawableElement.java View File

@ -21,13 +21,13 @@ public abstract class DrawableElement
public BufferedImage img;
//draws img onto the screen
public void Draw(Graphics g)
public void draw(Graphics g)
{
g.drawImage(img, x, y, null);
}
//loads the image from the disk into the Buffered image img
public void LoadImage()
public void loadImage()
{
img = null;
try

+ 0
- 9
src/tanks/DrawableObject.java View File

@ -1,9 +0,0 @@
package tanks;
/*
class that contains the image file location and cordinates of the object
*/
public class DrawableObject
{
}

+ 65
- 7
src/tanks/RotationalElement.java View File

@ -1,12 +1,70 @@
/*
jeffery R
5-21-16
super class for any object that moves at an angle
*/
package tanks;
/**
*
* Set 3 class project
* 5-19-16
*
*/
public class RotationalElement
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
public abstract class RotationalElement extends GameElement
{
//0-360 angle which determines which direction the object is facing
public double direction;
//convertes degrease to radians
public double degToRad()
{
return direction * (Math.PI/180);
}
//converts radians to degrees
public double radToDeg()
{
return direction * (180/ Math.PI);
}
/*
moves the object's x and y according to its degrees
the multiplier is used to move at a specific speed
a positive multiplier moves the object backwards
a negative multiplier moves the object forwards
*/
public void move(int multiplier)
{
y += Math.sin(degToRad()) * multiplier;
x += Math.cos(degToRad()) * multiplier;
}
/*
Draws the object at a specified angle rotated about the center
*/
@Override
public void draw(Graphics g)
{
//creates the object to rotate the image
AffineTransform tf = AffineTransform.getTranslateInstance(x, y);
//rotates the image around the center
tf.rotate(degToRad(), width/2, height/2);
//draws the rotated image on the panel
((Graphics2D)(g)).drawImage(img, tf, null);
}
/*
Sets the area variable shape in GameElement to be used in collision code
*/
@Override
public void setShape()
{
AffineTransform rotate = AffineTransform.getRotateInstance(degToRad(), x, y);
shape = new Area(rotate.createTransformedShape(new Rectangle(x,y,width, height)));
}
}

Loading…
Cancel
Save