Trying to convert 2 objects into a super class and a subclass

I’ve watched the videos, read the reference, but am hitting a wall. Not sure what I’m overlooking at this point.

Attempting to convert an earlier sketch into one implementing inheritance classes.

I think part of the problem are the functions: void rectAddColor() and void arcAddColor() in the super and subclasses.

Including 2 sketches:
The FIRST is the problem sketch.
The SECOND is the original sketch for reference.
I’m trying to have the FIRST and SECOND sketches look the same when run.

Any guidance / hints most welcome!
Thank you!
:nerd_face:

FIRST SKETCH here is problematic (doesn’t run):

// halfmoon motif from SUPER-CLASS & SUB-CLASS
// --------------------------------------- Main
Halfmoon moonRect;
Halfcrescent moonArc;

void setup() {
  size(200, 200);

  moonRect = new Halfmoon(0, 0, 200);
  moonArc = new Halfcrescent(0, 0, 200);
  
  frameRate(1);
}

void draw() {
background (255);

  moonRect.display();
  moonArc.display();
}
// --------------------------------- Super Class

class Halfmoon {

  color c1 = #FFFFFF;
  color c2 = #BEBEBE;
  color c3 = #7F7F7F;
  color c4 = #3F3F3F;
  color c5 = #000000;

  color [ ] grays = {c1, c2, c3, c4, c5};

  int x, y, sz;

  Halfmoon (int tempX, int tempY, int tempSz) {
    x = tempX;
    y = tempY;
    sz = tempSz;
  }

  void display() {
    noStroke();
    translate (sz/2, sz/2);
    rectAddColor (grays[int(random(grays.length))]);
  }

  void rectAddColor (color rectColor) {

    fill (rectColor);
    rectMode (CENTER);
    rect (x, y, sz, sz);
  }
}
// -------------------------------------------- subClass

class Halfcrescent extends Halfmoon {

  float deg = 90;
  float rad = radians (deg);
  
  //int x;
  //int y;
  //int sz;


  Halfcrescent (int x, int y, int sz) {
    super (x, y, sz);
  }

  void display() {

    noStroke();
    float r = random(1);
    println (r);

    if (r < 0.40) {
      rotate (rad);
    } else if (r > 0.40 && r < 0.50) {
      rotate (rad*3);
    } else if (r > 0.50 && r < 0.65) {
      rotate (rad*2);
    } else {
      rotate (rad*4);
    }

    arcAddColor (grays[int(random(grays.length))]);
  }
}
void arcAddColor (color arcColor) {
  
  //int x = 0; int y = 0; int sz = 0;
  
  fill (arcColor);
  arc (x, y, sz, sz, 0, PI);
}

SECOND SKETCH for reference (trying to make FIRST sketch look like this):

// halfmoon motif from 2 SEPARATE OBJECTS

HalfmoonRect hlfMn_R;
HalfmoonArc hlfMn_A;

void setup() {
  size(200, 200);

  hlfMn_R = new HalfmoonRect();
  hlfMn_A = new HalfmoonArc();
  
  frameRate(1);
}

void draw() {

  background (255);

  hlfMn_R.display();
  hlfMn_A.display();

}
class HalfmoonArc {

  color c1 = #FFFFFF;
  color c2 = #BEBEBE;
  color c3 = #7F7F7F;
  color c4 = #3F3F3F;
  color c5 = #000000;

  color [] grays = {c1, c2, c3, c4, c5};

  float deg = 90; // 90 degrees
  float rad = radians(deg); // convert to radians

  //-------------------------------------
void display() {

    noStroke();

    float r = random(1);
    println (r);

    if (r < 0.40) {
      rotate (rad);
    } else if (r > 0.40 && r < 0.50) {
      rotate (rad*3);
    } else if (r > 0.50 && r < 0.65) {
      rotate (rad*2);
    } else {
      rotate (rad*4);
    }

    arcPlusColor(grays[int(random(grays.length))]);
  }
}
//----------------------------------------

void arcPlusColor (color arcColor) {
  fill (arcColor);
  arc (0, -100, 200, 200, 0, PI);
}

class HalfmoonRect {

  color c1 = #FFFFFF;
  color c2 = #BEBEBE;
  color c3 = #7F7F7F;
  color c4 = #3F3F3F;
  color c5 = #000000;

  color [] grays = {c1, c2, c3, c4, c5};
  
  //-----------------------------

void display() {

    noStroke();

    translate (100, 100);

    rectPlusColor(grays[int(random(grays.length))]);
  }
  
  //-----------------------------

void rectPlusColor (color rectColor) {
    fill (rectColor);
    rectMode (CENTER);
    rect (0, 0, 200, 200);
  }
}
1 Like

Create a base class, then use extends on your classes you want to inherit from base.

https://processing.org/reference/extends.html
https://processing.org/reference/super.html

HalfmoonRect hlfMn_R;
HalfmoonArc hlfMn_A;

void setup() {
  size(200, 200);

  hlfMn_R = new HalfmoonRect();
  hlfMn_A = new HalfmoonArc();

  frameRate(1);
}

void draw() {

  background (255);

  hlfMn_R.display();
  hlfMn_A.display();
}
class Halfmoon {

  color c1 = #FFFFFF, 
    c2 = #BEBEBE, 
    c3 = #7F7F7F, 
    c4 = #3F3F3F, 
    c5 = #000000;
  color[] grays = {c1, c2, c3, c4, c5};  

  float deg, rad;

  Halfmoon() {    
    deg = radians(90);
  }

  void arcPlusColor (color arcColor) {
    fill (arcColor);
    arc (0, -100, 200, 200, 0, PI);
  }

  void rectPlusColor (color rectColor) {
    fill (rectColor);
    rectMode (CENTER);
    rect (0, 0, 200, 200);
  }
}
class HalfmoonArc extends Halfmoon {
  HalfmoonArc() {
    super();
  }

  void display() {
    noStroke();
    float r = random(1);
    println (r);

    if (r < 0.40) {
      rotate (deg);
    } else if (r > 0.40 && r < 0.50) {
      rotate (deg*3);
    } else if (r > 0.50 && r < 0.65) {
      rotate (deg*2);
    } else {
      rotate (deg*4);
    }

    arcPlusColor(grays[int(random(grays.length))]);
  }
}
class HalfmoonRect extends Halfmoon {

  HalfmoonRect() {
    super();
  }

  void display() {
    noStroke();
    translate (100, 100);
    rectPlusColor(grays[int(random(grays.length))]);
  }
}
2 Likes

Thank you @slow_izzm for this!!
I now see what I was doing wrong!
:nerd_face:

1 Like