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 › only draw object within region
Page Index Toggle Pages: 1
only draw object within region? (Read 295 times)
only draw object within region?
Nov 20th, 2008, 3:24am
 
hi everybody!

i've been working on creating a system of mini-windows embedded in larger ones.  each of these windows has its own scrollbar to move the contents in its own window.  how could i make it so that that certain objects are not visible if they are outside a certain region?  specifically i'm asking about border cases where only part of an object is visible.

the way i'm doing it now is with a combination of conditionals (such that the object is not drawn if it has moved beyond the region) and an invisible layer above that covers the object if it crosses the barrier (this takes care of the border condition until the object reaches the point where it is no longer drawn).  however, this is quite limited as it requires maintaining an order for each of the layers, and that the windows cannot be too close to each other (lest their invisible layers bump heads!).  it seems there is probably a better way to do this... ideas?
Re: only draw object within region?
Reply #1 - Nov 20th, 2008, 3:46pm
 
I am not sure I fully understood the problem, but maybe my code at frame around the applet / saving without a frame will give you some ideas.
Re: only draw object within region?
Reply #2 - Nov 22nd, 2008, 2:45am
 
hmm... this is promising.  it seems that the JAVA2D mode is the only way to get nice looking graphics, but everything gets choppy and slow.  P3D mode isn't choppy but the graphics don't look so hot.

how does the PGraphics class work?  is it basically like running another processing program on top?  maybe that's why it's choppy and slow in JAVA2D.
Re: only draw object within region?
Reply #3 - Nov 23rd, 2008, 2:42am
 
hmm, is there any way to embed pgraphics buffers inside each other?

for example... in the following code, i'd like to be able to embed pg1 inside pg, such that pg1 disappears when it crosses pg's border:

PGraphics pg;
PGraphics pg1;

int y = 0;

void setup()
{
 size(400, 400);
 smooth();
 
 pg = createGraphics(200, 200, JAVA2D);
 pg1 = createGraphics(50, 50, JAVA2D);
 pg.smooth();
 pg1.smooth();
}

void draw()
{
 stroke(255);
 background(40);
 
 pg.beginDraw();
 pg.translate(0, y/2);
 pg.background(90);
 
 pg1.beginDraw();
 pg1.background(160);
 
 for (int i = 0; i < 3; i++)
   pg.rect(30*(i+1), 100, 20, 15);
 
 pg1.fill(40, 20, 170);
 pg1.translate(0, y/4);
 pg1.rect(15, 20, 20, 15);
 
 pg.endDraw();
 image( pg, 100, 100);
 pg1.endDraw();
 translate(0, y/4);
 image( pg1, 230, 170);
 
 y--;
}
Re: only draw object within region?
Reply #4 - Nov 23rd, 2008, 2:13pm
 
Yes and not. Not in the way you intended to do, but in another way:
Code:
void draw()
{
 stroke(255);
 background(40);

pg1.beginDraw();
pg1.background(160);
pg1.fill(40, 20, 170);
pg1.translate(0, y/4);
pg1.rect(15, 20, 20, 15);
pg1.fill(#FF0000);
pg1.ellipse(15, 20, 5, 5);
pg1.endDraw();

pg.beginDraw();
pg.background(90);

pg.translate(0, y/2);
for (int i = 1; i <= 3; i++)
pg.rect(30*i, 100, 20, 15);

pg.translate(0, -y/4);
pg.image(pg1, 130, 70);

pg.endDraw();
image(pg, 100, 100);

y--;
}

To embed a graphics buffer inside another, just draw the first on the second, not on the final canvas. Processing will automatically do the clipping by pg's limits.
Page Index Toggle Pages: 1