2015-05-09 62 views
-2

我正在尝试为自定义Shape类编写一个包含方法,但是如果可能的话,我宁愿简单编写自己的方法而不实现Shape类。如何编写一个包含自定义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); 
    } 
} 
+1

“Shape”类的结构是什么? –

+1

基本抽象类非常简单,只是具有抽象draw()方法。它通过自定义形状实现,通过点,高/宽或x/y传递 – motifesta

+1

您可以发布扩展'Shape'的各种形状类的代码吗?你有没有实施过这样的课程? –

回答

1

你将不得不声明在无水方法道类Shape为:

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()将被用于计算两个点之间的距离。现在,你需要在网上实现为:

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

+0

我很感激帮助。为了澄清,我试图通过mousePressed事件来选择形状,但是,在执行此操作时,我得不到检测结果......任何建议? – motifesta

+1

我想你的'mousePressed'事件将包含点击鼠标的点的坐标。您需要使用该特定点并将其传递给各个形状的'contains()'方法以确定点是否处于该形状中。 –

+0

把我的头发拉到这里 – motifesta

相关问题