Example usage for java.awt Graphics2D setClip

List of usage examples for java.awt Graphics2D setClip

Introduction

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

Prototype

public abstract void setClip(Shape clip);

Source Link

Document

Sets the current clipping area to an arbitrary clip shape.

Usage

From source file:org.apache.fop.render.pcl.PCLRenderer.java

/**
 * Draws borders. Borders are drawn in-memory and painted as a bitmap.
 * @param borderRect the border rectangle
 * @param bpsBefore the border specification on the before side
 * @param bpsAfter the border specification on the after side
 * @param bpsStart the border specification on the start side
 * @param bpsEnd the border specification on the end side
 *//*from  w  w  w  .  j a  v a 2s . c o  m*/
protected void drawQualityBorders(Rectangle2D.Float borderRect, final BorderProps bpsBefore,
        final BorderProps bpsAfter, final BorderProps bpsStart, final BorderProps bpsEnd) {
    Graphics2DAdapter g2a = getGraphics2DAdapter();
    final Rectangle.Float effBorderRect = new Rectangle2D.Float(0, 0, borderRect.width, borderRect.height);
    final Rectangle paintRect = new Rectangle(Math.round(borderRect.x * 1000f),
            Math.round(borderRect.y * 1000f), (int) Math.floor(borderRect.width * 1000f) + 1,
            (int) Math.floor(borderRect.height * 1000f) + 1);
    //Add one pixel wide safety margin around the paint area
    int pixelWidth = (int) Math.round(UnitConv.in2mpt(1) / userAgent.getTargetResolution());
    final int xoffset = Math.round(-effBorderRect.x * 1000f) + pixelWidth;
    final int yoffset = pixelWidth;
    paintRect.x += xoffset;
    paintRect.y += yoffset;
    paintRect.width += 2 * pixelWidth;
    paintRect.height += 2 * pixelWidth;

    RendererContext rc = createRendererContext(paintRect.x, paintRect.y, paintRect.width, paintRect.height,
            null);
    Map atts = new java.util.HashMap();
    atts.put(ImageHandlerUtil.CONVERSION_MODE, ImageHandlerUtil.CONVERSION_MODE_BITMAP);
    atts.put(SRC_TRANSPARENCY, "true");
    rc.setProperty(RendererContextConstants.FOREIGN_ATTRIBUTES, atts);

    Graphics2DImagePainter painter = new Graphics2DImagePainter() {

        public void paint(Graphics2D g2d, Rectangle2D area) {
            g2d.translate(xoffset, yoffset);
            g2d.scale(1000, 1000);
            float startx = effBorderRect.x;
            float starty = effBorderRect.y;
            float width = effBorderRect.width;
            float height = effBorderRect.height;
            boolean[] b = new boolean[] { (bpsBefore != null), (bpsEnd != null), (bpsAfter != null),
                    (bpsStart != null) };
            if (!b[0] && !b[1] && !b[2] && !b[3]) {
                return;
            }
            float[] bw = new float[] { (b[0] ? bpsBefore.width / 1000f : 0.0f),
                    (b[1] ? bpsEnd.width / 1000f : 0.0f), (b[2] ? bpsAfter.width / 1000f : 0.0f),
                    (b[3] ? bpsStart.width / 1000f : 0.0f) };
            float[] clipw = new float[] { BorderProps.getClippedWidth(bpsBefore) / 1000f,
                    BorderProps.getClippedWidth(bpsEnd) / 1000f, BorderProps.getClippedWidth(bpsAfter) / 1000f,
                    BorderProps.getClippedWidth(bpsStart) / 1000f };
            starty += clipw[0];
            height -= clipw[0];
            height -= clipw[2];
            startx += clipw[3];
            width -= clipw[3];
            width -= clipw[1];

            boolean[] slant = new boolean[] { (b[3] && b[0]), (b[0] && b[1]), (b[1] && b[2]), (b[2] && b[3]) };
            if (bpsBefore != null) {
                //endTextObject();

                float sx1 = startx;
                float sx2 = (slant[0] ? sx1 + bw[3] - clipw[3] : sx1);
                float ex1 = startx + width;
                float ex2 = (slant[1] ? ex1 - bw[1] + clipw[1] : ex1);
                float outery = starty - clipw[0];
                float clipy = outery + clipw[0];
                float innery = outery + bw[0];

                //saveGraphicsState();
                Graphics2D g = (Graphics2D) g2d.create();
                moveTo(sx1, clipy);
                float sx1a = sx1;
                float ex1a = ex1;
                if (bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
                    if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
                        sx1a -= clipw[3];
                    }
                    if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
                        ex1a += clipw[1];
                    }
                    lineTo(sx1a, outery);
                    lineTo(ex1a, outery);
                }
                lineTo(ex1, clipy);
                lineTo(ex2, innery);
                lineTo(sx2, innery);
                closePath();
                //clip();
                g.clip(currentPath);
                currentPath = null;
                Rectangle2D.Float lineRect = new Rectangle2D.Float(sx1a, outery, ex1a - sx1a, innery - outery);
                Java2DRenderer.drawBorderLine(lineRect, true, true, bpsBefore.style, bpsBefore.color, g);
                //restoreGraphicsState();
            }
            if (bpsEnd != null) {
                //endTextObject();

                float sy1 = starty;
                float sy2 = (slant[1] ? sy1 + bw[0] - clipw[0] : sy1);
                float ey1 = starty + height;
                float ey2 = (slant[2] ? ey1 - bw[2] + clipw[2] : ey1);
                float outerx = startx + width + clipw[1];
                float clipx = outerx - clipw[1];
                float innerx = outerx - bw[1];

                //saveGraphicsState();
                Graphics2D g = (Graphics2D) g2d.create();
                moveTo(clipx, sy1);
                float sy1a = sy1;
                float ey1a = ey1;
                if (bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
                    if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
                        sy1a -= clipw[0];
                    }
                    if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
                        ey1a += clipw[2];
                    }
                    lineTo(outerx, sy1a);
                    lineTo(outerx, ey1a);
                }
                lineTo(clipx, ey1);
                lineTo(innerx, ey2);
                lineTo(innerx, sy2);
                closePath();
                //clip();
                g.setClip(currentPath);
                currentPath = null;
                Rectangle2D.Float lineRect = new Rectangle2D.Float(innerx, sy1a, outerx - innerx, ey1a - sy1a);
                Java2DRenderer.drawBorderLine(lineRect, false, false, bpsEnd.style, bpsEnd.color, g);
                //restoreGraphicsState();
            }
            if (bpsAfter != null) {
                //endTextObject();

                float sx1 = startx;
                float sx2 = (slant[3] ? sx1 + bw[3] - clipw[3] : sx1);
                float ex1 = startx + width;
                float ex2 = (slant[2] ? ex1 - bw[1] + clipw[1] : ex1);
                float outery = starty + height + clipw[2];
                float clipy = outery - clipw[2];
                float innery = outery - bw[2];

                //saveGraphicsState();
                Graphics2D g = (Graphics2D) g2d.create();
                moveTo(ex1, clipy);
                float sx1a = sx1;
                float ex1a = ex1;
                if (bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
                    if (bpsStart != null && bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
                        sx1a -= clipw[3];
                    }
                    if (bpsEnd != null && bpsEnd.mode == BorderProps.COLLAPSE_OUTER) {
                        ex1a += clipw[1];
                    }
                    lineTo(ex1a, outery);
                    lineTo(sx1a, outery);
                }
                lineTo(sx1, clipy);
                lineTo(sx2, innery);
                lineTo(ex2, innery);
                closePath();
                //clip();
                g.setClip(currentPath);
                currentPath = null;
                Rectangle2D.Float lineRect = new Rectangle2D.Float(sx1a, innery, ex1a - sx1a, outery - innery);
                Java2DRenderer.drawBorderLine(lineRect, true, false, bpsAfter.style, bpsAfter.color, g);
                //restoreGraphicsState();
            }
            if (bpsStart != null) {
                //endTextObject();

                float sy1 = starty;
                float sy2 = (slant[0] ? sy1 + bw[0] - clipw[0] : sy1);
                float ey1 = sy1 + height;
                float ey2 = (slant[3] ? ey1 - bw[2] + clipw[2] : ey1);
                float outerx = startx - clipw[3];
                float clipx = outerx + clipw[3];
                float innerx = outerx + bw[3];

                //saveGraphicsState();
                Graphics2D g = (Graphics2D) g2d.create();
                moveTo(clipx, ey1);
                float sy1a = sy1;
                float ey1a = ey1;
                if (bpsStart.mode == BorderProps.COLLAPSE_OUTER) {
                    if (bpsBefore != null && bpsBefore.mode == BorderProps.COLLAPSE_OUTER) {
                        sy1a -= clipw[0];
                    }
                    if (bpsAfter != null && bpsAfter.mode == BorderProps.COLLAPSE_OUTER) {
                        ey1a += clipw[2];
                    }
                    lineTo(outerx, ey1a);
                    lineTo(outerx, sy1a);
                }
                lineTo(clipx, sy1);
                lineTo(innerx, sy2);
                lineTo(innerx, ey2);
                closePath();
                //clip();
                g.setClip(currentPath);
                currentPath = null;
                Rectangle2D.Float lineRect = new Rectangle2D.Float(outerx, sy1a, innerx - outerx, ey1a - sy1a);
                Java2DRenderer.drawBorderLine(lineRect, false, false, bpsStart.style, bpsStart.color, g);
                //restoreGraphicsState();
            }
        }

        public Dimension getImageSize() {
            return paintRect.getSize();
        }

    };
    try {
        g2a.paintImage(painter, rc, paintRect.x - xoffset, paintRect.y, paintRect.width, paintRect.height);
    } catch (IOException ioe) {
        handleIOTrouble(ioe);
    }
}

