No modifications need to be made for this page as all the functionality is performed in the Java code. However, if you wish to have an element on the web page trigger the save action, then you can use the techniques learnt in the previous tutorial for this.
Most of the work in capturing a screen is performed within the browser. The result is returned to your user code as an AWT BufferedImage, which you can then do with as you wish. Note that saving an image to disk requires the highest level of security permissions that an applet can have, and many users may not like your code doing this.
Capturing an image of the screen requires two parts - implementing the
Xj3DScreenCaptureListener interface, and instructing the
browser on how many frames to capture. Once you have the capture image, you
are free to do with is as you wish.
screenCaptures(BufferedImage img).
In addition, we're going to add a Swing JButton to the UI so
that we have a way of telling the browser when to capture that screenshot.
The button will, of course, require you to implement the
ActionListener interface as well. In the
actionPerformed() method, we tell the browser how many frames
that we want to capture using the captureFrames() method. In this
case we just want to grab the next frame, so pass in a value of 1.
The final piece of new code is contained in the screenCaptured()
method. Once you have the BufferedImage, you can do whatever you like with
it. In this example, we'll use Java's ImageIO class to write it out to a
file name that you have selected. This code leaves out all the security
checks and exception handling that real code should have in it.
public class Xj3DAppletTutorial6 extends Applet
implements ActionListener, Xj3DScreenCaptureListener {
Xj3DBrowser browser;
public void init() {
setLayout(new BorderLayout());
browser = (Xj3DBrowser) getBrowser();
browser.setScreenCaptureListener(this);
JButton b = new JButton("Capture Now");
b.addActionListener(this);
add(b, BorderLayout.SOUTH);
loadScene();
}
public boolean actionPerformed(ActionEvent evt) {
browser.captureFrames(1);
}
public boolean screenCaptured(BufferedImage img) {
JFileChooser chooser = new JFileChooser();
if(chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
return;
File out_file = chooser.getSelectedFile();
try {
ImageIO.write(img, "PNG", out_file);
} catch (IOException e) {
e.printStackTrace();
}
}
No X3D modifications are needed for this tutorial. In fact, it doesn't matter what X3D file you use for the content here - be it simple or complex.
|
[
Xj3D Homepage |
Xj3D @ Web3d |
Screenshots |
Dev docs |
Dev Releases |
Contributors |
Getting Started
]
Last updated: $Date: 2008-09-09 17:05:05 $ |