Search in sources :

Example 6 with LinearGradientPaint

use of java.awt.LinearGradientPaint in project poi by apache.

the class DrawPaint method createLinearGradientPaint.

protected Paint createLinearGradientPaint(GradientPaint fill, Graphics2D graphics) {
    // TODO: we need to find the two points for gradient - the problem is, which point at the outline
    // do you take? My solution would be to apply the gradient rotation to the shape in reverse
    // and then scan the shape for the largest possible horizontal distance
    double angle = fill.getGradientAngle();
    if (!fill.isRotatedWithShape()) {
        angle -= shape.getRotation();
    }
    Rectangle2D anchor = DrawShape.getAnchor(graphics, shape);
    final double h = anchor.getHeight(), w = anchor.getWidth(), x = anchor.getX(), y = anchor.getY();
    AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(angle), anchor.getCenterX(), anchor.getCenterY());
    double diagonal = Math.sqrt(h * h + w * w);
    Point2D p1 = new Point2D.Double(x + w / 2 - diagonal / 2, y + h / 2);
    p1 = at.transform(p1, null);
    Point2D p2 = new Point2D.Double(x + w, y + h / 2);
    p2 = at.transform(p2, null);
    if (p1.equals(p2)) {
        // gradient paint on the same point throws an exception ... and doesn't make sense
        return null;
    }
    float[] fractions = fill.getGradientFractions();
    Color[] colors = new Color[fractions.length];
    int i = 0;
    for (ColorStyle fc : fill.getGradientColors()) {
        // if fc is null, use transparent color to get color of background
        colors[i++] = (fc == null) ? TRANSPARENT : applyColorTransform(fc);
    }
    return new LinearGradientPaint(p1, p2, fractions, colors);
}
Also used : Point2D(java.awt.geom.Point2D) ColorStyle(org.apache.poi.sl.usermodel.ColorStyle) Color(java.awt.Color) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) LinearGradientPaint(java.awt.LinearGradientPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) GradientPaint(org.apache.poi.sl.usermodel.PaintStyle.GradientPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint)

Aggregations

LinearGradientPaint (java.awt.LinearGradientPaint)6 Color (java.awt.Color)5 RadialGradientPaint (java.awt.RadialGradientPaint)4 Point2D (java.awt.geom.Point2D)4 GradientPaint (java.awt.GradientPaint)3 Paint (java.awt.Paint)3 TexturePaint (java.awt.TexturePaint)3 MultipleGradientPaint (java.awt.MultipleGradientPaint)2 AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2 Graphics2D (java.awt.Graphics2D)1 Shape (java.awt.Shape)1 BufferedImage (java.awt.image.BufferedImage)1 ColorStyle (org.apache.poi.sl.usermodel.ColorStyle)1 GradientPaint (org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)1