We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, Im trying to make a small code in which when I draw a rectangle it initially is blue but what I want it to do is once I release the mouse it automatically changes to red. This is my code:
int x,y,x2,y2 = 0;
void setup() {
size(300, 300);
}
void draw() {
background(255);
fill(0,0,255);
rect(x, y, x2, y2);
}
void mousePressed() {
x = mouseX;
y = mouseY;
}
void mouseDragged() {
x2 = mouseX - x;
y2 = mouseY - y;
rect(x, y, x2, y2);
}
void mouseReleased() {
fill(255,0,0);
rect(x,y,x2,y2);
}
Answers
Thanks! I forgot to mention that when I draw another rectangle it does the same thing. So when I draw another rectangle it will be blue then red once i release
either create an ArrayList of colors or a Rectangle class and store the color there
So that means you want to keep the previous rect you drew? If you store all your rectangles in an arrays, you can display all rectangles that are done with a blue fill (with a for loop) and in mouseDragged you draw the one you are currently creating with a red fill.
No I want it to disappear when I draw another one
That needs to be in the code somewhere. It draws over the rectangle.
Check this.
Kf
Thanks!
This is confusing to me however because im kind of new to programming in general but ill learn along the way.
I eventually figured it out this way: int x, y, x2, y2 = 0;
is there any benefit of doing it either way?
Nop... both works as a demo.
Kf
https://www.Reddit.com/r/processing/comments/6e3pdf/how_do_i_change_color_of_a_rectangle_after/