ServletOutputStream out = res.getOutputStream();
MainFrameModified frame = null;
Graphics g = null;
Applet applet = null;
try {
// Load the SecondApplet
// Must be in the standard CLASSPATH
try {
applet = (Applet) Class.forName(APPLETNAME).newInstance();
}
catch (Exception e) {
throw new ServletException("Could not load applet:" + e);
}
// Prepare the applet arguments
String args[] = new String[1];
args[0] = "barebones=true"; // run without a menu bar
// Put the applet in its frame
// addNotify() is called by MainFrameModified
frame = new MainFrameModified(applet, args, WIDTH, HEIGHT);
// Get a graphics region to match the applet size, using the Frame
Image image = frame.createImage(WIDTH, HEIGHT);
g = image.getGraphics();
// Ask the applet to paint itself
applet.validate();
applet.paint(g);
// Encode and return what it painted
res.setContentType("image/gif");
GifEncoder encoder = new GifEncoder(image, out);
encoder.encode();
}
finally {
// Clean up resources
if (applet != null) {
applet.stop();
applet.destroy();
applet.removeAll();
}
if (g != null) {
g.dispose();
}
if (frame != null) {
frame.removeAll();
frame.removeNotify();
frame.dispose();
}
}