We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › change background color
Page Index Toggle Pages: 1
change background color (Read 746 times)
change background color
Jun 26th, 2008, 12:03am
 
i have a stupid newby question, but don't find an answer to it.
I have a simple script and want to draw rects to my screen with a white outline stroke. but by pressing "+" and "-" one can change the background color. Therefore i've put the background () code in the draw() section.
actually i want to see all the rects that have been drawn to the screen not just the one right at the position of the mouse.
I thought of using the begindraw() / endDraw / PGraphics function but did not came to a solution.
Is there a way to solve this problem out? probably it's just not possible to change the background color without deleting the whole stuff drawn to the screen?



color backgr;
void setup() {

 size (500,500);
 smooth();
 noFill();
}

void draw() {
 background (backgr);
 stroke (255);
 rect (mouseX,mouseY,50,50);
}

void keyPressed() {

 if (key == '+' && backgr <=255) {
   backgr +=10;
 }
 if (backgr >= 255) {
   backgr = 255;
 }

 if (key == '-' && backgr >=0) {
   backgr -=10;
 }
 if (backgr <= 0) {
   backgr = 0;
 }
}

Re: change background color
Reply #1 - Jun 26th, 2008, 12:04am
 
You'll have to store the drawn rectangles, and re-draw them each frame, as to change background, everything gets cleared.
Re: change background color
Reply #2 - Jun 26th, 2008, 9:52am
 
ok, and how do i store rectangles? and of course how to r-draw them?
is there any code example online?

Re: change background color
Reply #3 - Jun 26th, 2008, 9:59am
 
to store rectangles you can keep their co-ordinates in some array funcionality, and redraw each rectangle in draw() using "for" loop for example.

some quick draft code:

class myRect{ // class for storing rect co-ordinates
float x,y,w,h;
myRect(float _x, float _y, float _w, float _h){
  x = _x;
  y = _y;
  w = _w;
  h = _h;
}
}

ArrayList myRectangles = new ArrayList();
// add some rectangles to the list here:
myRectangles.add(new myRect(1,1,100,100));
myRectangles.add(new myRect(100,100,100,100));


void draw(){
background(0); // this clear background each time

for( int i = 0; i < myRectangles.size(); i++)
rect(myRectangles[i].x,myRectangles[i].y,myRectangles[i].w,myRectangles[i].h);
}

hope this would help
Re: change background color
Reply #4 - Jun 26th, 2008, 1:52pm
 
dear jakub,

your script does not work . when trying to start it, i receive an error for these two lines:

myRectangles.add(new myRect(1,1,100,100));
myRectangles.add(new myRect(100,100,100,100));

