Example usage for java.awt Graphics2D clip

List of usage examples for java.awt Graphics2D clip

Introduction

In this page you can find the example usage for java.awt Graphics2D clip.

Prototype

public abstract void clip(Shape s);

Source Link

Document

Intersects the current Clip with the interior of the specified Shape and sets the Clip to the resulting intersection.

Usage

From source file:org.apache.fop.render.pdf.pdfbox.PSPDFGraphics2D.java

private BufferedImage getImage(int width, int height, Image img, ImageObserver observer) {
    Dimension size = new Dimension(width, height);
    BufferedImage buf = buildBufferedImage(size);
    Graphics2D g = buf.createGraphics();
    g.setComposite(AlphaComposite.SrcOver);
    g.setBackground(new Color(1, 1, 1, 0));
    g.fillRect(0, 0, width, height);/* w w  w .j a  va  2 s. co  m*/
    g.clip(new Rectangle(0, 0, buf.getWidth(), buf.getHeight()));
    if (!g.drawImage(img, 0, 0, observer)) {
        return null;
    }
    g.dispose();
    return buf;
}

From source file:org.deegree.securityproxy.wms.responsefilter.clipping.SimpleRasterClipper.java

private void executeClipping(Geometry visibleArea, Geometry imgBboxInVisibleAreaCrs, BufferedImage inputImage,
        BufferedImage outputImage) throws FactoryException, TransformException {
    Graphics2D graphics = (Graphics2D) outputImage.getGraphics();
    LiteShape clippingArea = retrieveWorldToScreenClippingArea(visibleArea, imgBboxInVisibleAreaCrs,
            inputImage);/*  ww  w  .  j a v  a2  s . com*/
    graphics.clip(clippingArea);
    graphics.drawImage(inputImage, null, 0, 0);
}

From source file:org.pentaho.reporting.designer.core.editor.report.AbstractRenderComponent.java

