Possible Duplicate:
Convert a Graphics2D to an Image or BufferedImage
Sorry. This is a very basic question. But I am very new to this platform. I am drawing a line using graphics2d draw function. I want to save this as an image into my system. I googled it out and found out that BufferedImage
class is useful in doing this and then use imagedraw function.
But I could not figure out how exactly use it in my code. Can any one please help me?
Here is my code.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class DrawAndSaveImage extends JApplet {
public void init() {
setBackground(Color.lightGray);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(Color.black);
g2d.draw(new Line2D.Double(50,150,150,150 ));
}
public static void main(String s[]) {
JFrame frame = new JFrame("sample image");
JApplet applet = new DrawAndSaveImage();
frame.getContentPane().add("Center", applet);
applet.init();
frame.setSize(200, 200);
frame.show();
}
}