在fillReport中传递JRAbstractSvgRenderer(JRRenderable)

时间:2010-02-26 09:45:48

标签: java swing jasper-reports

我使用JasperFillManager.fillReport传递JRAbstractSvgRenderer的简单实现(取自ireports pdf手册)作为参数之一。

public class CustomImageRenderer extends JRAbstractSvgRenderer {

    @Override
    public void render(Graphics2D g2d, Rectangle2D rect) throws JRException {
        System.out.println("CustomImageRenderer.render");
        // Save the Graphics2D affine transform
        AffineTransform savedTrans = g2d.getTransform();
        Font savedFont = g2d.getFont();
        // Paint a nice background...
        g2d.setPaint(new GradientPaint(0, 0, Color.ORANGE,
            0, (int) rect.getHeight(), Color.PINK));
        g2d.fillRect(0, 0, (int) rect.getWidth(), (int) rect.getHeight());
        Font myfont = new Font("Arial Black", Font.PLAIN, 50);
        g2d.setFont(myfont);
        FontRenderContext frc = g2d.getFontRenderContext();
        String text = new String("JasperReports!!!");
        TextLayout textLayout = new TextLayout(text, myfont, frc);
        Shape outline = textLayout.getOutline(null);
        Rectangle r = outline.getBounds();
        // Translate the graphic to center the text
        g2d.translate(
            (rect.getWidth() / 2) - (r.width / 2),
            rect.getHeight() / 2 + (r.height / 2));
        g2d.setColor(Color.BLACK);
        g2d.draw(outline);
        // Restore the Graphics2D affine transform
        g2d.setFont(savedFont);
        g2d.setTransform(savedTrans);
    }
}

...

Map parameters = new HashMap();
parameters.put("IMAGEPARAM", new CustomImageRenderer());

...

JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters, conn);

我已将报告中的图像组件链接到此参数,但图像未显示。我在这里缺少什么?

我想要完成的是最终将已创建的Java2D图像传递给我的报告,但我不想将其作为光栅图像传递。

1 个答案:

答案 0 :(得分:0)

我不知道JasperReports,但您可以轻松地创建屏幕外java.awt.Image

private Image getImage(int h, int w) {
    BufferedImage bi = new BufferedImage(h, w, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bi.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                         RenderingHints.VALUE_ANTIALIAS_ON);
    // your drawing code
    g2d.dispose();
    return bi;
}

作为预览渲染的便捷方式,您可以创建一个显示图像的JComponent,例如:

JLabel label = new JLabel(new ImageIcon(getImage(h, w)));

附录:您可能还会尝试确定是否正在调用您的渲染器,或者它是否以某种方式呈现任何内容。如果是后者,您可以将渲染代码简化为最低限度,例如设置颜色和填充rect