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 › turning rect on / off
Page Index Toggle Pages: 1
turning rect on / off (Read 472 times)
turning rect on / off
Jun 20th, 2008, 11:26pm
 
guys, i'm feeling really ashamed, asking such a stupid question, but I'm getting crazy on this problem.

i have this script:

for (int i = 0; i< width; i+=20) {
for (int j = 0; j< height; j+=20) {

rect (i,j,2,2);
}
}

as you can see i get sort of a grid when displaying that. when pressing 'k' i want it to appear, when pressing 'k' once again i want it to disappear....
sounds easy, but i don't know how to realize it.

in c++ i know a function named VISIBLE. (visible.true and visible.false). is there any equivalent in processing?

hope for your tips once again..sorry, for beeing newby.
Re: turning rect on / off
Reply #1 - Jun 20th, 2008, 11:36pm
 
Code:
boolean kPressed;

//...

void draw()
{
background(0);
if(!kPressed)
{
for (int i = 0; i< width; i+=20) {
for (int j = 0; j< height; j+=20) {
rect (i,j,2,2);
}
}
}
}

void keyReleased()
{
if(key=='k')
kPressed=!kPressed;
}
Re: turning rect on / off
Reply #2 - Jun 20th, 2008, 11:39pm
 
awesome....Smiley
thank you thank you thank you Smiley
it works perfectly well - exactly the way i want it.
Re: turning rect on / off
Reply #3 - Jun 21st, 2008, 3:26pm
 
on little question again:

is there a way to display the grid without deleting what i have done in the background so far. i want the grid as a structure one can display or not, but it needs to be layed over the rest of the screen. not deleting it.

i tried to give transparency to the background but that does not work.
Re: turning rect on / off
Reply #4 - Jun 21st, 2008, 7:08pm
 
Just omit the background() instruction? Or move it to setup().
If you want to overlay the grid then hide it, it is harder, unless you memorize all the operations the user has done, or you take a snapshot of the current image (get()), forbid doing something will the grid is on, then restore the image to hide the grid.
Actually, you can still let drawing while the grid is on, but you have to draw both on visible display and in the hidden copy (which has no grid).
Re: turning rect on / off
Reply #5 - Jun 21st, 2008, 8:16pm
 
A quick illustration of my explanation:
Code:
static final int GRID_SIZE = 20;
boolean bShowingGrid;
PGraphics drawing;

void setup()
{
size(800, 800);
background(#3388FF);
drawing = createGraphics(width, height, JAVA2D);
drawing.beginDraw();
drawing.background(#3388FF);
drawing.endDraw();
}

void draw()
{
if (mousePressed)
{
stroke(#FFFF00);
strokeWeight(4);
line(mouseX, mouseY, pmouseX, pmouseY);
drawing.beginDraw();
drawing.stroke(#FFFF00);
drawing.strokeWeight(4);
drawing.line(mouseX, mouseY, pmouseX, pmouseY);
drawing.endDraw();
}
}

void DrawGrid()
{
stroke(#005588);
strokeWeight(1);
for (int i = 0; i < width; i += GRID_SIZE)
{
line(i, 0, i, height);
line(0, i, width, i);
}
}

void keyReleased()
{
if (key == 'g')
{
bShowingGrid = !bShowingGrid;
if (bShowingGrid)
{
DrawGrid();
}
else
{
image(drawing, 0, 0);
}
}
}

It is primitive, but it might be a starting point.
Re: turning rect on / off
Reply #6 - Jun 23rd, 2008, 10:40am
 
dear PhilLho,

thanks for giving a "starting point". it really helped me.
i tried to put your script together with my script and now get some trouble while running.

actually my script has to do:

- drawing rects (20x20) when pressing a.
- by clicking one sets the starting point.
- when pressing Return the same sort of rect - line has to appear right under the previous line of rects.
- when clicking to another point, it has to start again.
- when pressing TAB the grid (your grid Wink appears.

my problem:

when pressing TAB the grid does not appear in the upper left corner (the inital defaut 0,0), but at the position of the mouse. which is  no surprise, as i set everything at the position of mouseX, mouseY. But I just want my rectangles to appear at the position of the mouse. the grid has to be always at (0,0) (to give the user a sort of orientation where to set the rectangles).

i hope you know a way to get out of this Smiley
thanks a lot.

static final int GRID_SIZE = 50;
boolean bShowingGrid;
PGraphics drawing;
int breite = 40;
int hoehe = 60;
int wertX, wertY;
int x1, x2 = 40;
int count;


void setup() {


 size (500,500);

 background (0);

 drawing = createGraphics (width, height, JAVA2D);
 drawing.beginDraw();
 drawing.background (0);
 drawing.endDraw();

}
void draw() {
}

void mouseClicked() {
wertX = 0;
 wertY = 0;
 count = 0;

}


void keyReleased() {

translate (mouseX,mouseY);

 x1 = breite/4;
 x2 = hoehe/4;
 wertX += x1;
 wertY -= x2/5;
 tastatur (wertX, wertY);

}

void DrawGrid()
{
 stroke(#005588);
 strokeWeight(1);
 for (int i = 0; i < width; i += GRID_SIZE)
 {
   line(i, 0, i, height);
   line(0, i, width, i);
 }
}

void tastatur (float typeX, float typeY) {
 switch (key) {

 case ENTER:
 count ++;
 
   wertX =  -count*x1/3;
   wertY = count*x2;
   break;

 case 'a':
   rect (typeX,typeY,20,20);
   drawing.beginDraw();
   drawing.rect (typeX, typeY, 20,20);
   drawing.endDraw();
   break;

 case TAB:
   {
     bShowingGrid = !bShowingGrid;
     if (bShowingGrid)
     {
       DrawGrid();
     }
     else
     {
       image(drawing, 0, 0);

     }
   }
   break;
 }
}

Re: turning rect on / off
Reply #7 - Jun 24th, 2008, 7:01am
 
Mmm, I am not sure I have understood everything, and I have no time right now to test your code, but the solution seems to remove the translate() call.
You can just add the mouse coordinates to the drawing calls:
   rect (typeX+mouseX,typeY+mouseY,20,20);
   drawing.beginDraw();
   drawing.rect (typeX+mouseX, typeY+mouseY, 20,20);
   drawing.endDraw();

And use a constant instead of 20, so you can easily change this setting if you feel it needs adjustments! Wink (and it autodocument the code).
Page Index Toggle Pages: 1