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 › save and display shape
Page Index Toggle Pages: 1
save and display shape? (Read 592 times)
save and display shape?
Oct 30th, 2009, 4:31am
 
Hi everyone,

I don't think this should be difficult, but I can't get it to work. I want do draw Bezier shapes like this:

Code:

   stroke(#FFFFFF,100);
   noFill();
   beginShape(POLYGON);
   for (int i=0; i<x2s.length-1; i++)
   {
   vertex(x2s[i], y2s[i]);
   bezierVertex(x1, y1, x1, y1, x2s[i+1], y2s[i+1]);
   }
   vertex(x2s[x2s.length-1], y2s[x2s.length-1]);
   bezierVertex(x1, y1, x1, y1, x2s[0], y2s[0]);
   endShape(CLOSE);


Now, that works. But because I have many such shapes that I would like to use over and over again, I tried to create a function like this one:

Code:

PGraphics2D create_cell_shape(int x1, int y1, int[] x2s, int[] y2s, color bg, color st) {
smooth();
stroke(#AAAAAA);
fill(#FFFFFF);

PGraphics2D cell = new PGraphics2D();
cell.beginDraw();

   cell.beginShape(POLYGON);
   for (int i=0; i<x2s.length-1; i++)
   {
   cell.vertex(x2s[i], y2s[i]);
   cell.bezierVertex(x1, y1, x1, y1, x2s[i+1], y2s[i+1]);
   }
   cell.vertex(x2s[x2s.length-1], y2s[x2s.length-1]);
   cell.bezierVertex(x1, y1, x1, y1, x2s[0], y2s[0]);
   cell.endShape(CLOSE);

cell.endDraw();
image(cell);

   return cell;
}


I played around a little with the type of object that 'cell' is, so at least it compiles now, but I can't get it do display in the draw() function, as in shape() or image() and so forth.

Any ideas about this would be very much appreciated. Thanks!
Re: save and display shape?
Reply #1 - Oct 30th, 2009, 5:52am
 
Perhaps something like:
Code:
PGraphics2D create_cell_shape(int cellWidth, int cellHeight, int x1, int y1, int[] x2s, int[] y2s, color bg, color st) {
PGraphics2D cell = createGraphics(cellWidth, cellHeight, JAVA2D);
cell.beginDraw();

cell.smooth();
cell.stroke(#AAAAAA);
cell.fill(#FFFFFF);

cell.beginShape(POLYGON);
for (int i=0; i<x2s.length-1; i++)
{
cell.vertex(x2s[i], y2s[i]);
cell.bezierVertex(x1, y1, x1, y1, x2s[i+1], y2s[i+1]);
}
cell.vertex(x2s[x2s.length-1], y2s[x2s.length-1]);
cell.bezierVertex(x1, y1, x1, y1, x2s[0], y2s[0]);
cell.endShape(CLOSE);

cell.endDraw();
// image(cell);

return cell;
}
Re: save and display shape?
Reply #2 - Oct 30th, 2009, 7:21am
 
Yes, indeed! Using PGraphics actually, not PGraphics2D. Thanks!  Smiley
Page Index Toggle Pages: 1