Hi,

There are a few general steps to saving a component's image as an image
file. Oversimplified, they are:
1) Create a BufferedImage of the same dimensions as the Component in
question.
2) Get the Graphics context of the Component and paint it to the
BufferedImage's Graphics context.
3) Encode the image to be saved in the appropriate format.
Try the following, which makes use of the undocumented (but bundled with
some JDK versions) com.sun.image.codec.jpg package. The
createComponentImage() method encapsulates steps 1 and 2, above. The
encodeImage() method encapsulates step 3. The rest is set-up and display. If
all works according to plan, you should have a new .jpg file called
ImageTest.jpg whose pixels mirror (though I've noticed some color loss)
those in the Canvas:

import com.sun.image.codec.jpeg.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;

class ImageUtils {
    private static JPEGImageEncoder encoder = null;
    private static FileOutputStream fileStream = null;
    public static BufferedImage createComponentImage(Component component) {
        BufferedImage image =
(BufferedImage)component.createImage(component.getWidth(),component.getHeigh
t());
        Graphics graphics = image.getGraphics();
        if(graphics != null) {
            component.paintAll(graphics);
        }
        return image;
    }
   public static void encodeImage(BufferedImage image, File file) throws
IOException {
        fileStream = new FileOutputStream(file);
        JPEGEncodeParam encodeParam =
JPEGCodec.getDefaultJPEGEncodeParam(image);
        encoder = JPEGCodec.createJPEGEncoder(fileStream);
        encoder.encode(image,encodeParam);
    }
}


class LineCanvas extends Canvas {
    public LineCanvas() {
        setSize(250,250);
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0,0,getSize().width, getSize().height);
        g.setColor(Color.green);
        g.drawArc(100,100,50,50,0,100);
        g.drawArc(150,150,50,50,90,100);
    }
}

public class ImageMain {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Canvas imageCanvas = new LineCanvas();
        frame.add(imageCanvas);
        frame.setSize(300,300);
        frame.setVisible(true);
        try {
            File file = new File("ImageTest.jpg");
            BufferedImage image =
ImageUtils.createComponentImage(imageCanvas);
            ImageUtils.encodeImage(image,file);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
----- Original Message -----
From: "Ken Burns" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, September 18, 2001 2:54 AM
Subject: [JAVA2D] Writing to a GIF/JPEG


> I have a component (derived from JComponent) that I'd like to write to a
> JPEG or GIF file.
>
> Does anyone know how or if this can be done?
>
> Thanks,
>
> Ken Burns
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff JAVA2D-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to