there must be something wrong with the round bracket (.

may you try to fix it? i sadly don't know how. i then'll try if your code might be the solution for my problem... thanks a lot
Re: change background color
Reply #5 - Jun 26th, 2008, 8:35pm
 
It was just quick pseudo-code to put you on the track... Smiley

An issue was the lack of setup(), another was his use of array index on ArrayList...
To keep the idea of extensible list, I give a corrected code:
Code:
class MyRect{ // class for storing rect co-ordinates
float x,y,w,h;
MyRect(float _x, float _y, float _w, float _h){
x = _x;
y = _y;
w = _w;
h = _h;
}
void draw(){
rect(x, y, w, h);
}
}

ArrayList myRectangles;
color backgr;

void setup()
{
size (500,500);
smooth();
noFill();
myRectangles = new ArrayList();
// add some rectangles to the list here:
myRectangles.add(new MyRect(1,1,100,100));
myRectangles.add(new MyRect(100,100,100,100));
}

void draw(){
background(backgr); // this clear background each time
stroke(255);
for (int i = 0; i < myRectangles.size(); i++)
((MyRect)myRectangles.get(i)).draw();
}

void keyPressed() {
if (key == '+') {
backgr += 10;
}
if (backgr > 255) {
backgr = 255;
}

if (key == '-') {
backgr -= 10;
}
if (backgr < 0) {
backgr = 0;
}
}

Note I delegated the drawing to the object itself: thus it is a little more than a simple structure.
Re: change background color
Reply #6 - Jun 27th, 2008, 4:59pm
 
ok, i see how this is supposed to work.
it's new to me that one can use the draw() more than once in a sketch. good to know. ..

but still it's not exactly the way i want it to be.
i have to add the rectangles to the ArrayList() but actually i want to draw the rects by moving the mouse. It's sort of a drawing program in which the user can draw rects and change the background color without deleting the drawn rects.
As the ArrayList is filled with data in the setup() i cannot use mouseX, mouseY variables.

how do i have to change your code to use it dynamically?

hope i don't get too much on your nerves Wink thanks a lot for your help!
Re: change background color
Reply #7 - Jun 27th, 2008, 5:18pm
 
put the statement that adds a rectangle to the list in the draw() method :

Quote:
class MyRect{ // class for storing rect co-ordinates
 float x,y,w,h;
 MyRect(float _x, float _y, float _w, float _h){
   x = _x;
   y = _y;
   w = _w;
   h = _h;
 }
 void draw(){
   rect(x, y, w, h);
 }
}

ArrayList myRectangles;
color backgr;  

void setup()
{
 size (500,500);
 smooth();
 noFill();  
 myRectangles = new ArrayList();
 rectMode(CENTER);
}

void draw() {
 // add some rectangles to the list here:
 if (mousePressed) myRectangles.add(new MyRect(mouseX, mouseY, 100, 100));
 background(backgr); // this clear background each time  
 stroke(255);
 for (int i = 0; i < myRectangles.size(); i++)
   ((MyRect)myRectangles.get(i)).draw();
}  

void keyPressed() {
 if (key == '+') {
   backgr += 10;
 }
 if (backgr > 255) {
   backgr = 255;
 }

 if (key == '-') {
   backgr -= 10;
 }
 if (backgr < 0) {
   backgr = 0;
 }
}


(rectangles are drawn when user presses the mouse)
Re: change background color
Reply #8 - Jun 29th, 2008, 1:42am
 
having two draw functions is new to me too, i saw that before but couldnt figure out why. So why do you use two draw functions and how does processing handle them? how does it go from one to the other?
Re: change background color
Reply #9 - Jun 29th, 2008, 5:50am
 
The important thing to notice here is that the first void draw() ... is wrapped inside the class MyRect definition's braces {}.  The second void draw() is the special one that Processing calls when it needs to draw a frame on the canvas.  Note also that the second draw() contains a line of code that calls the first one:

((MyRect)myRectangles.get(i)).draw();

Processing doesn't actually "distinguish" between the two -- it actually only knows about the special one because of where it's defined in the file.  Processing doesn't even "see" the draw() inside MyRect until it is explicitly called by that line in the special draw() method.

Another way of putting this is that MyRect's draw() could have been called drAwMe() instead (and the line that calls it updated to say ...drAwMe()) and everything would still work.  However, if the other draw() were changed to drAwMe(), Processing would not see the special draw() method and it wouldn't know to start the animation loop that calls it.

Similar to draw(), the definitions for setup() and keyPressed() are special Processing "hooks" that get invoked by Processing at the right times.

I hope this helps clarify.  If I've confused things further, try again and I'll attempt to undo the damage.  :-)
Re: change background color
Reply #10 - Jun 29th, 2008, 10:35am
 
great explaination.
the point with calling it drAwMe() explained it very well..
many thanks to you!
Re: change background color
Reply #11 - Jun 29th, 2008, 11:14am
 
yep thank you! i guess i was confused that it was called draw() too... but now it really makes sense. Thanks again
Re: change background color
Reply #12 - Jun 29th, 2008, 5:20pm
 
Personally, I kept the habit, from C/C++ times, to start my function names with a capital. So I would have called it Draw(), lifting the ambiguity...
For this example I followed the Java convention, casting  confusion I fear...
Page Index Toggle Pages: 1