4
我正在嘗試在Java中創建一個矩形,但僅限於awt包類。從2個特定點創建一個矩形
我只能點擊兩個點,程序必須計算寬度和高度並在這兩個確切點之間繪製一個矩形。
以下不爲我工作:
package ie.iact.shapes;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
public class Rect extends Shapes {``
private Point secondPoint;
public Rect(Point f, Point s) {
setFirstPoint(f);
setSecondPoint(s);
}
@Override
public void draw(Graphics g) {
int x1 = firstPoint.x;
int y1 = firstPoint.y;
int x2 = secondPoint.x;
int y2 = secondPoint.y;
int a;
int b;
if (x1 < x2) {
a = x1;
} else {
a = x2;
}
if (y1 < y2) {
b = y1;
} else {
b = y2;
}
int width = secondPoint.x - a;
int hight = secondPoint.y - b;
g.drawRect(getFirstPoint().x, getFirstPoint().y, secondPoint.x, secondPoint.y);
}
public Point getSecondPoint() {
return secondPoint;
}
public void setSecondPoint(Point secondPoint) {
this.secondPoint = secondPoint;
}
}
看看g.drawRect調用。你去計算左上角和高度和寬度的所有麻煩,但十完全忽略這個值... – MadProgrammer
爲了更好地幫助,請發佈[SSCCE](http://sscce.org/)。 –
謝謝你,但代碼很完美。我在另一個班上壓倒了它 –