Classes and ArrayList

So I am trying to make an Aquarium. This is a project to help with object oriented progamming. First I made bubbles that start at the bottom of the screen and goes to the top. Once they reach the top it resets, goes to the bottom, and spawns at another x coordinate. Then I made fish that swim around the screen. They spawn and start moving, and when they hit an end of the screen, the fish turns around and goes the opposite direction.

Now here is the part where I have trouble. I have to make a single object that both the bubbles and the fish inherit from. There should be behavior that both the bubbles and the fish share in the code. Once I do that, I need to consolidate the fish and bubbles arrays into a single AquariumEntity ArrayList that is iterated over twice calling update() & draw().

I really don’t understand how to do this can someone help?

1 Like

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

not sure whether this is what you are looking for

maybe you need something called interface

Chrisir

2 Likes

example for interface

// mcve for interface with different classes
// draw program

// the nodes 
ArrayList<Node> nodes = new ArrayList();

// data for drawing a rect 
boolean startingRect=false;
PVector start;

// --------------------------------------------
// Core functions 

void setup() {
  size(900, 800);

  // add new EllipseClass/RectClass with random color 
  color randomColor = color(random(0, 255), random(0, 255), random(0, 255));

  nodes.add ( new EllipseClass(44, 44, 
    randomColor));
  nodes.add ( new EllipseClass(111, 111, 
    randomColor));
  nodes.add ( new RectClass(211, 211, 
    16, 16, 
    randomColor));
} // func 

void draw() {
  // clear screen 
  background(255);

  // for-loop
  for (int i = 0; i < nodes.size (); i++) { 
    Node someClass  = nodes.get(i);
    someClass.display();
  } // for 

  // small demo
  if (frameCount>60 && frameCount<64) {     
    Node someClass  = nodes.get(1);
    someClass.incrementXY(22, 0); // change position 
    someClass.incrementXY(33, 0);
  } // 

  if (startingRect) {
    rect(start.x, start.y, mouseX-start.x, mouseY-start.y);

    stroke(255, 0, 0); 
    line(start.x-9, start.y, 
      start.x+9, start.y);
    line(start.x, start.y-9, 
      start.x, start.y+9);
    stroke(255); 

    fill(0); 
    text("Click again to store rect (press space bar to abort)", 19, 19);
    text("Click again", mouseX+19, mouseY+9);
  } else {
    fill(0); 
    text("Click and release to start drawing a rect", 19, 19);
  }
} // function draw()  

// ----------------------------------------------------------------------

void mousePressed() {

  if (!startingRect) {
    //first click 
    startingRect=true;
    start = new PVector(mouseX, mouseY);
  } else {
    //2nd click 
    startingRect=false;//reset 
    // add new RectClass with random color 
    color randomColor = color(random(0, 255), random(0, 255), random(0, 255));
    nodes.add ( new RectClass(start.x, start.y, 
      mouseX-start.x, mouseY-start.y, 
      randomColor));
    start=null;
  }
}

void keyPressed() {
  // abort rect 
  if (startingRect) {
    startingRect=false;//reset 
    start=null;
  }
}

// ==============================================
// interface and classes

class RectClass implements Node {  

  // pos
  float x=0;
  float y=0;

  float w, h;

  // color 
  color RectClassColor=0; 

  // constr 
  RectClass(float tempX, float tempY, 
    float wtemp, float htemp, 
    color tempRectClassColor) {
    x = tempX;
    y = tempY;

    w=wtemp;
    h=htemp; 

    RectClassColor = tempRectClassColor;
    //
  } // constr

  void display() {
    fill(RectClassColor);
    rect (x, y, w, h);
  }

  void setXY(float x_, float y_) {
    x=x_;
    y=y_;
  }

  void incrementXY(float x_, float y_) {
    x+=x_;
    y+=y_;
  }
  //
} // class

// -------------------------

class EllipseClass implements Node {  

  // pos
  float x=0;
  float y=0;

  // color 
  color RectClassColor=0; 

  // constr 
  EllipseClass(float tempX, float tempY, 
    color tempRectClassColor) {
    x = tempX;
    y = tempY;

    RectClassColor = tempRectClassColor;
    //
  } // constr
  //
  void display() {
    fill(RectClassColor);
    ellipse (x, y, 15, 15);
  }

  void setXY(float x_, float y_) {
    x=x_;
    y=y_;
  }

  void incrementXY(float x_, float y_) {
    x+=x_;
    y+=y_;
  }
  //
} // class

// -------------------------------------

interface Node {

  // Let's abstract

  // Let's abstract display()
  void display();
  void incrementXY(float x_, float y_);
}
//
3 Likes