* Returns an image at the given size of the 3D <code>view</code>.
* This image is created with an off screen canvas.
* @throws IllegalRenderingStateException if the image couldn't be created.
*/
public BufferedImage getOffScreenImage(View view, int width, int height) {
Canvas3D offScreenCanvas = null;
RenderingErrorObserver previousRenderingErrorObserver = getRenderingErrorObserver();
try {
// Replace current rendering error observer by a listener that counts down
// a latch to check further if a rendering error happened during off screen rendering
// (rendering error listener is called from a notification thread)
final CountDownLatch latch = new CountDownLatch(1);
setRenderingErrorObserver(new RenderingErrorObserver() {
public void errorOccured(int errorCode, String errorMessage) {
latch.countDown();
}
});
// Create an off screen canvas and bind it to view
offScreenCanvas = getOffScreenCanvas3D(width, height);
view.addCanvas3D(offScreenCanvas);
// Render off screen canvas
offScreenCanvas.renderOffScreenBuffer();
offScreenCanvas.waitForOffScreenRendering();
// If latch count becomes equal to 0 during the past instructions or in the coming 10 milliseconds,
// this means that a rendering error happened
if (latch.await(10, TimeUnit.MILLISECONDS)) {
throw new IllegalRenderingStateException("Off screen rendering unavailable");
}
return offScreenCanvas.getOffScreenBuffer().getImage();
} catch (InterruptedException ex) {
IllegalRenderingStateException ex2 =
new IllegalRenderingStateException("Off screen rendering interrupted");
ex2.initCause(ex);
throw ex2;
} finally {
if (offScreenCanvas != null) {
view.removeCanvas3D(offScreenCanvas);
try {
// Free off screen buffer and context
offScreenCanvas.setOffScreenBuffer(null);
} catch (NullPointerException ex) {
// Java 3D 1.3 may throw an exception
}
}
// Reset previous rendering error listener