Export image from JGoView?

Hi, is there any way to save an image of a JGoView (Swing)? In some work that I’m doing, my JGoView is scrollable, so the image needs to include everything on the JGoView (not just what is visible on the screen).

Your help is much appreciated.

Yes, you can use the JPEGView class in the Imager sample application to create a jpeg image of the diagram. Something like the following:

JPEGView jpgView = new JPEGView(jGoView1.getDocument());
jpgView.setViewPosition(0, 0);
jpgView.setViewSize(jpgView.getDocument().getDocumentSize());
try {
OutputStream outputStream = new FileOutputStream(
"c:\\temp\\image.jpg");
jpgView.render(outputStream);
outputStream.close();
}
catch (Exception exc) { }
Note that the above code expands the size of the JPEGView to contain the entire diagram, however the size of the created image will also change depending on the size of the diagram. You may want to call JPEGView setScale as well as JGPEGView setViewSize to create an image of a smaller size with the diagram scaled to fit.

Thanks, that works nicely. However, at times I am exporting very large images. I tried allocating more heap space, but many times the JPEG encoding fails (OutOfMemoryError exception). Is there any way around this?

There are a couple of possibilities.

My first suggestion would be to consider scaling the image. Just call JGPEGView setScale with a value smaller than 1. For example, set the scale to a value of .5 and you'll be able to use a viewSize that's half as large as before. Of course everything will also be half the size.
JPEGView jpgView = new JPEGView(jGoView1.getDocument());
jpgView.setViewPosition(0, 0);
jpgView.setScale(0.5);
Dimension dim = jpgView.getDocument().getDocumentSize();
dim.width = dim.width/2;
dim.height = dim.height/2;
jpgView.setViewSize(dim);
jpgView.setViewSize(jpgView.getDocument().getDocumentSize());
try {
OutputStream outputStream = new FileOutputStream(
"c:\\temp\\image.jpg");
jpgView.render(outputStream);
outputStream.close();
}
catch (Exception exc) {}
My second suggestion would be to consider changing the JPEG compression factor. If you take a look at the source code for the JEGView render method, you'll see that we're using a JPEG quality setting of 90%. Changing that to a lower value will reduce the quality of the image but should save on memory.

Hmmm, thanks. I suspected those might be my only two options.

As a side question, is there a way to tile background images on a JGoView (and update them when the view size changes)?

There’s nothing built into JGoView to accomplish that, but you could override JGoView.paintBackgroundDecoration if you wanted. The default behavior is to draw the background image once, but it shouldn’t be hard to make it tile the view instead. I believe that method will be called whenever the view size changes.

Regarding very large images, you probably just need to increase the maximum Java heap size. You can do so using the Xmx option, for example:

java -Xmx1G

Just out of curiosity, do you know if the heap space issue occurs because of the dimentions of the image being rendered, or because of the number of objects within the image? If it’s failing because of the sheer size of the rendered image, perhaps this issue could be alleviated by adjusting the view’s scale?

The heap space issue is occuring only because of the dimensions of the image being rendered. It doesn’t matter what’s being displayed in the image.

And yes, you are quite right that you can also deal with large images by adjusting the view's scale, but adjusting the maximum heap space can allow you to produce a large diagram with very good resolution.