Problems With Constructor!

Polygon triangle; void setup() { size(500,500); triangle = new Polygon (3,100,width/2,height/2) ; } void draw() { triangle.display() ; }

class Polygon { int n, r, x, y; float angle ; void Polygon (int tn, int tr, int tx, int ty) { angle = 360/tn; n=tn; r=tr; x=tx; y=ty; }

void display() { strokeWeight(2); for (int i=0; i<n; i++) { point((x+rcos(anglei)), (y-rsin(anglei))) ; } } }

Hello to beautiful community of processing! I've recently discovered processing and have stumbled into an error which i cant get rid off even after an hour of googling. Its because I'm not familiar with Java and have learnt a bit of C++ . Can you please take a look at the code and tell me whats going on wrong?

The Error I'm getting is : The Constructor Polygon does not exist. Thanks

Bhuvanesh

Tagged:

Answers

  • edited June 2017 Answer ✓

    https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    In Java, constructors don't have any returning datatype & are named exactly as its class' name. ~O)
    Even void isn't allowed on them! [-X

  • Answer ✓
    Polygon triangle;
    
    void setup() {
      size(500, 500);
      triangle = new Polygon (3, 100, width/2, height/2) ;
    }
    
    void draw() {
      triangle.display() ;
    }
    
    // ================================================
    
    class Polygon {
    
      int n, r, x, y;
      float angle;
    
      Polygon (int tn, int tr, int tx, int ty) { 
        angle = 360/tn;
        n=tn;
        r=tr;
        x=tx;
        y=ty;
      }
    
      void display() {
        strokeWeight(2);
        for (int i=0; i<n; i++) {
          point((x+r*cos(angle*i)), (y-r*sin(angle*i)))  ;
        }
      }
    }//class
    //
    
  • Consider going through the Objects Tutorial

    and looking at some of the objects-based examples in the Examples Sketchbook.

Sign In or Register to comment.