protected void paintComponent(final Graphics g) {
    if (fpsCalculator.isActive()) {
        fpsCalculator.tick();//w ww .ja  va 2s.  c o  m
    }

    final Graphics2D g2 = (Graphics2D) g.create();

    g2.setColor(new Color(224, 224, 224));
    g2.fillRect(0, 0, getWidth(), getHeight());

    final int leftBorder = (int) getLeftBorder();
    final int topBorder = (int) getTopBorder();
    final float scaleFactor = getRenderContext().getZoomModel().getZoomAsPercentage();

    // draw the page area ..
    final PageDefinition pageDefinition = getRenderContext().getContextRoot().getPageDefinition();
    final Rectangle2D.Double area = new Rectangle2D.Double(0, 0, pageDefinition.getWidth() * scaleFactor,
            getHeight());
    g2.translate(leftBorder * scaleFactor, topBorder * scaleFactor);
    g2.clip(area);
    g2.setColor(Color.WHITE);
    g2.fill(area);

    // draw the grid (unscaled, but translated)
    final Point2D offset = getOffset();
    if (offset.getX() != 0) {
        // The blackout area is for inline sub-reports and is the area where the subreport is not interested in
        // (so we can clip out).  The blackout area is only visible in the sub-report.
        final Rectangle2D.Double blackoutArea = new Rectangle2D.Double(0, 0, offset.getX() * scaleFactor,
                getHeight());
        g2.setColor(Color.LIGHT_GRAY);
        g2.fill(blackoutArea);
    }
    paintGrid(g2);
    paintElementAlignment(g2);
    g2.dispose();

    final Graphics2D logicalPageAreaG2 = (Graphics2D) g.create();
    // draw the renderable content ...
    logicalPageAreaG2.translate(leftBorder * scaleFactor, topBorder * scaleFactor);
    logicalPageAreaG2.clip(area);
    logicalPageAreaG2.scale(scaleFactor, scaleFactor);

    try {
        final ElementRenderer rendererRoot = getElementRenderer();
        if (rendererRoot != null) {
            if (rendererRoot.draw(logicalPageAreaG2) == false) {
                rendererRoot.handleError(designerContext, renderContext);

                logicalPageAreaG2.scale(1f / scaleFactor, 1f / scaleFactor);
                logicalPageAreaG2.setPaint(Color.WHITE);
                logicalPageAreaG2.fill(area);
            }
        }
    } catch (Exception e) {
        // ignore for now..
        UncaughtExceptionsModel.getInstance().addException(e);
    }

    logicalPageAreaG2.dispose();

    final OverlayRenderer[] renderers = new OverlayRenderer[4];
    renderers[0] = new OverlappingElementOverlayRenderer(getDefaultElement()); // displays the red border for warning
    renderers[1] = new SelectionOverlayRenderer(getDefaultElement());
    renderers[2] = new GuidelineOverlayRenderer(horizontalLinealModel, verticalLinealModel);
    renderers[3] = new SelectionRectangleOverlayRenderer(); // blue box when you shift and drag the region to select multiple
    // elements

    for (int i = 0; i < renderers.length; i++) {
        final OverlayRenderer renderer = renderers[i];
        final Graphics2D selectionG2 = (Graphics2D) g.create();

        renderer.validate(getRenderContext(), scaleFactor, offset);
        renderer.draw(selectionG2,
                new Rectangle2D.Double(getLeftBorder(), getTopBorder(), getWidth(), getHeight()), this);
        selectionG2.dispose();
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.graphics.internal.LogicalPageDrawable.java

/**
 * @param content/* w  w  w  . j av  a  2  s .c  o  m*/
 * @param image
 */
protected boolean drawImage(final RenderableReplacedContentBox content, Image image) {
    final StyleSheet layoutContext = content.getStyleSheet();
    final boolean shouldScale = layoutContext.getBooleanStyleProperty(ElementStyleKeys.SCALE);

    final int x = (int) StrictGeomUtility.toExternalValue(content.getX());
    final int y = (int) StrictGeomUtility.toExternalValue(content.getY());
    final int width = (int) StrictGeomUtility.toExternalValue(content.getWidth());
    final int height = (int) StrictGeomUtility.toExternalValue(content.getHeight());

    if (width == 0 || height == 0) {
        LogicalPageDrawable.logger.debug("Error: Image area is empty: " + content);
        return false;
    }

    WaitingImageObserver obs = new WaitingImageObserver(image);
    obs.waitImageLoaded();
    final int imageWidth = image.getWidth(obs);
    final int imageHeight = image.getHeight(obs);
    if (imageWidth < 1 || imageHeight < 1) {
        return false;
    }

    final Rectangle2D.Double drawAreaBounds = new Rectangle2D.Double(x, y, width, height);
    final AffineTransform scaleTransform;

    final Graphics2D g2;
    if (shouldScale == false) {
        double deviceScaleFactor = 1;
        final double devResolution = metaData.getNumericFeatureValue(OutputProcessorFeature.DEVICE_RESOLUTION);
        if (metaData.isFeatureSupported(OutputProcessorFeature.IMAGE_RESOLUTION_MAPPING)) {
            if (devResolution != 72.0 && devResolution > 0) {
                // Need to scale the device to its native resolution before attempting to draw the image..
                deviceScaleFactor = (72.0 / devResolution);
            }
        }

        final int clipWidth = Math.min(width, (int) Math.ceil(deviceScaleFactor * imageWidth));
        final int clipHeight = Math.min(height, (int) Math.ceil(deviceScaleFactor * imageHeight));
        final ElementAlignment horizontalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.ALIGNMENT);
        final ElementAlignment verticalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.VALIGNMENT);
        final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width,
                clipWidth);
        final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height,
                clipHeight);

        g2 = (Graphics2D) getGraphics().create();
        g2.clip(drawAreaBounds);
        g2.translate(x, y);
        g2.translate(alignmentX, alignmentY);
        g2.clip(new Rectangle2D.Float(0, 0, clipWidth, clipHeight));
        g2.scale(deviceScaleFactor, deviceScaleFactor);

        scaleTransform = null;
    } else {
        g2 = (Graphics2D) getGraphics().create();
        g2.clip(drawAreaBounds);
        g2.translate(x, y);
        g2.clip(new Rectangle2D.Float(0, 0, width, height));

        final double scaleX;
        final double scaleY;

        final boolean keepAspectRatio = layoutContext
                .getBooleanStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO);
        if (keepAspectRatio) {
            final double scaleFactor = Math.min(width / (double) imageWidth, height / (double) imageHeight);
            scaleX = scaleFactor;
            scaleY = scaleFactor;
        } else {
            scaleX = width / (double) imageWidth;
            scaleY = height / (double) imageHeight;
        }

        final int clipWidth = (int) (scaleX * imageWidth);
        final int clipHeight = (int) (scaleY * imageHeight);

        final ElementAlignment horizontalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.ALIGNMENT);
        final ElementAlignment verticalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.VALIGNMENT);
        final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width,
                clipWidth);
        final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height,
                clipHeight);

        g2.translate(alignmentX, alignmentY);

        final Object contentCached = content.getContent().getContentCached();
        if (contentCached instanceof Image) {
            image = (Image) contentCached;
            scaleTransform = null;
        } else if (metaData.isFeatureSupported(OutputProcessorFeature.PREFER_NATIVE_SCALING) == false) {
            image = RenderUtility.scaleImage(image, clipWidth, clipHeight,
                    RenderingHints.VALUE_INTERPOLATION_BICUBIC, true);
            content.getContent().setContentCached(image);
            obs = new WaitingImageObserver(image);
            obs.waitImageLoaded();
            scaleTransform = null;
        } else {
            scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
        }
    }

    while (g2.drawImage(image, scaleTransform, obs) == false) {
        obs.waitImageLoaded();
        if (obs.isError()) {
            LogicalPageDrawable.logger.warn("Error while loading the image during the rendering.");
            break;
        }
    }

    g2.dispose();
    return true;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.graphics.internal.LogicalPageDrawable.java

