Example usage for java.awt.geom Area intersect

List of usage examples for java.awt.geom Area intersect

Introduction

In this page you can find the example usage for java.awt.geom Area intersect.

Prototype

public void intersect(Area rhs) 

Source Link

Document

Sets the shape of this Area to the intersection of its current shape and the shape of the specified Area .

Usage

From source file:org.pentaho.plugin.jfreereport.reportcharts.JFreeChartReportDrawable.java

private AbstractImageMapEntry createMapEntry(final Shape area, final Rectangle2D dataArea) {
    if (buggyDrawArea) {
        if (area instanceof Ellipse2D) {
            final Ellipse2D ellipse2D = (Ellipse2D) area;
            if (ellipse2D.getWidth() == ellipse2D.getHeight()) {
                return new CircleImageMapEntry((float) (ellipse2D.getCenterX() + dataArea.getX()),
                        (float) (ellipse2D.getCenterY() + dataArea.getY()), (float) (ellipse2D.getWidth() / 2));
            }/*from w  w  w . j  av a  2 s  .  com*/
        } else if (area instanceof Rectangle2D) {
            final Rectangle2D rect = (Rectangle2D) area;
            return (new RectangleImageMapEntry((float) (rect.getX() + dataArea.getX()),
                    (float) (rect.getY() + dataArea.getY()), (float) (rect.getX() + rect.getWidth()),
                    (float) (rect.getY() + rect.getHeight())));
        }
    } else {
        if (area instanceof Ellipse2D) {
            final Ellipse2D ellipse2D = (Ellipse2D) area;
            if (ellipse2D.getWidth() == ellipse2D.getHeight()) {
                return new CircleImageMapEntry((float) (ellipse2D.getCenterX()),
                        (float) (ellipse2D.getCenterY()), (float) (ellipse2D.getWidth() / 2));
            }
        } else if (area instanceof Rectangle2D) {
            final Rectangle2D rect = (Rectangle2D) area;
            return (new RectangleImageMapEntry((float) (rect.getX()), (float) (rect.getY()),
                    (float) (rect.getX() + rect.getWidth()), (float) (rect.getY() + rect.getHeight())));
        }
    }

    final Area a = new Area(area);
    if (buggyDrawArea) {
        a.transform(AffineTransform.getTranslateInstance(dataArea.getX(), dataArea.getY()));
    }
    if (dataArea.isEmpty() == false) {
        a.intersect(new Area(dataArea));
    }
    final PathIterator pathIterator = a.getPathIterator(null, 2);
    final FloatList floats = new FloatList(100);
    final float[] coords = new float[6];
    while (pathIterator.isDone() == false) {
        final int retval = pathIterator.currentSegment(coords);
        if (retval == PathIterator.SEG_MOVETO || retval == PathIterator.SEG_LINETO) {
            floats.add(coords[0]);
            floats.add(coords[1]);
        }
        pathIterator.next();
    }

    if (floats.size() == 0) {
        return null;
    }
    return (new PolygonImageMapEntry(floats.toArray()));
}

From source file:org.jfree.experimental.swt.SWTGraphics2D.java

/**
 * Returns <code>true</code> if the rectangle (in device space) intersects
 * with the shape (the interior, if <code>onStroke</code> is false, 
 * otherwise the stroked outline of the shape).
 * //from   ww  w . j  a v  a  2s. c  o m
 * @param rect  a rectangle (in device space).
 * @param s the shape.
 * @param onStroke  test the stroked outline only?
 * 
 * @return A boolean. 
 */
@Override
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
    AffineTransform transform = getTransform();
    Shape ts;
    if (onStroke) {
        Stroke stroke = getStroke();
        ts = transform.createTransformedShape(stroke.createStrokedShape(s));
    } else {
        ts = transform.createTransformedShape(s);
    }
    if (!rect.getBounds2D().intersects(ts.getBounds2D())) {
        return false;
    }
    Area a1 = new Area(rect);
    Area a2 = new Area(ts);
    a1.intersect(a2);
    return !a1.isEmpty();
}

From source file:DefaultGraphics2D.java

