Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

save graphics 2D objects in jpg/gif format

Posted on 2003-12-10
7
Medium Priority
?
2,623 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

885 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