From source file:org.squidy.designer.components.versioning.Versioning.java

@Override
protected void paintShape(PPaintContext paintContext) {
    super.paintShape(paintContext);

    Graphics2D g = paintContext.getGraphics();

    PBounds bounds = getBoundsReference();
    double x = bounds.getX();
    double y = bounds.getY();
    double width = bounds.getWidth();
    double height = bounds.getHeight();

    //      g.setColor(Color.RED);
    //      g.draw(bounds);

    g.setClip(bounds);

    for (int i = 0; i < 7; i++) {
        PAffineTransform transform = new PAffineTransform();
        transform.scale(0.12, 0.12);//w w w . j  a v  a2s. co m
        transform.translate(i * 122, 0);

        paintContext.pushTransform(transform);

        if (!isRenderPrimitive()) {
            versionNode.fullPaint(paintContext);
        }

        if (i == 6) {
            g.setStroke(StrokeUtils.getBasicStroke(5f));
            g.setColor(Color.GRAY);
            if (isRenderPrimitiveRect())
                g.drawRect((int) x, (int) y, 100, (int) (height / 0.12));
            else
                g.drawRoundRect((int) x, (int) y, 100, (int) (height / 0.12), 15, 15);

            g.setColor(COLOR_FILL);
            if (isRenderPrimitiveRect())
                g.fillRect((int) x, (int) y, 100, (int) (height / 0.12));
            else
                g.fillRoundRect((int) x, (int) y, 100, (int) (height / 0.12), 15, 15);
        }

        paintContext.popTransform(transform);
    }

    g.setClip(null);
}