/**
 * Intersects the current <code>Clip</code> with the interior of the
 * specified <code>Shape</code> and sets the <code>Clip</code> to the
 * resulting intersection. The specified <code>Shape</code> is transformed
 * with the current <code>Graphics2D</code>
 * <code>Transform</code> before
 * being intersected with the current <code>Clip</code>. This method is
 * used to make the current <code>Clip</code> smaller. To make the
 * <code>Clip</code> larger, use <code>setClip</code>. The <i>user clip</i>
 * modified by this method is independent of the clipping associated with
 * device bounds and visibility. If no clip has previously been set, or if the
 * clip has been cleared using//w  w  w  . j  a v  a  2 s .  c  o m
 * {@link java.awt.Graphics#setClip(Shape) setClip} with a <code>null</code>
 * argument, the specified <code>Shape</code> becomes the new user clip.
 * 
 * @param s
 *          the <code>Shape</code> to be intersected with the current
 *          <code>Clip</code>. If <code>s</code> is <code>null</code>,
 *          this method clears the current <code>Clip</code>.
 */
public void clip(Shape s) {
    if (s != null)
        s = transform.createTransformedShape(s);

    if (clip != null) {
        Area newClip = new Area(clip);
        newClip.intersect(new Area(s));
        clip = new GeneralPath(newClip);
    } else {
        clip = s;
    }
}

From source file:org.apache.pdfbox.pdfviewer.PageDrawer.java

/**
 * Set the clipping Path./*from   w  w  w.j  a  v  a2s  .c  o  m*/
 *
 * @param windingRule The winding rule this path will use.
 * 
 */
public void setClippingPath(int windingRule) {
    PDGraphicsState graphicsState = getGraphicsState();
    GeneralPath clippingPath = (GeneralPath) getLinePath().clone();
    clippingPath.setWindingRule(windingRule);
    // If there is already set a clipping path, we have to intersect the new with the existing one
    if (graphicsState.getCurrentClippingPath() != null) {
        Area currentArea = new Area(getGraphicsState().getCurrentClippingPath());
        Area newArea = new Area(clippingPath);
        currentArea.intersect(newArea);
        graphicsState.setCurrentClippingPath(currentArea);
    } else {
        graphicsState.setCurrentClippingPath(clippingPath);
    }
    getLinePath().reset();
}

From source file:org.apache.pdfbox.rendering.PageDrawer.java

@Override
public void fillPath(int windingRule) throws IOException {
    graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    graphics.setPaint(getNonStrokingPaint());
    setClip();/* w ww.j  a v a  2s. c om*/
    linePath.setWindingRule(windingRule);

    // disable anti-aliasing for rectangular paths, this is a workaround to avoid small stripes
    // which occur when solid fills are used to simulate piecewise gradients, see PDFBOX-2302
    // note that we ignore paths with a width/height under 1 as these are fills used as strokes,
    // see PDFBOX-1658 for an example
    Rectangle2D bounds = linePath.getBounds2D();
    boolean noAntiAlias = isRectangular(linePath) && bounds.getWidth() > 1 && bounds.getHeight() > 1;
    if (noAntiAlias) {
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
    }

    if (!(graphics.getPaint() instanceof Color)) {
        // apply clip to path to avoid oversized device bounds in shading contexts (PDFBOX-2901)
        Area area = new Area(linePath);
        area.intersect(new Area(graphics.getClip()));
        graphics.fill(area);
    } else {
        graphics.fill(linePath);
    }

    linePath.reset();

    if (noAntiAlias) {
        // JDK 1.7 has a bug where rendering hints are reset by the above call to
        // the setRenderingHint method, so we re-set all hints, see PDFBOX-2302
        setRenderingHints();
    }
}

From source file:org.eclipse.birt.chart.device.g2d.G2dRendererBase.java

