Loading...
Logo
Processing Forum
The reference says:  Use this class if you need to draw into an off-screen graphics buffer. If something is off-screen, does that mean that it's not on the screen, therefor why bother? Even the examples don't do any justice. Any creategraphics examples can be easily reproduced without using the createGraphics class. So what is it good for? And what's an off-screen buffer? Can you give me an example? 

Replies(3)

I use off-screen buffers for color picking.
Here is a description off the wiki.

Processing offers the use of an off-screen buffer, which can be very useful for creating masking effects. In this hack I outline the basics of writing and reading pixels from an off-screen buffer and suggest some practical ways of applying this technique.

Processing offers the createGraphics method to create an off-screen graphics buffer. This buffer is in most ways the same as the buffer used to draw on-screen images, but it is different in that when you draw to it you don't actually see anything on the screen. This can be very handy for many reasons. For example, drawing to an off-screen buffer can save CPU cycles by letting you draw pixels using Processing drawing routines then save those pixels to a PImage, and draw the PImage to the screen instead of re-drawing the image each frame. Another use of off-screen buffers is the ability to mask areas of an image drawn in Processing to give the effect of a 'viewport' or scrollable area.



Your drawings go to a buffer, ie. a storage of the pixel information. Actually, you always draw to a graphic buffer, but by default, Processing shows the content of the buffer on each frame.
When you draw off-screen, indeed you won't see the result, but you can use it: as a mask, for example, as said, or just by drawing this off-screen image on the screen (using image() for example). It can be used, for example, to draw some information and paste it on a place of the screen.
You can even have a buffer that is never displayed: it is often used to detect collision, by drawing two complex objects in monochrome with transparency, and inspecting the pixels to see if there is a superposition.

Last advice: search the examples coming with Processing* and search the forums (use the search box in the Processing site) for createGraphics, you will find significant examples.

* I see 8 examples using it. You can search with find + grep if you are on Unix/Mac, or with the Windows search or a free tool like BareGrep.