Thursday, 1 January 2009

PApplet Ready Setup



Woah Boy! Is this image animating without the aid of a Java VM?

I noticed I had a slight problem when exporting (file/export) my processing code to run as a PApplet. I wanted to have one code base to run as an application and a PApplet.

When running as an applet I wanted the canvas dimensions to come from the parameters 'width' and 'height' found in the HTML source. The following hack (catching exceptions) worked out nicely.

View Petal Demo @ http://www.javaimage.co.uk/flower.html





// Example Setup
string mode ;
float CX,CY ;
int displayHeight,displayWidth ;
void setup()
{

mode = JAVA2D ;

try {
displayHeight =
Integer.parseInt(getParameter("height")) ;

displayWidth =
Integer.parseInt(getParameter("width")) ;

} catch (Exception e){

displayHeight = 3*screen.height/4 ;
displayWidth = 3*screen.width/4 ;
}

CX = displayWidth / 2 ;
CY = displayHeight / 2 ;

size(displayWidth, displayHeight,mode);
loop() ;

}

No comments: