How to use simple inheritance

edited January 2016 in JavaScript Mode

Hi all. I asked about using abstract classes yesterday here, but I think I have to go back and get the hang of something simpler first. (It's killing me! because I've read so much about this that it feels like I should know it all already!) So I'm trying to make a button class with the basic button characteristics, and make particular subclasses with different functionality.

here's what I've got so far: A main class called ButtonLinker:

void setup() {

  size(960, 600);

  line(120);
  stroke(3);
}
FirstButton firstBtn   = new FirstButton();
void draw() {
  firstBtn.showBtn();
}

a class called Button:

class Button {

  int xPos;
  int yPos;
  int lth ;
  int wth;
}

and another class called FirstButton:

    class FirstButton extends Button {


  {



    xPos = 300;
    yPos = 300;
    lth = 50;
    wth = 50;
  }
  void showBtn() {

    rect(xPos, yPos, lth, wth);
  }
}

anyone see what's wrong?

Answers

  • Your FirstButton class doesn't have a constructor. You need to change line 4 to:

    FirstButton() {
    
  • edited January 2016
    • I guess your problem is that some elements are outside their proper curling braces' scope.
    • Processing's IDE (PDE) got an über auto-indentation feature by hitting CTRL+T.
    • I advise you to do so there and re-post your code afterwards here.
    • I bet you're gonna spot many misplaced code lines! :>
  • edited January 2016
    // forum.Processing.org/two/discussion/14485/how-to-use-simple-inheritance
    // 2015-Jan-15
    
    final Button firstBtn = new FirstButton();
    
    void setup() {
      size(600, 400);
      smooth(4);
      noLoop();
    
      strokeWeight(3);
      stroke(0200);
      fill(#FFFF00);
    }
    
    void draw() {
      background(0);
      firstBtn.showBtn();
    }
    
    abstract class Button {
      int xPos;
      int yPos;
      int lth;
      int wth;
    
      abstract void showBtn();
    }
    
    class FirstButton extends Button {
      {
        xPos = 300;
        yPos = 300;
        lth  = 50;
        wth  = 50;
      }
    
      @ Override void showBtn() {
        rect(xPos, yPos, lth, wth);
      }
    }
    
  • edited January 2016

    Thank you both. I'm sorry, I should have said I'm working on a processing.js project using Processing 2.2.1. The code above works for the java version, but not for the javascript version.

    /*I just copy-pasted my own code above into a new sketch, and control-T'd it. Then commented out the dud line line(120); and it ran in the _java_ version. So I can't be a million miles away. Any thoughts on what's stopping it from running as .js? */

  • edited January 2016 Answer ✓

    JS Mode (PJS) can't transpile all Java features to JS.
    There are 2 things in my sample that JS Mode fails at:
    @Override & Initialization block inside a class.
    W/ just some tiny adjustments it works for PJS too: :bz

    http://ProcessingJS.org/tools/processing-helper.html

    // forum.Processing.org/two/discussion/14485/how-to-use-simple-inheritance
    // 2015-Jan-16
    
    final Button firstBtn = new FirstButton();
    
    void setup() {
      size(600, 400);
      smooth(4);
      noLoop();
    
      strokeWeight(3);
      stroke(0200);
      fill(#FFFF00);
    }
    
    void draw() {
      background(0);
      firstBtn.showBtn();
    }
    
    abstract class Button {
      int xPos;
      int yPos;
      int lth;
      int wth;
    
      abstract void showBtn();
    }
    
    class FirstButton extends Button {
      FirstButton() {
        xPos = 300;
        yPos = 300;
        lth  = 50;
        wth  = 50;
      }
    
      void showBtn() {
        rect(xPos, yPos, lth, wth);
      }
    }
    
  • That works. I'll experiment with it.

    Thanks very much!

Sign In or Register to comment.