我需要创建一个名为“TriangleShape”的类来实现java.awt.Shape。
现在另一个类“TriangleComponent”应该有一个TriangleShape类的对象,它应该绘制一个三角形,并且具有给定的边长。
我设法创建它,但我读过三角形应该按照以下方式绘制:
TriangleShape t = new TriangleShape(30,40,50);
g2.draw(t); //This is the Graphics2D object that I use in paintComponent以下是我创建的代码,但它使用Line2D创建三角形。
它是TriangleShape类,假设我已经实现了Shape类的所有方法。
public class TriangleShape implements java.awt.Shape{
private double a, b, c;
private int x,y;
private Point2D loc;
public TriangleShape() {
this.a=0;
this.b=0;
this.c=0;
}
public TriangleShape(double a, double b, double c) {
//if supplied dimensions form a valid Triangle
if ( this.isValid(a,b,c) ) {
this.a = a;
this.b = b;
this.c = c;
}