Is it possible to have and array of objects with varying parameters?

Hi everyone,

I’m just getting started with Processing and I’m writing a simple, one-octave piano keyboard.

I have a class named Key:

class Key{

  float x;
  float y;
  float w;
  float h;
  color col;
  
  
  Key(float keyX, float keyY, float keyW, float keyH, color keyCol){
    x = keyX;
    y = keyY;
    w = keyW;
    h = keyH;
    col = keyCol;
  }
  
  
  void display(){       
    fill(col);
    rect(x,y,w,h,0,0,4,4); 
}
      
}

I then make 12 instances of this class and call the display function on each, which gives me the familiar keyboard layout.

I thought it would be better to make an array of Keys which would be more efficient than writing the the code 12 times, but I need to be able to pass arguments to the constructor for each separate object, as the X position, dimensions, and the colour of the key will change.

With an array and loop like this:

Key[] keys = new Key[12];

for (int i = 0; i < 12; i++){
   keys[i] = new Key();
}

for (int i = 0; i < 12; i++){
   keys[i].display();
}

Is there a way to change the parameters of each individual key?

1 Like

if you write that class “Key” you would know how to use?

Key[] keys = new Key[12];

void setup() {
  size(500, 300);
  int x =40, y =50, w =35, h=140;
  color cw = color(220, 210, 200);
  color cb = color(40, 20, 60);
  for (int i = 0; i < 12; i++) { 
    color ci = cw;
    if ( i == 3 || i ==7 ) ci = cb;
    keys[i] = new Key(x+i*w, y, w, h, ci);
  }
}

void draw() {
  background(200, 200, 0);
  for (int i = 0; i < 12; i++)  keys[i].display();
}

class Key {
  float x, y, w, h;
  color col;
  Key(float keyX, float keyY, float keyW, float keyH, color keyCol) {
    x = keyX;
    y = keyY;
    w = keyW;
    h = keyH;
    col = keyCol;
  }
  void display() {       
    fill(col);
    rect(x, y, w, h, 0, 0, 4, 4);
  }
}

2 Likes

There is a tutorial about objects

Read about the constructor there

You can pass parameters to the constructor and so give each object its own position

The class is the cookie maker, the objects in your array are the cookies

Thanks so much for your detailed reply!

I tried plugging my numbers into your loop, and while I don’t get exactly what I’m looking for, this has certainly helped me with the thought process.

I hadn’t thought of initialising the variables in setup as I was setting those values in arguments and that seemed to work well.

Before considering an array, the code in my main sketch was:

Key C;
Key D;
Key E;
Key F;
Key G;
Key A;
Key B;

Key cSharp;
Key dSharp;
Key fSharp;
Key gSharp;
Key aSharp;

void setup(){
  size(560,280);
  background(250); 

 C = new Key(0,0,(width/7),height,color(250));
  D = new Key(80,0,(width/7),height,color(250));
  E = new Key(160,0,(width/7),height,color(250));
  F = new Key(240,0,(width/7),height,color(250));
  G = new Key(320,0,(width/7),height,color(250));
  A = new Key(400,0,(width/7),height,color(250));
  B = new Key(480,0,(width/7),height,color(250));
  
  cSharp = new Key(60,0,(width/14),height/1.7,color(0));
  dSharp = new Key(140,0,(width/14),height/1.7,color(0));
  fSharp = new Key(300,0,(width/14),height/1.7,color(0));
  gSharp = new Key(380,0,(width/14),height/1.7,color(0));
  aSharp = new Key(460,0,(width/14),height/1.7,color(0));
  
}

void draw(){
  
  C.display();
  D.display();
  E.display();
  F.display();
  G.display();
  A.display();
  B.display();
  
  cSharp.display();
  dSharp.display();
  fSharp.display();
  gSharp.display();
  aSharp.display();   

}

This code gives me the layout I need, it’s just very convoluted! Here’s what the sketch looks like:

Capture

So I have keys of different sizes and colours which is causing me an issue.

The other reason I want to use an array is that I eventually want to attach sound to these keys, so I was hoping to use a loop to determine which of the key objects is pressed during a mousePressed event or something similar.

You have set me on the right path though, so thanks again!

1 Like

Hey, thanks for your response!

I was aware of passing arguments to the constructor, I was doing that in my initial code but I wasn’t sure how to do that in an array an loop scenario.

I will take a look at the tutorial you’ve suggested!

1 Like