title: Java - making Triangle class
body: <p>I need to make a Class named "TriangleShape" which impliments java.awt.Shape.
  Now another Class "TriangleComponent" should have an object of TriangleShape class and it should draw a triangle, with the given length of sides.</p>
  
  <p>I managed to create it, but i've read that the triangle should be drawn in the following way:</p>
  
  <pre><code>TriangleShape t = new TriangleShape(30,40,50);
  g2.draw(t);         //This is the Graphics2D object that I use in paintComponent
  </code></pre>
  
  <p>The following is the code that I created, but it uses Line2D to create a triangle.
  It is the TriangleShape class, assume that I have implimented all the methods of the Shape Class.</p>
  
  <pre><code>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)) &lt;= 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();
  }
  </code></pre>
  
  <p>And the TriangleComponent class:</p>
  
  <pre><code>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;
  }
  
  }
  </code></pre>
  
  <p>This works, but I need a way to draw the triangle without relying on <code>Graphics2D</code> or drawing it directly with the <code>paintComponent</code> method. Is there a way to do this?</p>