Exercise 2

Changing of colours in squares according to the key pressed. The ky assigned here is C,M,Y,K i.e. cyan, magnta, yellow and key colour.

int a=75;side of squares
int d=50;
depth of squares
int x1=20;
int x2=115;
int x3=210;
int x4=305;
void setup()
{
size(400,300);
background(255);
rect(x1,d,a,a);
rect(x2,d,a,a);
rect(x3,d,a,a);
rect(x4,d,a,a);
}

void draw()
{

}

void keyReleased()
{
if(key=='c'||key=='C')
{
fill(0,147,221);
rect(x1,d,a,a);
fill(255);
rect(x2,d,a,a);
rect(x3,d,a,a);
rect(x4,d,a,a);
}
else
{
if(key=='m'||key=='M')
{
fill(221,19,123);
rect(x2,d,a,a);
fill(255);
rect(x1,d,a,a);
rect(x3,d,a,a);
rect(x4,d,a,a);
}
else{
if(key=='y'||key=='Y')
{
fill(255,245,0);
rect(x3,d,a,a);
fill(255);
rect(x1,d,a,a);
rect(x2,d,a,a);
rect(x4,d,a,a);
}
else{
if(key=='k'||key=='K')
{
fill(31,26,23);
rect(x4,d,a,a);
fill(255);
rect(x1,d,a,a);
rect(x2,d,a,a);
rect(x3,d,a,a);
}
else{fill(255);}
}
}
}
}

Screenshots on screen:

C = CYAN

color1

M = MAGENTA

color2

Y = YELLOW

color3

K = KEY

color4
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License