Loading...
Logo
Processing Forum
I want to declare the vine class without parameters but I am getting errors and wondering if maybe this is not possible??
My error is actually "unexpected token: {" in the part below in red but I don't see a problem with how I declared it.

New to OOP. Thanks for the help.
Copy code
  1. Vine vine;

    void setup() {
      size(400, 400);
      background(206, 198, 198);
      vine = new Vine();
      noLoop();
    }

    void draw() {
      vine.grow(width/2, height/2, 400, 300);
      if (mousePressed == true) {
        vine.leaf(mouseX, mouseY, 1);
      }
    }
  2. class Vine {
      float startX, startY, endX, endY, direction;
      int lfSize;
      {
        Vine() {
          startX = sX;
          startY = sY;
          endX = eX;
          endY = eY;
          direction = dir;
          lfSize = lfSize;
        }
        void grow(sX, sY, eX, eY) {
          line(sX, sY, eX, eY);
        }
        void leaf(sX, sY, lfSize) {
          float angle = radians(40);
          pushMatrix();
          translate(sX, sY);    //Global position x, y becomes local position 0,0
          rotate(angle);
          scale(lfSize);
          beginShape();
          vertex(0, 0);
          bezierVertex(50, -60, 100, -30, 150, -50);
          bezierVertex(100, 30, 50, 30, 0, 0);
          endShape();
        }
      }
    }

Replies(4)

There are several errors

(1) In the Vine class the { after int lfSize; is not required
(2) The methods in the Vine class have no data types for their parameters e.g.
      void grow(sX, sY, eX, eY) {
should be
      void grow(float sX, float sY, float eX, float eY) {
(3) In the constructor you are using sX, sY etc. which are obviously global variables. Although it will work as you become more familiar with object orientation you will come to realise this is not good practice. I suggest you change the constructor to
Copy code
  1.   Vine(float sX, float sY, float eX, float eY, float dir, float leafSize) {
  2.     startX = sX;
  3.     startY = sY;
  4.     endX = eX;
  5.     endY = eY;
  6.     direction = dir;
  7.     lfSize = leafSize;
  8.   }
it makes it easier to create multiple objects form this class with different starting values passed as parameters.



When I declared class Vine I already declared all my variables as int or floats (line) and then in the constructor I set those equal to how I will be refering to them in the my methods (ie. startX = sX) but I still have to to declare their types again in the constructor?

And can I declare their types in the parentheses like you did (in line 1 or your code above) OR do it by leaving the parentheses empty and putting the types below Vine() { like in CODE 2 below?

CODE 1:
Copy code
  1. class Vine {
  2.   float startX, startY, endX, endY, direction;
  3.   int lfSize;
  4.     Vine() {
          startX = sX;
          startY = sY;
          endX = eX;
          endY = eY;
          direction = dir;
          lfSize = lfSize;
        }
  5.     void grow(float sX, float sY, float eX, float eY) {
          line(sX, sY, eX, eY);
        }
CODE 2
Copy code
  1. class Vine {
  2.   float startX, startY, endX, endY, direction;
  3.   int lfSize;
  4.     Vine() {
          float startX = sX;
          float startY = sY;
          float endX = eX;
          float endY = eY;
          float direction = dir;
          int lfSize = lfSize;
        }
  5.     void grow(float sX, float sY, float eX, float eY) {
          line(sX, sY, eX, eY);
        }
If I put the variables in the constructor (like you did), when I create the vine class in my code (line 3 below) can I still do it with empty parentheses or do I have to fill it in with values.

Copy code
  1. Vine vine;
  2. void setup() {
      size(400, 400);
      background(206, 198, 198);
  3.   vine = new Vine();
  4.   noLoop();
    }
Thanks this will really clear stuff up.
McK



Before we start your Code 2 is wrong as you are creating local variables called startX etc rather than using the class variables declared in line 2.


A class can have more than one constructor so the following is permitted.

Copy code
  1. class Vine {
  2.   float startX, startY, endX, endY, direction;
  3.   int leafSize;
  4.  
  5.   Vine() {
  6.       startX = sX;
  7.       startY = sY;
  8.       endX = eX;
  9.       endY = eY;
  10.       direction = dir;
  11.       leafSize = lfSize;
  12.       }

  13.   Vine(float sX, float sY, float eX, float eY, float dir, float lSize) {
  14.             startX = sX;
  15.             startY = sY;
  16.             endX = eX;
  17.             endY = eY;
  18.             direction = dir;
  19.             leafSize = lSize;
  20.   }

  21.       // class methods here

  22. } // end of class Vine
NOTE the constructor starting at line 5 is called the default constructor because it has no parameters. The constructor starting at line 14 is called a parameter constructor

NOTE that I have changed the leaf size attribute so it does not have the same name as a global variable. In your code you have the following assignment statement
lfSize = lfSize;
this just copies the contents of the variable into itself - so nothing actually happens. You had this statement because you had a class variable called lfSize and a global variable with the same name.


To create an object with the first constructor use
Copy code
  1. vine = new Vine();
this will create a new Vine object using the values stored in the glocbal variables sX, sY etc.

To create an object with the second constructor use
Copy code
  1. vine = new Vine(100, 110, 120, 130, radians(45), 1.5);
This will store 100 in startX, 110 in startY etc.
If you want many Vine objects then the advantage of the second constructor should be obvious because you simply call the constructor with different values. To use the first constructor would involve changing the values of the global variables between each call to the default constructor.

One of the best ways to learn programming is to do what you are doing i.e. writing programs and overcoming programming bugs and logic errors so keep up the good work.

I strongly recommend that you read some basic texts on Java and object orientation (OO). In particular I would investigate functions and parameters, then OO basics.


Got it! I have done a few more examples. Working vine class code below.

Copy code
  1. Vine vine;
  2. void setup() {
  3.   size(400, 400);
  4.   background(206, 198, 198);
  5.   vine = new Vine(0.0, 0.0, 0.0, 0.0, 1);      //Initial parameters just sent to the constructor (not necessarily used in the methodsMN)
  6. }
  7. void draw() {
  8.   vine.grow(width/2, height/2, 400, 300);
  9.   if(mousePressed == true) {
  10.     vine.leaf(mouseX, mouseY, 1);
  11.   }
  12. }
  13. class Vine {
  14.   float startX, startY, endX, endY, direction;
  15.   int leafSize;
  16.  
  17.     Vine(float sX, float sY, float eX, float eY, int lfSize) {
  18.       startX = sX;
  19.       startY = sY;
  20.       endX = eX;
  21.       endY = eY;
  22.       leafSize = lfSize;
  23.     }
  24.    
  25.     void grow(float sX, float sY, float eX, float eY) {
  26.       line(sX, sY, eX, eY);
  27.     }
  28.     void leaf(float sX, float sY, int lfSize) {
  29.       float angle = radians(90);
  30.       pushMatrix();
  31.       translate(sX, sY);    //Global position x, y becomes local position 0,0
  32.       rotate(angle);
  33.       scale(lfSize);
  34.       beginShape();
  35.       vertex(0, 0);
  36.       bezierVertex(50, -60, 100, -30, 150, -50);
  37.       bezierVertex(100, 30, 50, 30, 0, 0);
  38.       endShape();
  39.       popMatrix();
  40.     }
  41.   }