Hello
I wanted to make 4buttons that show individual color. however, when I click on each of them all buttons are turned on together. I know my problem is with the draw event but I don’t know what should I do.
may I kindly ask you to help me?
color colA = color(125);
color colB = color(0,255,0);
color colC = color(0,0,255);
color colD = color(255,0,0);
color colE = color(120,0,200);
float colSwitch = 0;
float X = 740;
float Y = 740;
float x;
float y;
void setup()
{
size(740,740);
background(0);
}
void draw()
{
rect(0,0,X/2,Y/2);
rect(width/2,0,X/2,Y/2);
rect(0,height/2,X/2,Y/2);
rect(width/2,height/2,X/2,Y/2);
//line(width/2,0,width/2,Y);
//line(0,height/2,X,height/2);
}
void mousePressed()
{
if(mouseX > width/4-X/4 && mouseX < width/4+X/4 && mouseY > height/4-Y/4 && mouseY< height/4+Y/4 )
{
fill(colB);
}else if(mouseX > width/2 && mouseX<width/2+X/2 && mouseY > height/2-Y/2 && mouseY< Y/2)
{
fill(colC);
}else if(mouseX > width/4-X/4 && mouseX < width/4+X/4 && mouseY >height/2 && mouseY< Y)
{
fill(colD);
}else if(mouseX > width/2 && mouseX < width/2+X/2 && mouseY > height/2 && mouseY< Y)
{
fill(colE);
}
}
Why have you posted the same question twice. Once is plenty. Also please format your code, to improve readability. CTRL + E
Im sorry its because of lack of the internet in my country
Because each button has a different color you have to set each color before drawing each rectangle
PARINAZ129:
void draw()
{
rect(0,0,X/2,Y/2);
rect(width/2,0,X/2,Y/2);
rect(0,height/2,X/2,Y/2);
rect(width/2,height/2,X/2,Y/2);
before each of the rect() commands say fill(…color ) ;
void draw()
{
fill(colB);
rect(0,0,X/2,Y/2);
fill(colC);
rect(width/2,0,X/2,Y/2);
fill(colD);
rect(0,height/2,X/2,Y/2);
fill(colE);
rect(width/2,height/2,X/2,Y/2);
Then in mousePressed()
say
colE=color(random(255),random(255),random(255));
or
colB=color(random(255),random(255),random(255));
etc.
Don’t use fill() in mousePressed() since you say that in draw().
Warm regards,
Chrisir
glv
July 4, 2021, 8:38pm
6
@PARINAZ129 ,
I have shared these two with you in other posts:
Assuming you are working on a PC:
The references are available locally on your PC as well:
As a beginner you should be going through the tutorials and read all the references + related references for each function along with related examples:
You can also explore the examples (available locally on PC) to get some insight:
There is a learning curve to all of this and you have to invest some time in yourself.
:)