Solved

save graphics 2D objects in jpg/gif format

Posted on 2003-12-10
7
2,602 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

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!

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 39
Java: anonymous class 4 37
servlet and mdb, jms error 1 54
spring maven example issues 3 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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.

713 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