0

I need to save a chessboard as a jpeg file. I know how to save a java component into JPEG. But my problem is that I use JButtons as cells and when I try to save the whole chessboard, only the panel is saved. Does anybody know how to save a JComponent WITH ITS CHILD COMPNENTS into JPEG (png etc.) Thank you in advance.

Max
  • 75
  • 1
  • 10

3 Answers3

2

You can print a Component (such as JPanel) to any Graphics object. So, why not use the Graphics object of a BufferedImage and write it to disk.

void takePicture(JPanel panel) {
    BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
    panel.print(img.getGraphics()); // or: panel.printAll(...);
    try {
        ImageIO.write(img, "jpg", new File("panel.jpg"));
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

This makes this JPanel

    JPanel panel = new JPanel();
    panel.add(new JButton("Hello"));
    panel.add(new JButton("World"));

Look like this:

enter image description here

updated based on comments, many thanks to @MadProgrammer

Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45
  • 1
    Using `paint` has issues if the component is not already displayable. It also uses double buffering which can cause issues with the rendering process. It would normally be better to use `print` or `printAll` in this case...I might also be better to use the components preferred size instead of it's actual size, depending on when the component is begin painted, just saying ;) – MadProgrammer Sep 14 '13 at 21:43
  • 1
    Actually, it doesn't (`paint` vs `print`) painting and un-displayed component can cause exceptions under some systems because `paint` expects to be attached to a native peer ;) – MadProgrammer Sep 15 '13 at 07:50
  • Also, because of the way the double buffering works on some systems, it may not even produce a result...so no, I would say it doesn't always do what the OP wants ;) – MadProgrammer Sep 15 '13 at 07:58
  • 1
    I've had issues with this approach on Windows 7 and Mac OS. Regardless, `print` and `printAll` are designed to do exactly what the OP is asking. Just because something works, doesn't make it right ;) – MadProgrammer Sep 15 '13 at 08:02
  • Will it work if I have a scroll on my panel? I mean that the invisible area must be saved as well. – Max Sep 15 '13 at 13:50
1

Check out Screen Image. It will allow you to create an image of any component in the frame. If you choose to make an image of a panel all the child components are included in the image.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Use Robot class to create a screen capture of your screen.

Use this tutorial for help: Screen Capture using Robot class

Now for your chessboard, clip the image by calling subImage method of BufferedImage class.

Here's the link on clipping BufferedImage

You can get Dimensions of you JPanel by calling getX(), getY(), getWidth() and getHeight() methods which you can pass into subImage method. Manipulate the passing value by adding/substracting some value to get desired results.

Community
  • 1
  • 1
anshrpr
  • 43
  • 8