显示整页图像时,JRViewer冻结

时间:2011-12-16 14:50:44

标签: java image swing jasper-reports graphics2d

我正在使用JasperViewer在Java桌面应用程序中显示报告。 该报告由2页组成 - 每页都代表一张图片。

问题是,当用户在查看器中滚动页面时,会发生巨大的冻结。 图像的大小不是很大,大约1000x1000。

以这种方式生成图像:

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

2 个答案:

答案 0 :(得分:2)

您有两个选择

1)将您的图片Icon设置为JLabel

2)对于Swing JComponetspaintComponent()代替paint()

  • 请阅读有关Graphics

  • 的教程
  • 此论坛上的大量示例(Swing已标记),

答案 1 :(得分:2)

问题已解决。 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

因此,如果设置了此参数,则报告将在ImageIcon上绘制为JLabel。否则,它使用JRGraphics2DExporter绘制,使用大图像时速度要慢得多。

因此,解决方案是在属性文件中设置指定的属性,或者使用如下方式:

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