【问题标题】:How to write a contains method for custom Shape class如何为自定义 Shape 类编写包含方法
【发布时间】:2015-07-20 01:15:17
【问题描述】:

我正在尝试为自定义 Shape 类编写 contains 方法,但如果可能的话,我更愿意在不实现 Shape 类的情况下简单地编写自己的方法。

但是,我该如何编写这样一个方法来测试指定的 X 和 Y 坐标是在我的形状内还是在边框上?

[编辑]

这是一个示例类

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);
  }
}

另一个

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);
  }

另一个

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);
  }
}

【问题讨论】:

  • Shape 类的结构是什么?
  • 基础抽象类非常简单,仅具有抽象的 draw() 方法。它由自定义形状实现,通过点、高度/宽度或 x/y
  • 你能贴出扩展Shape的各种形状类的代码吗?你实现过这样的类吗?
  • 您可以在 Shape 中声明 contains 方法,该方法需要被所有类覆盖。
  • 对,但我没有实现 java Shape 类...我正在努力确定如何编写自己的 contains 方法

标签: java equals contains shape


【解决方案1】:

您必须在抽象类Shape 中声明contains() 方法为:

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()));
    }
}

distance() 方法将用于计算两点之间的距离。现在你需要在 Line 中实现contains()

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));
    }

}

这可以测试为:

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);
    }

上述代码的输出是true。现在您可以为其他类编写类似的方法,例如长方形可以查看this

【讨论】:

  • 感谢您的帮助。为了澄清,我试图通过 mousePressed 事件来选择形状,但是,在实现这一点时,我没有得到任何命中检测......有什么建议吗?
  • 我想您的mousePressed 事件将包含鼠标单击点的坐标。您需要使用该特定点并将其传递给相应形状的contains() 方法以确定点是否在该形状中。
  • 把我的头发拉过来
  • 哈哈哈。别担心我会帮你的:)
  • 所以我从 mouseevent getPoint() 实现了上述传递坐标,但我的命中检测结果为空。我相信它与没有宽度的线条有关......此外,如果我正在寻找位于矩形内的点,例如,这样的代码会有什么不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
相关资源
最近更新 更多