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?