Solved

save graphics 2D objects in jpg/gif format

Posted on 2003-12-10
7
2,585 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 48
stringclean challenge 26 58
Systems talking to each other 5 110
Core Java. What output will be and why ? 6 58
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…
It’s easy to embed any of your public Prezi presentations on your website or social network to share with others. Learn how simple it is in this tutorial.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now