Search code examples
javaimageswingjasper-reportsgraphics2d

JRViewer freezing when displaying full page images


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;
}

Solution

  • 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.");
            }
        }