Issue calling class constructor

edited October 2013 in Programming Questions

I am having an issue calling a constructor of a class I have defined in another tab.

The type def of the constructor is: Ring(int, int, color). The error is saying it can't find a Ring(int, int, int). I realize that ultimately a color is just an int; however, I can't help but thing this is screwing processing here. Snapshot

Having issues loading an image so I posted it here: snapshot

Tagged:

Answers

  • Since Processing's pre-processor changes color to int before compilation then changing the constructor definition to Ring(int, int, int) it won't make ANY difference what so ever.

    Since there is also a method color() I suggest that you use int instead of color for data types as it can save problems like this occurring.

  • edited October 2013

    I changed the color defs to ints. Still same error.

    Why does it say ELwave.Ring() if I am defining the class Ring on another tab/file?

  • Nevermind on the ELwave.Ring() question. I see that Processing automagically takes tabs and makes classes innerClasses.

    Still not sure why it can't find the constructor though.

  • Still not sure why it can't find the constructor though.

    Can you post the code that attempts to call the constructor and the constructors for this class.

  • edited October 2013
    import java.util.*;
    
    // GLOBALS
    float t1 = 1.0;               // base decay rate
    float t2 = 1.0;               // propogation decay constant
    float InitialIntensity = 1.0;
    float velocity = 100.0;       // pixels / second
    int elc = color(0, 255, 170); // el color
    int bgc = color(0,0,32);    // background color
    ArrayList<Ring> rings;
    int mortality = 160;
    
    // SETUP
    void setup(){
      size(800, 600);
      background(bgc);
      frameRate(30);
      rings = new ArrayList<Ring>();
    }
    
    // DRAW
    void draw(){
      //
      background(bgc);
      ListIterator<Ring> i=rings.listIterator();
      Ring r;
      while(i.hasNext()){
        // draw each ring
        r=i.next();
        if (mortality <= (frameCount-r.birth)) i.remove();
        else {
          // draw the ring
          r.draw();
        }
      }
    }
    
    // FUNCTIONS
    void mousePressed(){
      // Add a new ring
      rings.add(new Ring(mouseX, mouseY, elc));
    }
    
    void mouseDragged(){
      rings.add(new Ring(mouseX, mouseY, elc));
    }
    

    class

    class Ring {
      //
      int px,py;
      int birth;
      int clr; // initial full intensity color
      int initialSize = 30; // pixels
      int expansionRate = 30; // pixels/frame
      int timeConstant = 60; // frames (2sec)
    
      void Ring(int c) {
        Ring(0,0,c);
      }
    
      void Ring(int x, int y, int c){
        px = x;
        py = y;
        clr = c;
        birth = frameCount;
      }
    
      void draw(){
        // draw the ring
        int age = frameCount-birth;
        float decay = 1.0;
        //float decay = 
        noStroke();
        colorMode(HSB, 1.0);
        color c = color(hue(clr), saturation(clr), decay * brightness(clr));
        fill(c);
        int diam = age*expansionRate;
        diam = (diam < intialSize)? initialSize : diam;
        ellipse(px, py, diam, diam);
      }
    }
    
  • _vk_vk
    edited October 2013

    Hi, I think your problem is that constructors doesn't have a return type, neither "void", so actually you have no constructors...

    In the class, on lines 10 and 14 get rid of "void", and you are ok.

    after doing that There were some other errors, I comment them out and code runs fine :)

    try...

  • edited October 2013

    ...yes, I was in the middle of editing the class when I copied it.

    nixed the voids and now getting a Ring(int, int, int) does not exist on line 11.

    class Ring {
      //
      int px,py;
      int birth;
      int clr; // initial full intensity color
      int initialSize = 30; // pixels
      int expansionRate = 30; // pixels/frame
      float timeConstant = 60.0; // frames (2sec)
    
      Ring(int c) {
        Ring(0,0,c);
      }
    
      Ring(int x, int y, int c){
        px = x;
        py = y;
        clr = c;
        birth = frameCount;
      }
    
      void draw(){
        // draw the ring
        int age = frameCount-birth;
        float decay = 1.0;
        //float decay = exp(age/timeConstant);
        noStroke();
        colorMode(HSB, 1.0);
        color c = color(hue(clr), saturation(clr), decay * brightness(clr));
        fill(c);
        int diam = age*expansionRate;
        diam = (diam < intialSize)? initialSize : diam;
        ellipse(px, py, diam, diam);
      }
    }
    
  • _vk_vk
    Answer ✓

    that's because in the first constructor you call a the other constructor in a way, i believe is wrong. A constructor is expected to be called using new key word. I think I would do some thing like this (untested):

     Ring(int c) {
        px = 0;
        py = 0;
        clr = c;
        birth = frameCount;
      }
    
      Ring(int x, int y, int c){
        px = x;
        py = y;
        clr = c;
        birth = frameCount;
      }
    
  • cascading constructors should be fine and are used in C++, C#, and Java. I tried changing 11 to this.Ring(...) but that didn't work either.

  • I /commented/ out the fist constructor and it worked fine.

    Not sure why the cascade didn't work.

  • _vk_vk
    Answer ✓

    Indeed, here what I found in google:

    /*
     * edited from this question in stackoverflow 
     * http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java/16080312#16080312
    */
    
    
     void setup(){
        Cons c = new Cons();
        println(c.arg1 + "  " +c.arg2 + "  " + c.arg3);
     }
    
    
     class Cons {
    
       int arg1;
       int arg2;
       int arg3; 
    
     public Cons() {
      // A no arguments constructor that sends default values to the largest
      this(1,2,3);
     }
    
     public Cons(int arg1, int arg2) {
      // An example of a partial constructor that uses the passed in arguments
      // and sends a hidden default value to the largest
      this(arg1,arg2, 4);
     }
    
     // Largest constructor that does the work
     public Cons(int arg1, int arg2, int arg3) {
      this.arg1 = arg1;
      this.arg2 = arg2;
      this.arg3 = arg3;
     }
    }
    
  • edited October 2013

    Ahh this(); I switch between C++, C, Java, and JavaScript and these subtleties get me all the time...

Sign In or Register to comment.