@Override
public void fillRectangle(RectangleRenderEvent rre) throws ChartException {
    if (iv != null) {
        iv.modifyEvent(rre);/*from   w  ww  .  j  ava 2  s .co m*/
    }
    final Fill flBackground = validateMultipleFill(rre.getBackground());

    if (isFullTransparent(flBackground)) {
        return;
    }

    final Bounds bo = normalizeBounds(rre.getBounds());
    final Rectangle2D.Double r2d = new Rectangle2D.Double(bo.getLeft(), bo.getTop(), bo.getWidth(),
            bo.getHeight());
    if (flBackground instanceof ColorDefinition) {
        final ColorDefinition cd = (ColorDefinition) flBackground;
        _g2d.setColor((Color) _ids.getColor(cd));
        _g2d.fill(r2d);
    } else if (flBackground instanceof Gradient) {
        final Gradient g = (Gradient) flBackground;
        final ColorDefinition cdStart = g.getStartColor();
        final ColorDefinition cdEnd = g.getEndColor();
        // boolean bCyclic = g.isCyclic();
        double dAngleInDegrees = g.getDirection();
        final double dAngleInRadians = ((-dAngleInDegrees * Math.PI) / 180.0);
        // int iAlpha = g.getTransparency();

        if (dAngleInDegrees < -90 || dAngleInDegrees > 90) {
            throw new ChartException(ChartDeviceExtensionPlugin.ID, ChartException.RENDERING,
                    "SwingRendererImpl.exception.gradient.angle", //$NON-NLS-1$
                    new Object[] { new Double(dAngleInDegrees) }, Messages.getResourceBundle(getULocale()));
        }

        Point2D.Double p2dStart, p2dEnd;
        if (dAngleInDegrees == 90) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dEnd = new Point2D.Double(bo.getLeft(), bo.getTop());
        } else if (dAngleInDegrees == -90) {
            p2dEnd = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
        } else if (dAngleInDegrees > 0) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(),
                    bo.getTop() + bo.getHeight() - bo.getWidth() * Math.abs(Math.tan(dAngleInRadians)));
        } else if (dAngleInDegrees < 0) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(),
                    bo.getTop() + bo.getWidth() * Math.abs(Math.tan(dAngleInRadians)));
        } else {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(), bo.getTop());
        }
        _g2d.setPaint(new GradientPaint(p2dStart, (Color) _ids.getColor(cdStart), p2dEnd,
                (Color) _ids.getColor(cdEnd)));
        _g2d.fill(r2d);
    } else if (flBackground instanceof org.eclipse.birt.chart.model.attribute.Image) {
        if (flBackground instanceof PatternImage) {
            fillWithPatternImage(new Area(r2d), flBackground);
            return;
        }
        java.awt.Image img = createImageFromModel(flBackground);

        if (img != null) {
            final Shape shClip = _g2d.getClip();
            Area ar2 = new Area(r2d);
            if (shClip != null) {
                Area ar1 = new Area(shClip);
                ar2.intersect(ar1);
            }
            _g2d.setClip(ar2);

            img = scaleImage(img);
            final Size szImage = _ids.getSize(img);

            int iXRepeat = (int) (Math.ceil(r2d.width / szImage.getWidth()));
            int iYRepeat = (int) (Math.ceil(r2d.height / szImage.getHeight()));
            ImageObserver io = (ImageObserver) _ids.getObserver();
            for (int i = 0; i < iXRepeat; i++) {
                for (int j = 0; j < iYRepeat; j++) {
                    _g2d.drawImage(img, (int) (r2d.x + i * szImage.getWidth()),
                            (int) (r2d.y + j * szImage.getHeight()), io);
                }
            }

            _g2d.setClip(shClip); // RESTORE
        }
    }
}

From source file:org.eclipse.birt.chart.device.g2d.G2dRendererBase.java

