save graphics 2D objects in jpg/gif format

Posted on 2003-12-10
Last Modified: 2013-11-19
I have one application there i build E-R digrams like objects,relationship means i am using java.awt.geom.RectangularShape,java.awt.geom.Rectangle2D and java.awt.Graphics2D so now i want to save my whole digram in jpg/gif/png or any other graphic format.
So can anyone help me to do this.

Thx a lot
Question by:manish14
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
LVL 35

Expert Comment

ID: 9912246
 Which JDk are you using? If you are using 1.4 and above take a look here:

Author Comment

ID: 9955752
I am using java 1.3

Author Comment

ID: 9957215
I tried your way it is saving a file with given name but it contains nothing just a black square.
I have some graphical symbols for that i am using interface  SymbolCreable.
And if i want to use createImage method then i am getting error cannot resolve symbol. I did import java.awt.*. So do u know y.

private static void saveJPEGSchema(String fileName, Draw d){
       // Create an image to save
      RenderedImage rendImage = myCreateImage(d);
        // Write generated image to a file
      try {
        // Save as PNG
        File file = new File(fileName);
        ImageIO.write(rendImage, "png", file);
        // Save as JPEG
        ImageIO.write(rendImage, "jpg", file);
      } catch (IOException e) {

    public static RenderedImage myCreateImage(Draw d) {
        int width = 1000;
        int height = 1000;
        // Create a buffered image in which to draw
        //BufferedImage bufferedImage ;
        Graphics2D g2d ;
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       if (bufferedImage  == null) {
          System.out.print("bufferedImage is  : "+bufferedImage);
         // bufferedImage  = (BufferedImage)createImage(width,height);
       if (bufferedImage  != null) {
         g2d = bufferedImage.createGraphics();
            //d = bufferedImage.createGraphics();
    // ..
        // Create a graphics contents on the buffered image
        // Draw graphics

        return bufferedImage;

Thx alot for your comments.
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.


Author Comment

ID: 9957231
now i started using java1.4

Author Comment

ID: 10104588
i solved my problem myself, so if you are interested in solution then
function will look like this

private static void saveJPEGSchema (Draw d, OutputStream out) throws IOException {
       //Create an image to save
       int w = getWidth());
       int h = getHeight());
       BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
       Graphics2D g2 = image.createGraphics();
       ImageIO.write(image, "jpeg", out);
like this you can create in any format either jpeg,png,gif

thx for your comments

Author Comment

ID: 10104598
sorry without draw, draw was in my application

private static void saveJPEGSchema (OutputStream out) throws IOException {
       //Create an image to save
       int w = getWidth());
       int h = getHeight());
       BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
       Graphics2D g2 = image.createGraphics();
       ImageIO.write(image, "jpeg", out);

Accepted Solution

PashaMod earned 0 total points
ID: 10106407
PAQed, with points refunded (100)

Community Support Moderator

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
CSV file parsing thru Java 13 54
how to debug htl and js pages 8 80
Find out all repos that a user is most active on Github 1 58
How to fix  socket closed error 11 64
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the fileā€¦
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question