Fixed collision and updated to WCS (Wesley Code Standard)
This commit is contained in:
+23
-65
@@ -15,22 +15,20 @@ import javax.swing.ImageIcon;
|
||||
|
||||
public abstract class DrawObject
|
||||
{
|
||||
protected Point2D position;
|
||||
protected double rotation;
|
||||
protected double scale;
|
||||
protected int width;
|
||||
protected int height;
|
||||
Point2D position;
|
||||
double rotation;
|
||||
double scale;
|
||||
int width;
|
||||
int height;
|
||||
private Image image;
|
||||
protected boolean selected;
|
||||
|
||||
protected int areaTop;
|
||||
protected int areaBottom;
|
||||
protected int areaLeft;
|
||||
protected int areaRight;
|
||||
|
||||
private Image image;
|
||||
private Image rotateImage;
|
||||
protected boolean selected;
|
||||
private Rectangle2D rektAngle;
|
||||
|
||||
private Rectangle2D upperLeftCorner;
|
||||
private Rectangle2D upperRightCorner;
|
||||
private Rectangle2D bottomLeftCorner;
|
||||
@@ -41,19 +39,18 @@ public abstract class DrawObject
|
||||
static final int UPPERRIGHT = 1;
|
||||
static final int BOTTOMLEFT = 2;
|
||||
static final int BOTTOMRIGHT = 3;
|
||||
|
||||
private Color rectangleColor;
|
||||
|
||||
public DrawObject(String filename, Point2D position)
|
||||
{
|
||||
image = new ImageIcon(filename).getImage();
|
||||
rotateImage = new ImageIcon("rotate.png").getImage();
|
||||
width = image.getWidth(null);
|
||||
height = image.getHeight(null);
|
||||
scale = 1;
|
||||
rotation = 0;
|
||||
this.position = position;
|
||||
rectangleColor = Color.BLACK;
|
||||
|
||||
areaTop = 0;
|
||||
areaBottom = 0;
|
||||
areaLeft = 0;
|
||||
@@ -72,7 +69,7 @@ public abstract class DrawObject
|
||||
// height/2);
|
||||
|
||||
AffineTransform rotate = AffineTransform.getRotateInstance(Math.toRadians(rotation), position.getX() + width / 2, position.getY() + height / 2);
|
||||
g.transform(rotate);
|
||||
//g.transform(rotate);
|
||||
g.setColor(rectangleColor);
|
||||
g.setStroke(new BasicStroke(7));
|
||||
rektAngle = new Rectangle2D.Double((int) position.getX() - areaLeft, (int) position.getY() - areaTop, width + areaLeft + areaRight, height + areaTop + areaBottom);
|
||||
@@ -158,60 +155,17 @@ public abstract class DrawObject
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that gets all the corner coordinates index: 0 -> top left 1 -> top
|
||||
* right 3 -> bottom left 4 -> bottom right
|
||||
*
|
||||
* @return corner coordinates
|
||||
*/
|
||||
public Point2D[] getCorners()
|
||||
{
|
||||
Rectangle2D tempRekt = new Rectangle2D.Double(0-areaLeft, 0-areaTop, width+areaLeft+areaRight, height+areaTop+areaBottom);
|
||||
tempRekt.setFrame(getTransform().createTransformedShape(tempRekt).getBounds());
|
||||
Point2D[] points = new Point2D[4];
|
||||
Point2D point = new Point2D.Double(tempRekt.getX(), tempRekt.getY());
|
||||
points[0] = point;
|
||||
point = new Point2D.Double(tempRekt.getX() + tempRekt.getHeight(), tempRekt.getY());
|
||||
points[1] = point;
|
||||
point = new Point2D.Double(tempRekt.getX(), tempRekt.getY() + tempRekt.getHeight());
|
||||
points[2] = point;
|
||||
point = new Point2D.Double(tempRekt.getX() + tempRekt.getHeight(), tempRekt.getY() + tempRekt.getHeight());
|
||||
points[3] = point;
|
||||
return points;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param corners
|
||||
* @return if there is a collision
|
||||
* Checking collision.
|
||||
* @param object. the drawObject to check collision with.
|
||||
* @return if there is a collision.
|
||||
*/
|
||||
public boolean collision(DrawObject object)
|
||||
{
|
||||
// Point2D[] ownCorners = getCorners();
|
||||
Point2D[] objectCorners = object.getCorners();
|
||||
Shape ownShape = new Rectangle2D.Double(-9 - areaLeft, -9 - areaTop, width + 13 + areaLeft+areaRight, height + 13+areaTop+areaBottom);
|
||||
ownShape = getTransformSelection().createTransformedShape(ownShape);
|
||||
// Shape otherShape = new Rectangle2D.Double(-9,
|
||||
// -9,object.getImage().getWidth(null) +
|
||||
// 13,object.getImage().getHeight(null) + 13);
|
||||
// otherShape =
|
||||
// getTransformSelection().createTransformedShape(otherShape);
|
||||
if (ownShape.contains(objectCorners[0]))
|
||||
Shape ownShape = getRectangle();
|
||||
Rectangle2D otherRectangle = object.getTransform().createTransformedShape(object.getImageRectangle()).getBounds2D();
|
||||
if(ownShape.intersects(otherRectangle))
|
||||
return true;
|
||||
else if (ownShape.contains(objectCorners[1]))
|
||||
return true;
|
||||
else if (ownShape.contains(objectCorners[2]))
|
||||
return true;
|
||||
else if (ownShape.contains(objectCorners[3]))
|
||||
return true;
|
||||
// if(otherShape.contains(ownCorners[0]))
|
||||
// return true;
|
||||
// else if(otherShape.contains(ownCorners[1]))
|
||||
// return true;
|
||||
// else if(otherShape.contains(ownCorners[2]))
|
||||
// return true;
|
||||
// else if(otherShape.contains(ownCorners[3]))
|
||||
// return true;
|
||||
else
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -243,8 +197,8 @@ public abstract class DrawObject
|
||||
case 4:
|
||||
if (point.getX() > rotateDot.getX() && point.getX() < rotateDot.getX() + rotateDot.getWidth() && point.getY() > rotateDot.getY() && point.getY() < rotateDot.getY() + rotateDot.getHeight())
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
else
|
||||
return false;
|
||||
|
||||
}
|
||||
if (point.getX() > chosenCorner.getX() && point.getX() < chosenCorner.getX() + chosenCorner.getWidth() && point.getY() > chosenCorner.getY() && point.getY() < chosenCorner.getY() + chosenCorner.getHeight())
|
||||
@@ -322,7 +276,11 @@ public abstract class DrawObject
|
||||
{
|
||||
this.rectangleColor = rectangleColor;
|
||||
}
|
||||
|
||||
|
||||
public Rectangle2D getImageRectangle() {
|
||||
return new Rectangle2D.Double(0-areaLeft,0-areaTop, width+areaLeft+areaRight, height+areaTop+areaBottom);
|
||||
}
|
||||
|
||||
public int getAreaTop()
|
||||
{
|
||||
return areaTop;
|
||||
|
||||
Reference in New Issue
Block a user