Solved

save graphics 2D objects in jpg/gif format

Posted on 2003-12-10
7
2,613 Views
Last Modified: 2013-11-19
Hello
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
0
Comment
Question by:manish14
[X]
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
7 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 9912246
 Which JDk are you using? If you are using 1.4 and above take a look here: http://javaalmanac.com/egs/javax.imageio/Graphic2File.html?l=find
0
 

Author Comment

by:manish14
ID: 9955752
I am using java 1.3
0
 

Author Comment

by:manish14
ID: 9957215
Hello
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();
         g2d.setColor(Color.white);
            //d = bufferedImage.createGraphics();
       }
    // ..
        // Create a graphics contents on the buffered image
       
        // Draw graphics

        return bufferedImage;
    }

Thx alot for your comments.
0
Independent Software Vendors: 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!

 

Author Comment

by:manish14
ID: 9957231
now i started using java1.4
0
 

Author Comment

by:manish14
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();
       paint(g2);
       g2.dispose();
       ImageIO.write(image, "jpeg", out);
    }
like this you can create in any format either jpeg,png,gif

thx for your comments
0
 

Author Comment

by:manish14
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();
       paint(g2);
       g2.dispose();
       ImageIO.write(image, "jpeg", out);
    }
0
 

Accepted Solution

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

PashaMod
Community Support Moderator
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…
Learn how to use the remote presentation tool in Prezi to allow you to harness the power of this cloud based presentation platform. You can show your presentation with a remote audience using this free tool.
Suggested Courses

632 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