@Override
public void fillPolygon(PolygonRenderEvent pre) throws ChartException {
    if (iv != null) {
        iv.modifyEvent(pre);/*from ww w  . ja va 2  s  . co  m*/
    }
    final Fill flBackground = validateMultipleFill(pre.getBackground());

    if (isFullTransparent(flBackground)) {
        return;
    }

    final Location[] loa = pre.getPoints();
    final int[][] i2a = getCoordinatesAsInts(loa);

    if (flBackground instanceof ColorDefinition) {
        final ColorDefinition cd = (ColorDefinition) flBackground;
        _g2d.setColor((Color) _ids.getColor(cd));
        _g2d.fill(getPolygon(loa));
    } else if (flBackground instanceof Gradient) {
        final Gradient g = (Gradient) flBackground;
        final ColorDefinition cdStart = g.getStartColor();
        final ColorDefinition cdEnd = g.getEndColor();
        // final boolean bRadial = g.isCyclic();
        final double dAngleInDegrees = g.getDirection();
        final double dAngleInRadians = ((-dAngleInDegrees * Math.PI) / 180.0);
        // final int iAlpha = g.getTransparency();

        final double dMinX = BaseRenderer.getX(loa, IConstants.MIN);
        final double dMaxX = BaseRenderer.getX(loa, IConstants.MAX);
        final double dMinY = BaseRenderer.getY(loa, IConstants.MIN);
        final double dMaxY = BaseRenderer.getY(loa, IConstants.MAX);

        if (dAngleInDegrees < -90 || dAngleInDegrees > 90) {
            throw new ChartException(ChartDeviceExtensionPlugin.ID, ChartException.RENDERING,
                    "SwingRendererImpl.exception.gradient.angle", //$NON-NLS-1$
                    new Object[] { new Double(dAngleInDegrees) }, Messages.getResourceBundle(getULocale()));
        }

        Point2D.Double p2dStart, p2dEnd;
        if (dAngleInDegrees == 90) {
            p2dStart = new Point2D.Double(dMinX, dMaxY);
            p2dEnd = new Point2D.Double(dMinX, dMinY);
        } else if (dAngleInDegrees == -90) {
            p2dStart = new Point2D.Double(dMinX, dMinY);
            p2dEnd = new Point2D.Double(dMinX, dMaxY);
        } else if (dAngleInDegrees > 0) {
            p2dStart = new Point2D.Double(dMinX, dMaxY);
            p2dEnd = new Point2D.Double(dMaxX, dMaxY - (dMaxX - dMinX) * Math.abs(Math.tan(dAngleInRadians)));
        } else if (dAngleInDegrees < 0) {
            p2dStart = new Point2D.Double(dMinX, dMinY);
            p2dEnd = new Point2D.Double(dMaxX, dMinY + (dMaxX - dMinX) * Math.abs(Math.tan(dAngleInRadians)));
        } else {
            p2dStart = new Point2D.Double(dMinX, dMinY);
            p2dEnd = new Point2D.Double(dMaxX, dMinY);
        }
        _g2d.setPaint(new GradientPaint(p2dStart, (Color) _ids.getColor(cdStart), p2dEnd,
                (Color) _ids.getColor(cdEnd)));
        _g2d.fill(getPolygon(loa));
    } else if (flBackground instanceof org.eclipse.birt.chart.model.attribute.Image) {
        Area ar2 = new Area(new Polygon(i2a[0], i2a[1], loa.length));
        if (flBackground instanceof PatternImage) {
            fillWithPatternImage(ar2, flBackground);
            return;
        }
        java.awt.Image img = createImageFromModel(flBackground);

        if (img != null) {
            final Shape shClip = _g2d.getClip();
            if (shClip != null) {
                Area ar1 = new Area(shClip);
                ar2.intersect(ar1);
            }
            _g2d.setClip(ar2);

            final double dMinX = BaseRenderer.getX(loa, IConstants.MIN);
            final double dMaxX = BaseRenderer.getX(loa, IConstants.MAX);
            final double dMinY = BaseRenderer.getY(loa, IConstants.MIN);
            final double dMaxY = BaseRenderer.getY(loa, IConstants.MAX);
            final Size szImage = _ids.getSize(img);

            final int iXRepeat = (int) (Math.ceil((dMaxX - dMinX) / szImage.getWidth()));
            final int iYRepeat = (int) (Math.ceil((dMaxY - dMinY) / szImage.getHeight()));
            final ImageObserver io = (ImageObserver) _ids.getObserver();
            for (int i = 0; i < iXRepeat; i++) {
                for (int j = 0; j < iYRepeat; j++) {
                    _g2d.drawImage(img, (int) (dMinX + i * szImage.getWidth()),
                            (int) (dMinY + j * szImage.getHeight()), io);
                }
            }

            _g2d.setClip(shClip); // RESTORE
        }
    }
}

From source file:org.eclipse.birt.chart.device.g2d.G2dRendererBase.java

@Override
public void drawArc(ArcRenderEvent are) throws ChartException {
    if (iv != null) {
        iv.modifyEvent(are);//from   w  w  w.j  av  a  2 s  .  c  o m
    }

    // CHECK IF THE LINE ATTRIBUTES ARE CORRECTLY DEFINED
    final LineAttributes lia = are.getOutline();
    if (!validateLineAttributes(are.getSource(), lia)) {
        return;
    }

    // SETUP THE FOREGROUND COLOR (DARKER BACKGROUND IF DEFINED AS NULL)
    final Color cFG = (Color) validateEdgeColor(lia.getColor(), are.getBackground(), _ids);
    if (cFG == null || cFG.getAlpha() == 0) {
        return;
    }

    // DRAW THE ARC
    Stroke sPrevious = null;
    Stroke sCurrent = getCachedStroke(lia);
    if (sCurrent != null) // SOME STROKE DEFINED?
    {
        sPrevious = _g2d.getStroke();
        _g2d.setStroke(sCurrent);
    }
    _g2d.setColor(cFG);

    if ((are.getInnerRadius() >= 0 && are.getOuterRadius() > 0 && are.getInnerRadius() < are.getOuterRadius())
            || (are.getInnerRadius() > 0 && are.getOuterRadius() <= 0)) {
        Bounds rctOuter = getOuterRectangle(are);
        Bounds rctInner = getInnerRectangle(are);

        Shape outerArc = new Arc2D.Double(rctOuter.getLeft(), rctOuter.getTop(), rctOuter.getWidth(),
                rctOuter.getHeight(), are.getStartAngle(), are.getAngleExtent(), Arc2D.OPEN);
        Shape innerArc = new Arc2D.Double(rctInner.getLeft(), rctInner.getTop(), rctInner.getWidth(),
                rctInner.getHeight(), are.getStartAngle() + are.getAngleExtent(), -are.getAngleExtent(),
                Arc2D.OPEN);

        double startAngle = Math.toRadians(-are.getStartAngle());
        double stopAngle = Math.toRadians(-are.getStartAngle() - are.getAngleExtent());

        double xsOuter = (rctOuter.getLeft() + (Math.cos(startAngle) * 0.5 + 0.5) * rctOuter.getWidth());
        double ysOuter = (rctOuter.getTop() + (Math.sin(startAngle) * 0.5 + 0.5) * rctOuter.getHeight());

        double xeInner = (rctInner.getLeft() + (Math.cos(stopAngle) * 0.5 + 0.5) * rctInner.getWidth());
        double yeInner = (rctInner.getTop() + (Math.sin(stopAngle) * 0.5 + 0.5) * rctInner.getHeight());

        GeneralPath gp = new GeneralPath();
        gp.append(outerArc, false);
        gp.lineTo((float) xeInner, (float) yeInner);
        gp.append(innerArc, false);
        gp.lineTo((float) xsOuter, (float) ysOuter);

        Area area = new Area(gp);
        Shape prevClip = _g2d.getClip();
        Area ar2 = new Area(area);
        if (prevClip != null) {
            Area ar1 = new Area(prevClip);
            ar2.intersect(ar1);
        }
        _g2d.setClip(ar2);
        _g2d.draw(area);
        _g2d.setClip(prevClip);

    } else {
        _g2d.draw(new Arc2D.Double(are.getTopLeft().getX(), are.getTopLeft().getY(), are.getWidth(),
                are.getHeight(), are.getStartAngle(), are.getAngleExtent(), toG2dArcType(are.getStyle())));
    }

    if (sPrevious != null) // RESTORE PREVIOUS STROKE
    {
        _g2d.setStroke(sPrevious);
    }
}

From source file:org.eclipse.birt.chart.device.g2d.G2dRendererBase.java