From source file:pl.datamatica.traccar.api.providers.ImageProvider.java

public byte[] getCustomMarker(long id) throws IOException {
    if (!custMarkerCache.containsKey(id)) {
        Picture pic = em.find(Picture.class, id);
        if (pic == null)
            return null;
        Image img = ImageIO.read(new ByteArrayInputStream(pic.getData()));
        float l = 17f / 141, t = 15f / 189, r = 123f / 141, b = 120f / 189;
        int w = emptyMarker.getWidth(null), h = emptyMarker.getHeight(null);
        BufferedImage marker = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        marker.createGraphics().drawImage(emptyMarker, 0, 0, null);
        Graphics2D g = marker.createGraphics();
        g.setClip(new Ellipse2D.Float((int) Math.round(l * w), (int) Math.round(t * h),
                (int) Math.round((r - l) * w), (int) Math.round((b - t) * h)));
        g.drawImage(img, (int) Math.round(l * w), (int) Math.round(t * h), (int) Math.round((r - l) * w),
                (int) Math.round((b - t) * h), null);

        ByteArrayOutputStream boss = new ByteArrayOutputStream();
        ImageIO.write((RenderedImage) marker, "png", boss);
        custMarkerCache.put(id, boss.toByteArray());
    }//from   ww w .  j a  v a 2s. c  o m
    return custMarkerCache.get(id);
}

From source file:savant.view.swing.GraphPane.java

/**
 * Render the background of this GraphPane
 *
 * @param g The graphics object to use/*www . j  a  v a2 s .  co m*/
 */
