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 › Example for offscreen drawing with fonts
Page Index Toggle Pages: 1
Example for offscreen drawing with fonts (Read 517 times)
Example for offscreen drawing with fonts
Jan 31st, 2007, 8:50am
 
No problem... just thought I'd share an example of offscreen drawing using PGraphics with fonts and copying between PGraphics instances since there were a few little tricks I had to figure out (all documented though).  If this isn't an appropriate place to post working examples just let me know.

http://www.mangtronix.com/p5/OffscreenFontTest/

Quote:


/**
* <p>
*   Demonstration of drawing text offscreen then copying it for onscreen display. <br>
*   Click to toggle drawing of offscreen buffer.
* </p>
*
* <p>
*   Michael Ang - http://www.michaelang.com <br>
*   Created 2007-01-31
* </p>
*/

PGraphics pg;
PFont pf;
boolean blitmeharder = true;

void setup() {
 size(350, 200);
 
 pf = loadFont("SWIsop1-48.vlw");
 textFont(pf, 48);
 
 pg = new PGraphicsJava2D(width, height/2, this);  
 pg.beginDraw(); // $$$ the begin/end is required for the textFont to work
 pg.textFont(pf, 48);
 pg.endDraw();
}

void draw() {
 background(0);
 
 text("Onscreen text", 10, 50);
 
 pg.beginDraw();
 pg.background(127);
 pg.text("Offscreen text", 10, 50);
 pg.endDraw();
 
 if (blitmeharder) {
   pg.loadPixels();
   copy(pg, 0, 0, width, height/2, 0, height/2, width, height/2);
 }
}

void mouseClicked() {
 blitmeharder = !blitmeharder;  
}

void keyPressed() {
 if (key == 's') {
   // Save screenshot to smaller buffer
   PGraphics buffer = new PGraphicsJava2D(width/2, height/2, this);
   
   this.g.loadPixels(); // needed to ensure fresh before copy
   buffer.copy(this.g, 0, 0, width, height, 0, 0, width/2, height/2);
   try {
     buffer.save("offscreentest.png");
     println("Saved screenshot");
   } catch (Exception e) {
     println("Couldn't save screenshot");
     e.printStackTrace();  
   }
 }
}


Page Index Toggle Pages: 1