views:

536

answers:

3

How to save drawing in JComponent into tiff format? I only know how to save the whole Java file but I dont know how to save specific Jcomponent. Help me :-(

EDITED: Thanks guys, now I am able to save my drawing to Jpeg.

However I just wanted to save one of the component? The c.paintAll(bufferedImage.getGraphics()); seem to save the whole component. However, I just want to save this component c.add(new PaintSurface(), BorderLayout.CENTER); without panel.add(saveBtn); How can I do that? Thanks.

Container c = getContentPane();
c.setLayout(new BorderLayout());      
Panel panel = new Panel();
panel.add(saveBtn);
c.add("South", panel);
c.setBackground(Color.WHITE);
c.add(new PaintSurface(), BorderLayout.CENTER);
A: 

you can get a Buffered Image of the component, or the panel that contains the drawing by creating a buffered image the size of the panel. You can then paint the panels contents onto the buffered image. You can then use JAI(Java Advanced Imaging) library to save the buffered image as a tiff. You'll have to check the docs on that here.

JComponent component;  //this is your already created component
BufferedImage image = new BufferedImage(component.getWidth(),
                                        component.getHeight(),
                                        Bufferedimage.TYPERGB)

Graphics g = image.getGraphics;
component.paintComponent(g);

The syntax may be slightly off, i'm not at an idea, but that is the general idea. You can then use JAI and convert the buffered image to a TIFF.

broschb
A: 

This is essentially identical to broschb's solution only using correct syntax and actually calling the appropriate JAI routines.

public void saveComponentAsTiff(Component c, String filename, boolean subcomp) throws IOException {
    saveComponentTiff(c, "tiff", filename, subcomp);
}

public void saveComponent(Component c, String format, String filename, boolean subcomp) throws IOException {
    // Create a renderable image with the same width and height as the component
    BufferedImage image = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);

    if(subcomp) {
     // Render the component and all its sub components
     c.paintAll(image.getGraphics());
    }
    else {
     // Render the component and ignoring its sub components
     c.paint(image.getGraphics());
    }

    // Save the image out to file
    ImageIO.write(image, format, new File(filename));
}

Documentation for the various functions can be found here:

If you want to save in a format other than tiff you can use ImageIO.getWriterFormatNames() to obtain a list of all image output formats currently loaded by the JRE.

UPDATE: If you are not interested in painting sub components you can substitute the call to Component.paintAll(...) with Component.paint(...). I have altered the example code to reflect this. Setting subcomp to true with render the subcompnents and setting it to false will ignore them.

Kevin Loney
:-) thanks Kevin
Jessy
A: 

The ScreenImage class allows you to save an image of any component.

camickr