private void renderBackground(Graphics2D g2, boolean xGridOn, boolean yGridOn) {
    int h = getHeight();
    int w = getWidth();

    // Paint a gradient from top to bottom
    GradientPaint gp0 = new GradientPaint(0, 0, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_TOP), 0,
            h, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_BOTTOM));
    g2.setPaint(gp0);
    g2.fillRect(0, 0, w, h);

    // We don't want the axes stomping on our labels, so make sure the clip excludes them.
    Area clipArea = new Area(new Rectangle(0, 0, w, h));
    Color gridColor = ColourSettings.getColor(ColourKey.AXIS_GRID);

    if (yGridOn) {
        // Smallish font for tick labels.
        Font tickFont = g2.getFont().deriveFont(Font.PLAIN, 9);

        int[] yTicks = MiscUtils.getTickPositions(transformYPixel(getHeight()), transformYPixel(0.0));

        g2.setColor(gridColor);
        g2.setFont(tickFont);
        for (int t : yTicks) {
            double y = transformYPos(t);

            // Skip labels at the top or bottom of the window because they look stupid.
            if (y != 0.0 && y != getHeight()) {
                String s = Integer.toString(t);
                Rectangle2D labelRect = tickFont.getStringBounds(s, g2.getFontRenderContext());
                double baseline = y + labelRect.getHeight() * 0.5 - 2.0;
                g2.drawString(s, 4.0F, (float) baseline);
                clipArea.subtract(new Area(new Rectangle2D.Double(3.0, baseline - labelRect.getHeight() - 1.0,
                        labelRect.getWidth() + 2.0, labelRect.getHeight() + 2.0)));
            }
        }
        g2.setClip(clipArea);
        for (int t2 : yTicks) {
            double y = transformYPos(t2);
            g2.draw(new Line2D.Double(0.0, y, w, y));
        }
    }
    if (xGridOn) {
        Range r = LocationController.getInstance().getRange();
        int[] xTicks = MiscUtils.getTickPositions(r);

        g2.setColor(gridColor);
        for (int t : xTicks) {
            double x = transformXPos(t);
            g2.draw(new Line2D.Double(x, 0, x, h));
        }
    }
    g2.setClip(null);
}

From source file:savant.view.variation.swing.VariantMap.java

@Override
public void paintComponent(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Paint a gradient from top to bottom
    int h = getHeight();
    int w = getWidth();
    GradientPaint gp0 = new GradientPaint(0, 0, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_TOP), 0,
            h, ColourSettings.getColor(ColourKey.GRAPH_PANE_BACKGROUND_BOTTOM));
    g2.setPaint(gp0);/*from w w w  .  j  a v a2s .  c o  m*/
    g2.fillRect(0, 0, w, h);

    List<VariantRecord> data = controller.getData();
    if (data != null && !data.isEmpty()) {
        double boxTop = Double.NaN;
        double boxBottom = Double.NaN;
        Range browseRange = LocationController.getInstance().getRange();

        int participantCount = controller.getParticipantCount();
        unitHeight = (double) h / data.size();
        unitWidth = (double) w / participantCount;

        boolean gappable = unitHeight > GAP_HEIGHT * 2.0;

        ColourScheme cs = new ColourScheme(ColourKey.A, ColourKey.C, ColourKey.G, ColourKey.T,
                ColourKey.INSERTED_BASE, ColourKey.DELETED_BASE, ColourKey.N);
        ColourAccumulator accumulator = new ColourAccumulator(cs);

        double y = 0.0;
        double topGap = 0.0;
        for (int i = 0; i < data.size(); i++) {
            VariantRecord varRec = data.get(i);

            double bottomGap = 0.0;
            if (gappable && i + 1 < data.size()) {
                VariantRecord nextRec = data.get(i + 1);
                if (nextRec.getPosition() - varRec.getPosition() > 1) {
                    bottomGap = GAP_HEIGHT * 0.5;
                }
            }

            if (Double.isNaN(boxTop) && browseRange.getFrom() <= varRec.getPosition()
                    && browseRange.getTo() >= varRec.getPosition()) {
                boxTop = y + topGap;
                boxBottom = boxTop + unitHeight;
            } else if (varRec.getPosition() <= browseRange.getTo()) {
                boxBottom = y + unitHeight - bottomGap;
            }
            double x = 0.0;
            for (int j = 0; j < participantCount; j++) {
                VariantTrackRenderer.accumulateZygoteShapes(varRec.getVariantsForParticipant(j), accumulator,
                        new Rectangle2D.Double(x, y + topGap, unitWidth, unitHeight - topGap - bottomGap));
                x += unitWidth;
            }
            topGap = bottomGap;
            y += unitHeight;
        }
        accumulator.fill(g2);

        if (gappable) {
            drawGapSizes(g2);
        } else {
            // Not enough room to draw gaps, so just label the axes.
            labelVerticalAxis(g2);
        }

        g2.setClip(null);
        g2.setColor(Color.BLUE);
        g2.draw(new Rectangle2D.Double(0.0, boxTop, w - 1.0, boxBottom - boxTop - 1.0));
    }
}