Link to home
Start Free TrialLog in
Avatar of manish14
manish14

asked on

save graphics 2D objects in jpg/gif format

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
Avatar of girionis
girionis
Flag of Greece image

 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
Avatar of manish14
manish14

ASKER

I am using java 1.3
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.
now i started using java1.4
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
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);
    }
ASKER CERTIFIED SOLUTION
Avatar of PashaMod
PashaMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial