views:

102

answers:

1

Hi,

I'm passing a simple implementation of JRAbstractSvgRenderer (taken from the ireports pdf manual) as one of the parameters using JasperFillManager.fillReport.

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

I have linked an Image component in my report to this parameter but the image does not display. What am I missing here?

What I'd like to accomplish is to eventually pass an already created Java2D image to my report but I don't want pass it as a raster image.

Thanx in advance

A: 

I don't know JasperReports, but you can create an off-screen java.awt.Image easily:

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

As a handy way to preview a rendering, you can create a JComponent that displays the image, e.g.:

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

Addendum: You might also try to determine if your renderer is being invoked at all or if it is somehow rendering nothing. If the latter, you might simplify your rendering code to some bare minimum such as setting the color and filling rect.

trashgod
Thanx but I don't want a raster image.
Christo Du Preez
Ah, that makes sense. See above.
trashgod