Rect blink

/* assignment: to capture the keypress and make rect blink according to the 'key' pressed*/

color cBoxColor=color(255,0,0); //initial color of obx
color cSelected=color(0,255,0); // color of box after selection
boolean b1Selected=false;
boolean b2Selected=false;
boolean b3Selected=false;
boolean b4Selected=false;

int Height=100;
int boxSide=Height/2;
int Width=4*boxSide+5*(boxSide/2);;

int y=Height/4;
int x1=boxSide/2;
int x2=x1+boxSide+boxSide/2;
int x3=x2+boxSide+boxSide/2;
int x4=x3+boxSide+boxSide/2;

void setup(){
size(Width,Height);
background(255,255,255);
}

void draw(){
noStroke();
if(b1Selected)
fill(cSelected);
else
fill(cBoxColor);
rect(x1,y,boxSide,boxSide);

if(b2Selected)
fill(cSelected);
else
fill(cBoxColor);
rect(x2,y,boxSide,boxSide);

if(b3Selected)
fill(cSelected);
else
fill(cBoxColor);
rect(x3,y,boxSide,boxSide);

if(b4Selected)
fill(cSelected);
else
fill(cBoxColor);
rect(x4,y,boxSide,boxSide);
}

void keyPressed(){
if(key=='a')
b1Selected=true;
if(key=='b')
b2Selected=true;
if(key=='c')
b3Selected=true;
if(key=='d')
b4Selected=true;
}

void keyReleased(){
b1Selected=false;
b2Selected=false;
b3Selected=false;
b4Selected=false;
}

Programme output:

The four rectangles corospond to four chars 'a','b','c','d'.
2a

which ever key user will press corrosponding rectangle will change its color

'b' pressed
2b

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