protected boolean drawDrawable(final RenderableReplacedContentBox content, final Graphics2D g2,
        final DrawableWrapper d) {
    final double x = StrictGeomUtility.toExternalValue(content.getX());
    final double y = StrictGeomUtility.toExternalValue(content.getY());
    final double width = StrictGeomUtility.toExternalValue(content.getWidth());
    final double height = StrictGeomUtility.toExternalValue(content.getHeight());

    if ((width < 0 || height < 0) || (width == 0 && height == 0)) {
        return false;
    }/*from w w w  . j  a  v a 2  s.  com*/

    final Graphics2D clone = (Graphics2D) g2.create();

    final StyleSheet styleSheet = content.getStyleSheet();
    final Object attribute = styleSheet.getStyleProperty(ElementStyleKeys.ANTI_ALIASING);
    if (attribute != null) {
        if (Boolean.TRUE.equals(attribute)) {
            clone.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        } else if (Boolean.FALSE.equals(attribute)) {
            clone.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        }

    }
    if (RenderUtility.isFontSmooth(styleSheet, metaData)) {
        clone.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    } else {
        clone.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    }

    if (strictClipping == false) {
        final double extraPadding;
        final Object o = styleSheet.getStyleProperty(ElementStyleKeys.STROKE);
        if (o instanceof BasicStroke) {
            final BasicStroke stroke = (BasicStroke) o;
            extraPadding = stroke.getLineWidth() / 2.0;
        } else {
            extraPadding = 0.5;
        }

        final Rectangle2D.Double clipBounds = new Rectangle2D.Double(x - extraPadding, y - extraPadding,
                width + 2 * extraPadding, height + 2 * extraPadding);

        clone.clip(clipBounds);
        clone.translate(x, y);
    } else {
        final Rectangle2D.Double clipBounds = new Rectangle2D.Double(x, y, width + 1, height + 1);

        clone.clip(clipBounds);
        clone.translate(x, y);
    }
    configureGraphics(styleSheet, clone);
    configureStroke(styleSheet, clone);
    final Rectangle2D.Double bounds = new Rectangle2D.Double(0, 0, width, height);
    d.draw(clone, bounds);
    clone.dispose();
    return true;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfLogicalPageDrawable.java

protected boolean drawImage(final RenderableReplacedContentBox content, final Image image,
        final com.lowagie.text.Image itextImage) {
    final StyleSheet layoutContext = content.getStyleSheet();
    final boolean shouldScale = layoutContext.getBooleanStyleProperty(ElementStyleKeys.SCALE);

    final int x = (int) StrictGeomUtility.toExternalValue(content.getX());
    final int y = (int) StrictGeomUtility.toExternalValue(content.getY());
    final int width = (int) StrictGeomUtility.toExternalValue(content.getWidth());
    final int height = (int) StrictGeomUtility.toExternalValue(content.getHeight());

    if (width == 0 || height == 0) {
        PdfLogicalPageDrawable.logger.debug("Error: Image area is empty: " + content);
        return false;
    }//  w w w .  jav a  2  s  .  co  m

    final WaitingImageObserver obs = new WaitingImageObserver(image);
    obs.waitImageLoaded();
    final int imageWidth = image.getWidth(obs);
    final int imageHeight = image.getHeight(obs);
    if (imageWidth < 1 || imageHeight < 1) {
        return false;
    }

    final Rectangle2D.Double drawAreaBounds = new Rectangle2D.Double(x, y, width, height);
    final AffineTransform scaleTransform;

    final Graphics2D g2;
    if (shouldScale == false) {
        double deviceScaleFactor = 1;
        final double devResolution = getMetaData()
                .getNumericFeatureValue(OutputProcessorFeature.DEVICE_RESOLUTION);
        if (getMetaData().isFeatureSupported(OutputProcessorFeature.IMAGE_RESOLUTION_MAPPING)) {
            if (devResolution != 72.0 && devResolution > 0) {
                // Need to scale the device to its native resolution before attempting to draw the image..
                deviceScaleFactor = (72.0 / devResolution);
            }
        }

        final int clipWidth = Math.min(width, (int) Math.ceil(deviceScaleFactor * imageWidth));
        final int clipHeight = Math.min(height, (int) Math.ceil(deviceScaleFactor * imageHeight));
        final ElementAlignment horizontalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.ALIGNMENT);
        final ElementAlignment verticalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.VALIGNMENT);
        final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width,
                clipWidth);
        final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height,
                clipHeight);

        g2 = (Graphics2D) getGraphics().create();
        g2.clip(drawAreaBounds);
        g2.translate(x, y);
        g2.translate(alignmentX, alignmentY);
        g2.clip(new Rectangle2D.Float(0, 0, clipWidth, clipHeight));
        g2.scale(deviceScaleFactor, deviceScaleFactor);

        scaleTransform = null;
    } else {
        g2 = (Graphics2D) getGraphics().create();
        g2.clip(drawAreaBounds);
        g2.translate(x, y);
        g2.clip(new Rectangle2D.Float(0, 0, width, height));

        final double scaleX;
        final double scaleY;

        final boolean keepAspectRatio = layoutContext
                .getBooleanStyleProperty(ElementStyleKeys.KEEP_ASPECT_RATIO);
        if (keepAspectRatio) {
            final double scaleFactor = Math.min(width / (double) imageWidth, height / (double) imageHeight);
            scaleX = scaleFactor;
            scaleY = scaleFactor;
        } else {
            scaleX = width / (double) imageWidth;
            scaleY = height / (double) imageHeight;
        }

        final int clipWidth = (int) (scaleX * imageWidth);
        final int clipHeight = (int) (scaleY * imageHeight);

        final ElementAlignment horizontalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.ALIGNMENT);
        final ElementAlignment verticalAlignment = (ElementAlignment) layoutContext
                .getStyleProperty(ElementStyleKeys.VALIGNMENT);
        final int alignmentX = (int) RenderUtility.computeHorizontalAlignment(horizontalAlignment, width,
                clipWidth);
        final int alignmentY = (int) RenderUtility.computeVerticalAlignment(verticalAlignment, height,
                clipHeight);

        g2.translate(alignmentX, alignmentY);
        scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    }

    final PdfGraphics2D pdfGraphics2D = (PdfGraphics2D) g2;
    pdfGraphics2D.drawPdfImage(itextImage, image, scaleTransform, null);
    g2.dispose();
    return true;
}

From source file:org.pentaho.reporting.engine.classic.extensions.modules.sbarcodes.BarcodeWrapper.java

public void draw(final Graphics2D g2, final Rectangle2D bounds) {
    final Graphics2D gr2 = (Graphics2D) g2.create();
    try {/*from w  w w.  j  a  va 2s  .  com*/
        gr2.clip(bounds);
        if (scale) {
            final Dimension size = barcode.getPreferredSize();
            final double horzScale = bounds.getWidth() / size.getWidth();
            final double vertScale = bounds.getHeight() / size.getHeight();
            if (keepAspectRatio) {
                final double scale = Math.min(horzScale, vertScale);
                gr2.scale(scale, scale);
            } else {
                gr2.scale(horzScale, vertScale);
            }
        }
        barcode.draw(gr2, (int) bounds.getX(), (int) bounds.getY());
    } catch (OutputException e) {
        logger.error("Unable to draw barcode element", e);
    } finally {
        gr2.dispose();
    }

}