I am trying to write a contains method for a custom Shape class, but I would prefer, if possible, to simply write my own method without implementing the Shape class.
However, how do I go about writing such a method that would test whether or not specified X & Y coordinates are either within my shape or on the border?
[edit]
Here is an example class
abstract class BoundedShape extends Shape {
protected Point upperLeft;
protected int width, height;
public BoundedShape(Color color, Point corner, int wide, int high) {
strokeColor = color;
upperLeft = corner;
width = wide;
height = high;
}
public void setShape(Point firstPt, Point currentPt) {
if (firstPt.x <= currentPt.x)
if (firstPt.y <= currentPt.y)
upperLeft = firstPt;
else
upperLeft = new Point(firstPt.x, currentPt.y);
else if (firstPt.y <= currentPt.y)
upperLeft = new Point(currentPt.x, firstPt.y);
else
upperLeft = currentPt;
width = Math.abs(currentPt.x - firstPt.x);
height = Math.abs(currentPt.y - firstPt.y);
}
}
another
public class Line extends Shape {
protected Point firstPoint;
protected Point secondPoint;
public Line(Color color, Point p1, Point p2) {
strokeColor = color;
firstPoint = p1;
secondPoint = p2;
}
public void setEndPoint(Point endPoint) {
secondPoint = endPoint;
}
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(strokeColor);
g2d.drawLine(firstPoint.x, firstPoint.y, secondPoint.x,
secondPoint.y);
}
another
public class Rect extends BoundedShape {
public Rect(Color color, Point corner, int wide, int high) {
super(color, corner, wide, high);
}
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(strokeColor);
g2d.drawRect(upperLeft.x, upperLeft.y, width, height);
}
}