How can I capture the image of an AWT GUI?
Created May 4, 2012
Scott Stanchfield
You can use the printAll() method of Component to accomplish this.
As an example
Component comp = ... ; // the top-level component // you want to capture // create an image for the capture Image img = comp.createImage(comp.getWidth(), comp.getHeight()); // grab a graphics context for that image Graphics g = img.getGraphics(); // print the GUI into that image comp.printAll(g);
The resulting Image contains a picture of the GUI. Note that if the GUI is not displayes, you either need to display it first or call addNotify() on it to create the necessary peer components.