/**
* Creates a new PGraphics object and sets it to the specified size.
*
* Note that you cannot change the renderer once outside of setup().
* In most cases, you can call size() to give it a new size,
* but you need to always ask for the same renderer, otherwise
* you're gonna run into trouble.
*
* The size() method should *only* be called from inside the setup() or
* draw() methods, so that it is properly run on the main animation thread.
* To change the size of a PApplet externally, use setSize(), which will
* update the component size, and queue a resize of the renderer as well.
*/
public void size(final int iwidth, final int iheight,
String irenderer, String ipath) {
// Run this from the EDT, just cuz it's AWT stuff (or maybe later Swing)
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Set the preferred size so that the layout managers can handle it
setPreferredSize(new Dimension(iwidth, iheight));
setSize(iwidth, iheight);
}
});
// ensure that this is an absolute path
if (ipath != null) ipath = savePath(ipath);
String currentRenderer = g.getClass().getName();
if (currentRenderer.equals(irenderer)) {
// Avoid infinite loop of throwing exception to reset renderer
resizeRenderer(iwidth, iheight);
//redraw(); // will only be called insize draw()
} else { // renderer is being changed
// otherwise ok to fall through and create renderer below
// the renderer is changing, so need to create a new object
g = makeGraphics(iwidth, iheight, irenderer, ipath, true);
width = iwidth;
height = iheight;
// fire resize event to make sure the applet is the proper size
// setSize(iwidth, iheight);
// this is the function that will run if the user does their own
// size() command inside setup, so set defaultSize to false.
defaultSize = false;
// throw an exception so that setup() is called again
// but with a properly sized render
// this is for opengl, which needs a valid, properly sized
// display before calling anything inside setup().
throw new RendererChangeException();
}
}