@Override
public void fillArc(ArcRenderEvent are) throws ChartException {
    if (iv != null) {
        iv.modifyEvent(are);//from w  ww .j a  v  a  2s  .  c  o m
    }

    final Fill flBackground = validateMultipleFill(are.getBackground());

    if (isFullTransparent(flBackground)) {
        return;
    }

    if (flBackground instanceof ColorDefinition) {
        final ColorDefinition cl = (ColorDefinition) flBackground;
        final Color clrPrevious = _g2d.getColor();
        final Color currentColor = (Color) _ids.getColor(cl);
        _g2d.setColor(currentColor);

        if ((are.getInnerRadius() >= 0 && are.getOuterRadius() > 0
                && are.getInnerRadius() < are.getOuterRadius())
                || (are.getInnerRadius() > 0 && are.getOuterRadius() <= 0)) {
            Bounds rctOuter = getOuterRectangle(are);
            Bounds rctInner = getInnerRectangle(are);

            Shape outerArc = new Arc2D.Double(rctOuter.getLeft(), rctOuter.getTop(), rctOuter.getWidth(),
                    rctOuter.getHeight(), are.getStartAngle(), are.getAngleExtent(), Arc2D.PIE);

            Shape innerArc = new Arc2D.Double(rctInner.getLeft(), rctInner.getTop(), rctInner.getWidth(),
                    rctInner.getHeight(), are.getStartAngle(), are.getAngleExtent(), Arc2D.PIE);

            Area fArea = new Area(outerArc);
            fArea.exclusiveOr(new Area(innerArc));

            Shape prevClip = _g2d.getClip();
            Area ar2 = new Area(fArea);
            if (prevClip != null) {
                Area ar1 = new Area(prevClip);
                ar2.intersect(ar1);
            }
            _g2d.setClip(ar2);
            _g2d.fill(fArea);
            _g2d.setClip(prevClip);
        } else {
            _g2d.fill(new Arc2D.Double(are.getTopLeft().getX(), are.getTopLeft().getY(), are.getWidth(),
                    are.getHeight(), are.getStartAngle(), are.getAngleExtent(), toG2dArcType(are.getStyle())));
        }

        _g2d.setColor(clrPrevious); // RESTORE
    } else if (flBackground instanceof Gradient) {
        final Gradient g = (Gradient) flBackground;
        final ColorDefinition cdStart = g.getStartColor();
        final ColorDefinition cdEnd = g.getEndColor();
        double dAngleInDegrees = g.getDirection();
        final double dAngleInRadians = ((-dAngleInDegrees * Math.PI) / 180.0);
        Bounds bo = are.getBounds();

        if (dAngleInDegrees < -90 || dAngleInDegrees > 90) {
            throw new ChartException(ChartDeviceExtensionPlugin.ID, ChartException.RENDERING,
                    "SwingRendererImpl.exception.gradient.angle", //$NON-NLS-1$
                    new Object[] { new Double(dAngleInDegrees) }, Messages.getResourceBundle(getULocale()));
        }

        Point2D.Double p2dStart, p2dEnd;
        if (dAngleInDegrees == 90) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dEnd = new Point2D.Double(bo.getLeft(), bo.getTop());
        } else if (dAngleInDegrees == -90) {
            p2dEnd = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
        } else if (dAngleInDegrees > 0) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(),
                    bo.getTop() + bo.getHeight() - bo.getWidth() * Math.abs(Math.tan(dAngleInRadians)));
        } else if (dAngleInDegrees < 0) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(),
                    bo.getTop() + bo.getWidth() * Math.abs(Math.tan(dAngleInRadians)));
        } else {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(), bo.getTop());
        }

        final Paint pPrevious = _g2d.getPaint();
        _g2d.setPaint(new GradientPaint(p2dStart, (Color) _ids.getColor(cdStart), p2dEnd,
                (Color) _ids.getColor(cdEnd)));

        if ((are.getInnerRadius() >= 0 && are.getOuterRadius() > 0
                && are.getInnerRadius() < are.getOuterRadius())
                || (are.getInnerRadius() > 0 && are.getOuterRadius() <= 0)) {
            Bounds rctOuter = getOuterRectangle(are);
            Bounds rctInner = getInnerRectangle(are);

            Shape outerArc = new Arc2D.Double(rctOuter.getLeft(), rctOuter.getTop(), rctOuter.getWidth(),
                    rctOuter.getHeight(), are.getStartAngle(), are.getAngleExtent(), Arc2D.PIE);

            Shape innerArc = new Arc2D.Double(rctInner.getLeft(), rctInner.getTop(), rctInner.getWidth(),
                    rctInner.getHeight(), are.getStartAngle(), are.getAngleExtent(), Arc2D.PIE);

            Area fArea = new Area(outerArc);
            fArea.exclusiveOr(new Area(innerArc));

            Shape prevClip = _g2d.getClip();
            Area ar2 = new Area(fArea);
            if (prevClip != null) {
                Area ar1 = new Area(prevClip);
                ar2.intersect(ar1);
            }
            _g2d.setClip(ar2);
            _g2d.fill(fArea);
            _g2d.setClip(prevClip);
        } else {
            _g2d.fill(new Arc2D.Double(are.getTopLeft().getX(), are.getTopLeft().getY(), are.getWidth(),
                    are.getHeight(), are.getStartAngle(), are.getAngleExtent(), toG2dArcType(are.getStyle())));
        }

        _g2d.setPaint(pPrevious); // RESTORE
    } else if (flBackground instanceof org.eclipse.birt.chart.model.attribute.Image) {
        final Bounds bo = are.getBounds();
        final Rectangle2D.Double r2d = new Rectangle2D.Double(bo.getLeft(), bo.getTop(), bo.getWidth(),
                bo.getHeight());

        Shape shPreviousClip = _g2d.getClip();
        Area ar = null;

        if ((are.getInnerRadius() >= 0 && are.getOuterRadius() > 0
                && are.getInnerRadius() < are.getOuterRadius())
                || (are.getInnerRadius() > 0 && are.getOuterRadius() <= 0)) {
            Bounds rctOuter = getOuterRectangle(are);
            Bounds rctInner = getInnerRectangle(are);

            Shape outerArc = new Arc2D.Double(rctOuter.getLeft(), rctOuter.getTop(), rctOuter.getWidth(),
                    rctOuter.getHeight(), are.getStartAngle(), are.getAngleExtent(), Arc2D.PIE);

            Shape innerArc = new Arc2D.Double(rctInner.getLeft(), rctInner.getTop(), rctInner.getWidth(),
                    rctInner.getHeight(), are.getStartAngle(), are.getAngleExtent(), Arc2D.PIE);

            Area fArea = new Area(outerArc);
            fArea.exclusiveOr(new Area(innerArc));

            if (shPreviousClip != null) {
                Area ar1 = new Area(shPreviousClip);
                fArea.intersect(ar1);
            }
            // _g2d.setClip( fArea );
            ar = fArea;
        } else {
            // SETUP THE CLIPPING AREA
            final Shape shArc = new Arc2D.Double(are.getTopLeft().getX(), are.getTopLeft().getY(),
                    are.getWidth(), are.getHeight(), are.getStartAngle(), are.getAngleExtent(),
                    toG2dArcType(are.getStyle()));

            Area ar2 = new Area(shArc);
            if (shPreviousClip != null) {
                Area ar1 = new Area(shPreviousClip);
                ar2.intersect(ar1);
            }
            // _g2d.setClip( ar2 );
            ar = ar2;
        }

        if (flBackground instanceof PatternImage) {
            fillWithPatternImage(new Area(ar), flBackground);
            return;
        }

        _g2d.setClip(ar);

        // LOAD THE IMAGE
        java.awt.Image img = createImageFromModel(flBackground);
        if (img != null) {
            // REPLICATE THE IMAGE AS NEEDED
            final Size szImage = _ids.getSize(img);
            int iXRepeat = (int) (Math.ceil(r2d.width / szImage.getWidth()));
            int iYRepeat = (int) (Math.ceil(r2d.height / szImage.getHeight()));
            ImageObserver io = (ImageObserver) _ids.getObserver();
            for (int i = 0; i < iXRepeat; i++) {
                for (int j = 0; j < iYRepeat; j++) {
                    _g2d.drawImage(img, (int) (r2d.x + i * szImage.getWidth()),
                            (int) (r2d.y + j * szImage.getHeight()), io);
                }
            }
        }

        _g2d.setClip(shPreviousClip); // RESTORE
    }
}

From source file:org.eclipse.birt.chart.device.g2d.G2dRendererBase.java

@Override
public void fillOval(OvalRenderEvent ore) throws ChartException {
    if (iv != null) {
        iv.modifyEvent(ore);/*from   www .  j  a  v a 2s . c  om*/
    }

    final Fill flBackground = validateMultipleFill(ore.getBackground());

    if (isFullTransparent(flBackground)) {
        return;
    }

    final Bounds bo = ore.getBounds();
    final Ellipse2D.Double e2d = new Ellipse2D.Double(bo.getLeft(), bo.getTop(), bo.getWidth(), bo.getHeight());
    if (flBackground instanceof ColorDefinition) {
        final ColorDefinition cd = (ColorDefinition) flBackground;
        _g2d.setColor((Color) _ids.getColor(cd));
        _g2d.fill(e2d);
    } else if (flBackground instanceof Gradient) {
        final Gradient g = (Gradient) flBackground;
        final ColorDefinition cdStart = g.getStartColor();
        final ColorDefinition cdEnd = g.getEndColor();
        // boolean bCyclic = g.isCyclic();
        double dAngleInDegrees = g.getDirection();
        final double dAngleInRadians = ((-dAngleInDegrees * Math.PI) / 180.0);

        if (dAngleInDegrees < -90 || dAngleInDegrees > 90) {
            throw new ChartException(ChartDeviceExtensionPlugin.ID, ChartException.RENDERING,
                    "SwingRendererImpl.exception.gradient.angle", //$NON-NLS-1$
                    new Object[] { new Double(dAngleInDegrees) }, Messages.getResourceBundle(getULocale()));
        }

        Point2D.Double p2dStart, p2dEnd;
        if (dAngleInDegrees == 90) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dEnd = new Point2D.Double(bo.getLeft(), bo.getTop());
        } else if (dAngleInDegrees == -90) {
            p2dEnd = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
        } else if (dAngleInDegrees > 0) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop() + bo.getHeight());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(),
                    bo.getTop() + bo.getHeight() - bo.getWidth() * Math.abs(Math.tan(dAngleInRadians)));
        } else if (dAngleInDegrees < 0) {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(),
                    bo.getTop() + bo.getWidth() * Math.abs(Math.tan(dAngleInRadians)));
        } else {
            p2dStart = new Point2D.Double(bo.getLeft(), bo.getTop());
            p2dEnd = new Point2D.Double(bo.getLeft() + bo.getWidth(), bo.getTop());
        }
        _g2d.setPaint(new GradientPaint(p2dStart, (Color) _ids.getColor(cdStart), p2dEnd,
                (Color) _ids.getColor(cdEnd)));
        _g2d.fill(e2d);
    } else if (flBackground instanceof org.eclipse.birt.chart.model.attribute.Image) {
        Area ar2 = new Area(e2d);
        if (flBackground instanceof PatternImage) {
            fillWithPatternImage(ar2, flBackground);
            return;
        }
        java.awt.Image img = createImageFromModel(flBackground);

        if (img != null) {

            final Shape shClip = _g2d.getClip();
            if (shClip != null) {
                Area ar1 = new Area(shClip);
                ar2.intersect(ar1);
            }
            _g2d.setClip(ar2);

            final Size szImage = _ids.getSize(img);

            int iXRepeat = (int) (Math.ceil(e2d.width / szImage.getWidth()));
            int iYRepeat = (int) (Math.ceil(e2d.height / szImage.getHeight()));
            ImageObserver io = (ImageObserver) _ids.getObserver();
            for (int i = 0; i < iXRepeat; i++) {
                for (int j = 0; j < iYRepeat; j++) {
                    _g2d.drawImage(img, (int) (e2d.x + i * szImage.getWidth()),
                            (int) (e2d.y + j * szImage.getHeight()), io);
                }
            }

            _g2d.setClip(shClip); // RESTORE
        }
    }
}