1

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();
}
}
Community
  • 1
  • 1
  • See also [`ComponentImageCapture`](http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of-jframe/5853992#5853992). – Andrew Thompson Jan 11 '13 at 06:59
  • You're welcome. Note the 'signed' comment by Nikolay (and my clarification in comment). While the applet appears in a frame it will not have a `SecurityManager`, but it certainly will when it is embedded in a web page. – Andrew Thompson Jan 11 '13 at 07:12

2 Answers2

1

First, you can create a BufferedImage instance like this:

    private BufferedImage image = new BufferedImage(600, 800, BufferedImage.TYPE_INT_RGB);

Then you can get the Graphics2d object from it:

    Graphics2D g2d = image.createGraphics();

Then, you can use the ImageIO API to save the image.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Swapnil
  • 8,201
  • 4
  • 38
  • 57
1
  1. JFrame and main are not supposed to be used in an applet, unless you try to create hybrid application.

  2. Don't override paint() method

  3. Use SwingUtilities.invokeLater() to create GUI

  4. Applet is not allowed to write/read from external files, unless it is signed

  5. ImageIO.write() can write an image to file

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
  • 1
    1. *"..unless you try to create hybrid application."* That is another way of showing an applet in a frame. OTOH I wondered why it was in the example. 2. That could use expanding. Note that a `JApplet` has no `paintComponent(Graphics)` method. 3. That's good. 4. *"..unless it is signed"* ..or uses the JNLP API services ([demo.](http://pscode.org/jws/api.html)). 5. That's good. +1 – Andrew Thompson Jan 11 '13 at 07:05
  • @AndrewThompson Can I just save the graphics into an image. instead of loading it into applet. Please let me know. Any sample code links. (That would be great.) – user1969103 Jan 11 '13 at 07:12
  • *"instead of loading it into applet."* The answer is probably yes, but I am a little unclear on why the applet is there at all. Note that I could achieve this in a command line app. (no GUI). Here is an [example of painting directly to an image](http://stackoverflow.com/questions/13440201/how-to-resize-text-in-java/13440543#13440543). – Andrew Thompson Jan 11 '13 at 07:15