Java - 制作Triangle类

时间:2012-02-27 07:37:05

标签: java class geometry paintcomponent

我需要创建一个名为“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;        
    }
    //Otherwise make it zero sized triangle
    else{
        this.a=0;
        this.b=0;
        this.c=0;
    }            
}

public void resize(double a, double b, double c) {
    if ( this.isValid(a,b,c) ) {
        this.a = a;
        this.b = b;
        this.c = c;        
    }
    //else let size remain unchanged
}

public TriangleShape getRandomTriangle() {
    TriangleShape t = new TriangleShape(5,8,9);
    return t;
}

public double area(){
    double area, s;
    s = (a+b+c)/2;
    area = Math.sqrt(s *(s-a) * (s-b) * (s-c));
    return area;
}

private boolean isValid(double a, double b, double c) {
    double s = (a+b+c)/2;
    if ( ((s-a) * (s-b) * (s-c)) <= 0 )
        return false;
    else
        return true;
}

public double perimeter() {
    double p;
    p = a+b+c;
    return p;
}

public double getA(){
    return a;
}
public double getB(){
    return b;
}
public double getC(){
    return c;
}

public void setLocation(Point2D location){
loc = location;
}

public Point2D getLocation(){
return loc;
}

public double getX(){
return loc.getX();
}

public double getY(){
return loc.getY();
}

TriangleComponent类:

public class TriangleComponent extends JComponent{

TriangleShape t;
double alpha, beta, gamma;
double a,b,c;
double X,Y;

@Override
protected void paintComponent(Graphics g) {
//super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
t = new TriangleShape(100,100,190);
t.setLocation(new Point2D.Double(100,500));
a = t.getA();
b = t.getB();
c = t.getC();

X = t.getX();
Y = t.getY();

///////////////Drawing Base line.....

g2.draw(new Line2D.Double(X,Y,(X+c),Y));    //line c...
g2.draw(new Line2D.Double((X+c), Y, ((X+c)+a*Math.cos(Math.PI+getBeta())), (Y+a*Math.sin(Math.PI+getBeta())))); //line a...


//JOIning the last end points
g2.draw(new Line2D.Double(X, Y, ((X+c)+a*Math.cos(Math.PI+getBeta())), (Y+a*Math.sin(Math.PI+getBeta()))));


System.out.println("X1 = "+X+"  Y1 = "+Y);
System.out.println("X2 = "+(X+c)+"  Y2 = "+Y);  
System.out.println("X3 = "+((X+c)+a*Math.cos(Math.PI+getBeta()))+" Y3 = "+ (Y+a*Math.sin(Math.PI+getBeta())));
//System.out.println("Alpha = "+getAlpha());
System.out.println("Gamma = "+(getGamma()*180)/Math.PI);
}

public double getAlpha(){
double temp = Math.acos(((Math.pow(c, 2)+Math.pow(b, 2))-Math.pow(a, 2))/(2*b*c));
System.out.println("Alpha = "+temp+" Degrees");
return temp;
}

public double getBeta(){
double temp = Math.acos(((Math.pow(c, 2)+Math.pow(a, 2))-Math.pow(b, 2))/(2*a*c));
System.out.println("Beta = "+temp+" Degrees");
return (temp);// * Math.PI)/180;
}

public double getGamma(){
double temp = Math.acos(((Math.pow(a, 2)+Math.pow(b, 2))-Math.pow(c, 2))/(2*b*a));
System.out.println("Gamma = "+temp+" Degrees");
return (temp);// * Math.PI)/180;
}

}

这很有效,但我需要一种方法来绘制三角形而不依赖Graphics2D或直接用paintComponent方法绘制它。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

根据JavaDoc of the Graphics2D类,根据以下原则呈现形状:

  

形状操作

     
      
  1. 如果操作是绘制(Shape)操作,则Graphics2D上下文中当前Stroke属性的createStrokedShape方法用于构造包含指定Shape的轮廓的新Shape对象。

  2.   
  3. 使用Graphics2D上下文中的当前Transform将Shape从用户空间转换为设备空间。

  4.   
  5. 使用Shape的getPathIterator方法提取Shape的轮廓,该方法返回沿Shape的边界迭代的PathIterator对象。

  6.   
  7. 如果Graphics2D对象无法处理PathIterator对象返回的曲线段,则它可以调用Shape的备用getPathIterator方法,该方法会使Shape变平。

  8.   
  9. 查询Graphics2D上下文中当前的Paint是否有PaintContext,它指定要在设备空间中呈现的颜色。

  10.   

简而言之,这意味着Graphics2D.draw(Shape)方法将调用您的TraingleShape.getPathIterator(AffineTransform)方法并使用返回的PathIterator对象来查找在哪些点之间绘制线条。

因此,您可能需要实现与TriangleShape实现相对应的自己的PathIterator实现。

然而,上述解决方案可能比它需要的更复杂。另一种方法是查看Path2D类,它允许您使用简单的操作(如lineTo(x,y))轻松指定任意形状。由于此类实现了Shape接口,因此您可以允许TriangleShape类扩展此类,或者只是委托它。下面是使用GeneralPath类的示例,它的工作方式与Path2D类似: http://www.roseindia.net/java/example/java/swing/graphics2D/general-path.shtml

但是,这取决于您的特定分配,这是否是可接受的解决方案。