Jump to content
We've recently updated our Privacy Statement, available here ×

How to upgrade code that uses JRAbstractSvgRenderer


ian.james.lloyd

Recommended Posts

Hi,

Upgrading some code from JasperReports 4.8.0 to 6.7.0

Code uses an extension of JRAbstractSvgRenderer to provide an AWT gradient fill, as described in iReports Ultimate Guide section for dynamic images (2011 edition).

An example fo the type of code in question can be seen at http://tedwise.com/2010/02/07/creating-a-circular-gradient-in-ireport.

JRAbstractSvgRenderer is now deprecated, and trying to run a report using the code against 6.7.0 code base fails at run time - exception reports the code doesn't implement an appropriate interface.

I've tried looking at the current iReports guide onthis page, but the link doesn't appear to be valid.

Could anyone provide an example of how one would provide equivalent fucntionality against the current code base?

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

  • 1 month later...

I was able to produce a gradient using this code. My modifications are in red.

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Dimension2D;

import net.sf.jasperreports.renderers.AbstractRenderToImageDataRenderer;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReportsContext;

/**
 * Draws a colored, circular gradient background used in the report.
 */
public class CircularGradientImageRenderer extends AbstractRenderToImageDataRenderer {
    /**
     * The gradient color.
     */
    private String rgb;
    private int width;
    private int height;

    /**
     * Create a circular gradient image in a bounded square.
     *
     * @param rgb The gradient color.
     */
    public CircularGradientImageRenderer(String rgb) {
        this.rgb = rgb;
    }

    /**
     * Pull out a substring without returning null.
     *
     * @param value The value.
     * @param begin The beginning index.
     * @param end   The ending index.
     * @return The substring.
     */
    private static String safeSubstring(String value, int begin, int end) {
        String sub = "";

        if (value != null) {
            if (value.length() > begin) {
                end = Math.min(end, value.length() - 1);
                sub = value.substring(begin, end + 1);
            }
        }

        return sub;
    }

    /**
     * Parse a hex value without throwing an exception.
     *
     * @param value The value to convert.
     * @return The integer value.
     */
    private static int safeHexParse(String value) {
        try {
            return Integer.parseInt(value, 16);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    /**
     * Convert a string hex color into a Java Color object.
     *
     * @param rgb The string value.
     * @return The Color object.
     */
    public static Color stringToRGB(String rgb) {
        int r = 0;
        int g = 0;
        int b = 0;

        if (rgb != null && rgb.startsWith("#")) {
            r = safeHexParse(safeSubstring(rgb, 1, 2));
            g = safeHexParse(safeSubstring(rgb, 3, 4));
            b = safeHexParse(safeSubstring(rgb, 5, 6));
        }

        return new Color(r, g, b);
    }

    public void render(JasperReportsContext jasperReportsContext
                      ,Graphics2D  g2d
                      ,Rectangle2D  rect) throws JRException {
        // Save the Graphics2D affine transform
        AffineTransform savedTrans = g2d.getTransform();

        float radius = (float) (Math.max(rect.getHeight(), rect.getWidth()) / 2);
        float[] fractions = {0.0f, 0.3f, 1.0f};
        Color[] colors = {Color.WHITE, Color.WHITE, stringToRGB(rgb)};

        // Paint a nice background...
        g2d.setPaint(new RadialGradientPaint((float) rect.getCenterX(), (float) rect.getCenterY(), radius, fractions, colors));
        
        this.width  = (int) rect.getWidth();
        this.height = (int) rect.getHeight();
        
        g2d.fillRect((int) rect.getX(), (int) rect.getY(), this.width, this.height);

        g2d.draw(rect);

        // Restore the Graphics2D affine transform
        g2d.setTransform(savedTrans);
    }
    
    public Dimension2D getDimension(JasperReportsContext jasperReportsContext) throws JRException
    {
        return new Dimension(this.height,this.width);
    }

}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...