-2

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);
  }
}
motifesta
  • 49
  • 7

1 Answers1

1

You will have to declare contains() method in abstract class Shape as:

abstract public class Shape {
    abstract boolean contains(Point point);
    double distance(Point a, Point b) {
        return Math.sqrt((a.getX()-b.getX())*(a.getX()-b.getX()) + (a.getY()-b.getY())*(a.getY()-b.getY()));
    }
}

The method distance() will be used to compute the distance between two points. Now you need to implement contains() in Line as:

public class Line extends Shape {
    private Point firstPoint;
    private Point secondPoint;

    public Line(Point firstPoint, Point secondPoint) {
        this.firstPoint = firstPoint;
        this.secondPoint = secondPoint;
    }

    @Override
    boolean contains(Point point) {
       return (distance(firstPoint, point) + distance(secondPoint, point) == distance(firstPoint, secondPoint));
    }

}

And this can be tested as:

public static void main(String[] args) {
        Shape shape = new Line(new Point(10,10),new Point(10,40));
        boolean result = shape.contains(new Point(10,20));
        System.out.println(result);
    }

The output of the above code is true. Now you can write similar methods for other classes e.g. Rectangle you can check this.

Community
  • 1
  • 1
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • I appreciate the help. To clarify, I am trying to select the shapes by mousePressed events, however, when implementing this, I am getting no hit detection... any suggestions? – motifesta May 09 '15 at 04:07
  • 1
    I suppose your `mousePressed` event will contain the coordinates of point where mouse clicked. You need to use that particular point and pass it to `contains()` method of respective shape to determine whether point is in that shape or not. – akhil_mittal May 09 '15 at 04:09
  • Pulling my hair out over here – motifesta May 09 '15 at 04:55
  • 1
    Ha ha ha. Dont worry I will help you :) – akhil_mittal May 09 '15 at 04:57
  • So I implemented the above passing coordinates from a mouseevent getPoint(), but my hit detection came up empty. I believe it has to do with lines having no width... Additionally, if I am looking for points located within a rectangle, for example, how would such code differ? – motifesta May 09 '15 at 05:08
  • Are you able to get correct Point values in `mouseevent getPoint()` if yes then the logic should have passed. I tested it with some dummy data and i worked. I believe you are stuck somewhere else. – akhil_mittal May 09 '15 at 05:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/77371/discussion-between-motifesta-and-akhil-mittal). – motifesta May 09 '15 at 05:15
  • isn't Shape an interface? – Raul H Jan 10 '17 at 22:25