2015-05-09 105 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

相關問題