Changing a child classes characteristics

I need to use inheritance for a project of mine. Currently, I Have been looking at this example code to create a person.

class Human implements Drawable{
  
  int armAngle = 0;
  int angleChange = 5;
  final int ANGLE_LIMIT = 135;

void draw()
{
  noStroke();
  fill(38, 38, 200);
  ellipse(73+x, 225+y, 40, 40); // head
  rect(50+x, 250+y, 50, 60); // body
  drawLeftArm();
  drawRightArm();
  rect(60+x, 300+y, 16, 50); // left leg
  rect(80+x, 300+y, 15, 50); // right leg
  
  fill(222, 222, 249);
  ellipse(80+x, 220+y, 8, 8); // left eye
  ellipse(65+x, 220+y, 8, 8);  // right eye
  
 
}

void drawLeftArm()
{
  pushMatrix();
  translate(12, 32);
  rotate(radians(armAngle));
  rect(25+x, 220+y, 10, 45); // left arm
  popMatrix();
}

void drawRightArm()
{
  pushMatrix();
  translate(66, 32);
  rotate(radians(-armAngle));
  rect(37+x, 220+y, 10, 45); // right arm
  popMatrix();
}

In my Child class, I have this

class Child extends Human{


  void draw(){
   
    super.draw();
     }
}

Obviously my problem is my child class is just drawing an exact copy on top of my Human class but I can not figure out how to alter the child class without just copy pasting the code over again making the inheritence pointless. Ideally I would like to make the child smaller and beside the Human parent class. How would i go about this?

1 Like

looks very good,
just need to deal with x and y
and please post a runable code if possible.

class Human {
  int armAngle = 0;
  int x,y;
  
  void draw() {
    move();
    push();
    translate(x,y);
    noStroke();
    fill(38, 38, 200);
    ellipse(73, 25, 40, 40); // head
    rect(50, 50, 50, 60); // body
    drawLeftArm();
    drawRightArm();
    rect(60, 100, 16, 50); // left leg
    rect(80, 100, 15, 50); // right leg

    fill(222, 222, 249);
    ellipse(80, 20, 8, 8); // left eye
    ellipse(65, 20, 8, 8);  // right eye
    pop();
  }
  
  void move() {
    x += 1;
    if ( x > width ) x = 0;
    //y = 100;
    armAngle += 5;
  }

  void drawLeftArm() {
    pushMatrix();
    translate(52, 52);
    rotate(radians(armAngle));
    rect(-5, 0, 10, 45); // left arm
    popMatrix();
  }

  void drawRightArm() {
    pushMatrix();
    translate(96, 52);
    rotate(radians(-armAngle));
    rect(-5, 0, 10, 45); // right arm
    popMatrix();
  }
}

class Child extends Human {
  void draw() {
    super.draw();
  }
}

Child one = new Child();
Child two = new Child();

void setup() {
  size(500,500);
  one.y = 100;
  two.y = 300;
  two.x = 200;
}

void draw() {
  background(200,200,0);
  one.draw();
  two.draw();
}

thanks, follow @quark

repair and add color and size
class Human {
  private int armAngle = 0;
  public int x,y,dx=0,dy=0;
  public color colf = color(38, 38, 200);
  public float big=1.0;
  
  void draw() {
    move();
    pushMatrix();
    translate(x,y);
    scale(big);
    noStroke();
    fill(colf);
    ellipse(73, 25, 40, 40); // head
    rect(50, 50, 50, 60); // body
    drawLeftArm();
    drawRightArm();
    rect(60, 100, 16, 50); // left leg
    rect(80, 100, 15, 50); // right leg

    fill(222, 222, 249);
    ellipse(80, 20, 8, 8); // left eye
    ellipse(65, 20, 8, 8);  // right eye
    popMatrix();
  }
  
  void move() {
    x += dx;
    if ( x > width ) x = 0;
    y += dy;
    if ( y > height ) y = 0;
    armAngle += 5;
  }

  void drawLeftArm() {
    pushMatrix();
    translate(52, 52);
    rotate(radians(armAngle));
    rect(-5, 0, 10, 45); // left arm
    popMatrix();
  }

  void drawRightArm() {
    pushMatrix();
    translate(96, 52);
    rotate(radians(-armAngle));
    rect(-5, 0, 10, 45); // right arm
    popMatrix();
  }
}

class Child extends Human {
  void draw() {
    super.draw();
  }
}

Child one = new Child();
Child two = new Child();

void setup() {
  size(500,500);
  // set public things about Child/Human
  one.dx = 1;
  one.y = 100;
  
  two.x = 200;
  two.dx = 2;
  two.y = 300;
  two.colf = color(200,0,200);
  two.big = 0.5;
}

void draw() {
  background(200,200,0);
  one.draw();
  two.draw();
}

1 Like

@kll in the Human class draw method the push and pop should be pushMatrix and popMatrix

3 Likes

What are you trying to accomplish in Child that Human does not do? If you want a child to be smaller, than Human should have a signature

Human.draw(myHeight)

…and draw based on that height. Or it has an int height property that is set when you call the myHuman = new Human(myheight) constructor. Then Human mychild doesn’t need inheritence – it is just a Human with a short height.

If you are trying to do something else with inheritance, what is it? At that point, you can override draw() in Child and write a new one, rather than (or in addition to) parameterizing it.

1 Like