1

I am using a JasperViewer to display the report inside of a Java desktop application. The report consists of 2 pages - each of them represents an image.

The problem is, when user scrolls the page inside the viewer, there are huge freezes. The size of image isn't so big, about 1000x1000.

The image is generated in this way:

private BufferedImage createImage(Component panel) {
    int w = (int) panel.getSize().getWidth();
    int h = (int) panel.getSize().getHeight();
    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bi.createGraphics();
    panel.paint(g);
    g.dispose();
    return bi;
}
StKiller
  • 7,631
  • 10
  • 43
  • 56

2 Answers2

2

You have two choices

1) put your image as Icon to JLabel

2) for Swing JComponets is there paintComponent() instead of paint(),

  • please read tutorial about Graphics

  • tons of examples on this forum (Swing tagged),

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Is there any relation between how is image generated and why Jasper Viewer is freezing ? – StKiller Dec 16 '11 at 15:02
  • 1
    1st place is used paint() method, 2nd place is used method(s) for connection from/to database if exist, 3rd way how did you generating image from report, to try split that to these three areas and test that separatelly, if you are using some IDE, then profile this code with JProfiler (I used that as built-in in NetBeans) or JMeter or ... – mKorbel Dec 16 '11 at 16:41
  • For reference, this [example](http://stackoverflow.com/a/5129757/230513) scrolls even larger images without any problem. – trashgod Dec 16 '11 at 17:48
  • The image and report are generated only once. It doesn't take much time. So the issue is not with image generation. The freezes are seen when the report is displayed _inside_ the JasperViewer component. – StKiller Dec 19 '11 at 06:13
  • @Andrei Podoprigora 1) you have to check if JasperViewer is based on AWT or Swing, 2) did you tried JProfiler, there you must to catch memory or procesors issue – mKorbel Dec 19 '11 at 07:13
  • @mKorbel - this component is based on Swing. – StKiller Dec 19 '11 at 10:02
2

The issue is resolved. There is a parameter in the JRViewer:

//Maximum size (in pixels) of a buffered image that would be used by {@link JRViewer JRViewer} to render a report page.
//If rendering a report page would require an image larger than this threshold
//(i.e. image width x image height > maximum size), the report page will be rendered directly on the viewer component.
//If this property is zero or negative, buffered images will never be user to render a report page.
//By default, this property is set to 0.
public static final String VIEWER_RENDER_BUFFER_MAX_SIZE

So, if this parameter is set, the reports is drawn as an ImageIcon on a JLabel. Otherwise, it's drawn using JRGraphics2DExporter that is much more slower when working with big images.

So the solution is to set the specified property in the property file or using way like this:

 /**
 * This number represents maximum size of an image ( x*y )
 * So this value cover up to 300% zoom for an image 1000x1000 pixels
 */
public static final String MAX_PIXELS_NUMBER = "10000000";   

static {
        try {
            JRProperties.setProperty(JRViewer.VIEWER_RENDER_BUFFER_MAX_SIZE, MAX_PIXELS_NUMBER);
        } catch (Exception e) {
            System.err.println("Cannot set the VIEWER_RENDER_BUFFER_MAX_SIZE property. Reports will be rendered slowly.");
        }
    }
StKiller
  • 7,631
  • 10
  • 43
  • 56