Do not know where to add the constructor

edited December 2017 in Programming Questions

I mean in a normaal java class you would like code

class Whatever {
  public Whatever() {}
}

but now where there is just the void setup() and draw() I do not exactly know how/where to add a cunstructor to the first class. So... could anybody tell me what to do?

Answers

  • class Whatever {
    Whatever(){}
    }
    
  • edited December 2017 Answer ✓
    • In order to add your own custom constructor for the sketch, you need to know its actual name.
    • For the PDE (Processing's IDE), that name matches the sketch's own folder name.
    • Which is also the ".pde" filename of the PDE's main 1st tab.
    • Let's say you saved your sketch and chose the name "Whatever" for it.
    • Then its folder is also named "Whatever/" and the main tab is named "Whatever.pde".
    • Then you've got all the "ingredients" for your custom constructor "hack"! Here it is: :ar!

    public Whatever() {
      println("Constructor: " + getClass().getName());
    }
    
  • @GoToLoop well actually I feel a little dumb. I already tried to make a constructor with the file's name but I directly wanted to implement something like a ComponentListener and that did not work and idk. Thx

  • edited December 2017 Answer ✓

    see tutorial objects:

    https://www.processing.org/tutorials/objects/

    Example:

    here is a nice example by TfGuy

    ArrayList<Bullet> bullets = new ArrayList();
    
    Tower t0 = new Tower( 100, 100 );
    Tower t1 = new Tower( 100, 300 );
    Tower t2 = new Tower( 300, 300 );
    Tower t3 = new Tower( 300, 100 );
    
    void setup() {
      size( 400, 400 );
    }
    
    void draw() {
      background( 0 );
    
      for ( int i = bullets.size() - 1; i >= 0; i-- ) 
        if ( bullets.get(i).draw() ) 
          bullets.remove(i);
    
      t0.draw();
      t1.draw();
      t2.draw();
      t3.draw();
    
      stroke( 255, 0, 0 );
      noFill();
      ellipse( mouseX, mouseY, 20, 20 );
      ellipse( mouseX, mouseY, 40, 40 );
      line( mouseX - 30, mouseY, mouseX + 30, mouseY );
      line( mouseX, mouseY - 30, mouseX, mouseY + 30 );
    }
    
    void mousePressed() {
      //bullets.add( new Bullet() );
    }
    
    // ===========================================================
    
    class Bullet {
    
      PVector p, v;
      color colorBullet;
    
      // constructor
      Bullet( float ipx, float ipy, color iColor ) {
        p = new PVector( ipx, ipy, 0 );
        float a = atan2(mouseY-ipy, mouseX-ipx );  // random( TWO_PI );
        v = new PVector( 5*cos(a), 5*sin(a), 0 );
        colorBullet=iColor;
      }
    
      boolean draw() {
        p.add( v );
        fill( colorBullet );
        noStroke();
        ellipse( p.x, p.y, 10, 10 );
        boolean targetReached = dist(p.x, p.y, mouseX, mouseY) < 11; 
        return( targetReached  ||  !on_screen() );
      }
    
      boolean on_screen() {
        return( p.x > -20 && 
          p.x < width + 20 &&
          p.y > -20 && 
          p.y < height + 20 );
      }
    }
    
    // ===========================================================
    
    class Tower {
    
      float px, py;
      int time;
      color colorBullet=color(random(255), random(255), random(255)); 
    
      // constructor
      Tower( float ipx, float ipy ) {
        px = ipx;
        py = ipy;
        new_time();
        time += random(500); // Mix it up a bit.
      }
    
      void new_time() {
        time = millis() + 500;
      }
    
      void draw() {
        fill( 0, 0, 255 );
        stroke( 0 );
        triangle( px, py, px+5, py+10, px-5, py+10 );
        if ( millis() > time ) {
          new_time();
          // Create new bullet at this tower's position.
          bullets.add( new Bullet( px, py, colorBullet ) );
        }
      }
    }
    //
    
  • @Chrisir may I ask why exactly there are no modifiers like private or public at all? I mean in normal Java this would be considered as dirty coding...

  • edited December 2017 Answer ✓
    • All code inside ".pde" files are concatenated & transpiled as 1 big ".java" file.
    • And then wrapped up as a subclass of PApplet.
    • Therefore, all classes & interfaces inside our ".pde" files are nested to that PApplet top subclass.
    • B/c everything are members of that PApplet top subclass, private access level can't prohibit anything from accessing each other within it. >-)
  • (if you export a project and dig around in the created directories you can find the java file it creates)

Sign In or Register to comment.