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 & HelpPrograms › resizable draw area in a resizable window
Page Index Toggle Pages: 1
resizable draw area in a resizable window (Read 1377 times)
resizable draw area in a resizable window
Apr 13th, 2010, 5:34am
 
PGraphics drawingArea;
int DA_X = 0, DA_Y = 200;
int lastWidth, lastHeight;
int DA_WIDTH, DA_HEIGHT;

       
void setup(){  
 size(1000, 800);  
 frame.setResizable(true);
 registerPre(this);
 smooth();  
 background(0);  // Create a new graphical context  
drawingArea = createGraphics(DA_WIDTH, DA_HEIGHT, P3D);  
drawingArea.beginDraw();  
drawingArea.background(#FFFFFF);
drawingArea.endDraw();
lastWidth = width;  
lastHeight = height;
}

void draw(){  // Update display  
image(drawingArea, DA_X, DA_Y);

println(lastWidth);
println(lastHeight);
}


void mousePressed(){  
 if (mouseX < DA_X || mouseX >= DA_X + DA_WIDTH || mouseY < DA_Y || mouseY >= DA_Y + DA_HEIGHT)    
 return;  drawingArea.beginDraw();  for (int i = 0; i < 10; i++)
{    drawingArea.ellipse(random(DA_WIDTH), random(DA_HEIGHT), 5 + random(20), 5 + random(20));  }  
drawingArea.endDraw();
}

void pre(){  
if(width != lastWidth || height != lastHeight){      
 lastWidth = width;  
 lastHeight = height;
DA_WIDTH=lastWidth;
DA_HEIGHT=lastHeight-DA_Y;

}
}


When I give DA_WIDTH and DA_HEIGHT a number it works. But when I want to make it dependent of the framesize, it doesn't work anymore..

why?
Re: resizable draw area in a resizable window
Reply #1 - Apr 13th, 2010, 6:20am
 
I think you should re-create drawingArea in pre().
Re: resizable draw area in a resizable window
Reply #2 - Apr 14th, 2010, 5:21am
 
you mean, that I should copy this part into the pre?
there's the same result, then..
Re: resizable draw area in a resizable window
Reply #3 - Apr 14th, 2010, 6:38am
 
Actually, I am not sure to understand your usage of pre().
Re: resizable draw area in a resizable window
Reply #4 - Apr 14th, 2010, 2:00pm
 
when I'm honest I'm not, too.. not yet.. Everything that is in this program I haven't programmed on my own to be honest...

But here someone has told me to do it like this.. http://processing.org/discourse/yabb2/num_1270214846.html

Re: resizable draw area in a resizable window
Reply #5 - Apr 15th, 2010, 2:06am
 
OK, I understand better, with context.
The following works:
Code:
PGraphics drawingArea;
int DA_X = 0, DA_Y = 200;
int lastWidth, lastHeight;
int DA_WIDTH, DA_HEIGHT;


void setup(){  
size(500, 500);  
frame.setResizable(true);
registerPre(this);
smooth();  
background(0);
lastWidth = width;  
lastHeight = height;
// Create a new graphical context  
ReCreateDrawingArea(lastWidth, lastHeight);
}

void draw(){  // Update display  
 image(drawingArea, DA_X, DA_Y);
}


void mousePressed(){  
if (mouseX < DA_X || mouseX >= DA_X + DA_WIDTH || mouseY < DA_Y || mouseY >= DA_Y + DA_HEIGHT)    
 return;  
drawingArea.beginDraw();  
for (int i = 0; i < 10; i++)
{
  drawingArea.ellipse(random(DA_WIDTH), random(DA_HEIGHT), 5 + random(20), 5 + random(20));  
}  
drawingArea.endDraw();
}

void pre(){  
 if(width != lastWidth || height != lastHeight){
   lastWidth = width;  
   lastHeight = height;
   ReCreateDrawingArea(lastWidth, lastHeight);
 }
}

void ReCreateDrawingArea(int daWidth, int daHeight)
{
  DA_WIDTH = daWidth;
  DA_HEIGHT = daHeight - DA_Y;

drawingArea = createGraphics(DA_WIDTH, DA_HEIGHT, P3D);  
drawingArea.beginDraw();  
drawingArea.background(#FFFFFF);
drawingArea.endDraw();
}

pre() isn't magic, it is a method always called just before draw().
Re: resizable draw area in a resizable window
Reply #6 - Apr 16th, 2010, 4:50am
 
wow. Thank you very very much for your work.. that's almost perfect.. only: if I maximize the window, the background is set white. I want the things that I have drawn to stay when I change the size of the window..

but maybe I'll work it out on my own.
Re: resizable draw area in a resizable window
Reply #7 - Apr 16th, 2010, 7:20am
 
Keep a reference on the previous drawing area, and draw it on the new one. Something like:
Code:
PGraphics prevDA = drawingArea;
drawingArea = createGraphics(DA_WIDTH, DA_HEIGHT, P3D);
drawingArea.beginDraw();
drawingArea.background(#FFFFFF);
drawingArea.image(prevDA);
drawingArea.endDraw();

(untested)
Page Index Toggle Pages: 1