Example usage for java.awt.geom AffineTransform scale

List of usage examples for java.awt.geom AffineTransform scale

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform scale.

Prototype

@SuppressWarnings("fallthrough")
public void scale(double sx, double sy) 

Source Link

Document

Concatenates this transform with a scaling transformation.

Usage

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

private RenderedImage getMask(RenderedImage img, Dimension targetDim) {
    ColorModel cm = img.getColorModel();
    if (cm.hasAlpha()) {
        BufferedImage alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Raster raster = img.getData();
        GraphicsUtil.copyBand(raster, cm.getNumColorComponents(), alpha.getRaster(), 0);

        BufferedImageOp op1 = new LookupOp(new ByteLookupTable(0, THRESHOLD_TABLE), null);
        BufferedImage alphat = op1.filter(alpha, null);

        BufferedImage mask;//from  w  w  w .  ja v  a2 s . c om
        if (true) {
            mask = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_BYTE_BINARY);
        } else {
            byte[] arr = { (byte) 0, (byte) 0xff };
            ColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr);
            WritableRaster wraster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, targetDim.width,
                    targetDim.height, 1, 1, null);
            mask = new BufferedImage(colorModel, wraster, false, null);
        }

        Graphics2D g2d = mask.createGraphics();
        try {
            AffineTransform at = new AffineTransform();
            double sx = targetDim.getWidth() / img.getWidth();
            double sy = targetDim.getHeight() / img.getHeight();
            at.scale(sx, sy);
            g2d.drawRenderedImage(alphat, at);
        } finally {
            g2d.dispose();
        }
        /*
        try {
        BatchDiffer.saveAsPNG(alpha, new java.io.File("D:/out-alpha.png"));
        BatchDiffer.saveAsPNG(mask, new java.io.File("D:/out-mask.png"));
        } catch (IOException e) {
        e.printStackTrace();
        }*/
        return mask;
    } else {
        return null;
    }
}

From source file:net.sf.jasperreports.swing.JRViewerPanel.java

protected Image getPageErrorImage() {
    PrintPageFormat pageFormat = viewerContext.getPageFormat();
    Image image = new BufferedImage((int) (pageFormat.getPageWidth() * realZoom) + 1,
            (int) (pageFormat.getPageHeight() * realZoom) + 1, BufferedImage.TYPE_INT_RGB);

    AffineTransform transform = new AffineTransform();
    transform.scale(realZoom, realZoom);

    Graphics2D grx = (Graphics2D) image.getGraphics();
    try {//from www  . j  ava  2  s.c  o m
        grx.transform(transform);

        drawPageError(grx);
    } finally {
        grx.dispose();
    }

    return image;
}

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

/**
 * Render the font using the Glyph2D interface.
 * /*  w  ww. j  a v  a 2s  .  c  o m*/
 * @param glyph2D the Glyph2D implementation provided a GeneralPath for each glyph
 * @param font the font
 * @param code character code
 * @param displacement the glyph's displacement (advance)
 * @param at the transformation
 * @throws IOException if something went wrong
 */
private void drawGlyph2D(Glyph2D glyph2D, PDFont font, int code, Vector displacement, AffineTransform at)
        throws IOException {
    PDGraphicsState state = getGraphicsState();
    RenderingMode renderingMode = state.getTextState().getRenderingMode();

    GeneralPath path = glyph2D.getPathForCharacterCode(code);
    if (path != null) {
        // stretch non-embedded glyph if it does not match the width contained in the PDF
        if (!font.isEmbedded()) {
            float fontWidth = font.getWidthFromFont(code);
            if (fontWidth > 0 && // ignore spaces
                    Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001) {
                float pdfWidth = displacement.getX() * 1000;
                at.scale(pdfWidth / fontWidth, 1);
            }
        }

        // render glyph
        Shape glyph = at.createTransformedShape(path);

        if (renderingMode.isFill()) {
            graphics.setComposite(state.getNonStrokingJavaComposite());
            graphics.setPaint(getNonStrokingPaint());
            setClip();
            graphics.fill(glyph);
        }

        if (renderingMode.isStroke()) {
            graphics.setComposite(state.getStrokingJavaComposite());
            graphics.setPaint(getStrokingPaint());
            graphics.setStroke(getStroke());
            setClip();
            graphics.draw(glyph);
        }

        if (renderingMode.isClip()) {
            textClippingArea.add(new Area(glyph));
        }
    }
}

From source file:statechum.analysis.learning.Visualiser.java

protected static PluggableRenderer constructRenderer(Graph g, final LayoutOptions options) {
    final LayoutOptions graphLayoutOptions = options != null ? options : new LayoutOptions();

    PluggableRenderer r = new PluggableRenderer() {
        /**/*from   w w w  . j av a2 s. c om*/
         * Draws the edge <code>e</code>, whose endpoints are at <code>(x1,y1)</code>
         * and <code>(x2,y2)</code>, on the graphics context <code>g</code>.
         * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code> instance
         * is scaled in the x-direction so that its width is equal to the distance between
         * <code>(x1,y1)</code> and <code>(x2,y2)</code>.
         */
        @Override
        protected void drawSimpleEdge(Graphics2D g2d, Edge e, int x1, int y1, int x2, int y2) {
            final Vertex v1 = (Vertex) e.getEndpoints().getFirst();
            final Vertex v2 = (Vertex) e.getEndpoints().getSecond();
            boolean isLoop = v1.equals(v2);
            final Shape s2 = vertexShapeFunction.getShape(v2);
            Shape edgeShape = edgeShapeFunction.getShape(e);
            final double dx = x2 - x1;
            final double dy = y2 - y1;

            boolean edgeHit = true;
            boolean arrowHit = true;
            Rectangle deviceRectangle = null;
            if (screenDevice != null) {
                Dimension d = screenDevice.getSize();
                if (d.width <= 0 || d.height <= 0) {
                    d = screenDevice.getPreferredSize();
                }
                deviceRectangle = new Rectangle(0, 0, d.width, d.height);
            }

            String label = edgeStringer.getLabel(e);
            assert (label != null);
            Component labelComponent = prepareRenderer(graphLabelRenderer, label, isPicked(e), e);
            Dimension d = labelComponent.getPreferredSize();
            Rectangle2D EdgeShapeBoundaries = edgeShape.getBounds2D();
            AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);
            double yMin = 0, yMax = 0;
            double thetaRadians = 0;
            if (isLoop) {
                // this is a self-loop. scale it is larger than the vertex
                // it decorates and translate it so that its nadir is
                // at the center of the vertex.
                int edgeIndex = ParallelEdgeIndexSingleton.getInstance().getIndex(e);
                Rectangle2D s2Bounds = s2.getBounds2D();
                double scaleBy = 1 + (graphLayoutOptions.scaleLines - 1) * 1. / 3.;
                double translation = s2Bounds.getHeight() * (1. / 4. + edgeIndex / 4.);

                xform.translate(0, -scaleBy * translation);
                xform.scale(scaleBy * s2Bounds.getWidth(), scaleBy * s2Bounds.getHeight());
                yMin = scaleBy * (EdgeShapeBoundaries.getMinY() * s2Bounds.getHeight()) - translation;
                yMax = scaleBy * (EdgeShapeBoundaries.getMaxY() * s2Bounds.getHeight()) - translation;

            } else {
                // this is a normal edge. Rotate it to the angle between
                // vertex endpoints, then scale it to the distance between
                // the vertices
                thetaRadians = Math.atan2(dy, dx);
                double dist = Math.sqrt(dx * dx + dy * dy);
                xform.rotate(thetaRadians);
                xform.scale(dist, 1.0);
                yMin = EdgeShapeBoundaries.getMinY();
                yMax = EdgeShapeBoundaries.getMaxY();
            }

            edgeShape = xform.createTransformedShape(edgeShape);
            // Debug code
            /*
            if (!isLoop)
            {
            g2d.setPaint(new Color( 250, 250, 0));
               AffineTransform rect = AffineTransform.getTranslateInstance(x1, y1+yMin);
               rect.rotate(thetaRadians);
               g2d.fill(rect.createTransformedShape(
              new Rectangle(0,0,(int)Math.sqrt(dx*dx + dy*dy),(int)(yMax-yMin))));
            }
            else
            {
            g2d.setPaint(new Color( 100, 250, 0));
            AffineTransform rect = AffineTransform.getTranslateInstance(x1-s2.getBounds2D().getWidth()/2, y1+yMin);
               rect.rotate(thetaRadians);
               g2d.fill(rect.createTransformedShape(
              new Rectangle(0,0,(int)s2.getBounds2D().getWidth(),(int)(yMax-yMin))));
            }*/

            edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle);

            if (edgeHit == true) {
                Paint oldPaint = g2d.getPaint();

                // get Paints for filling and drawing
                // (filling is done first so that drawing and label use same Paint)
                Paint fill_paint = edgePaintFunction.getFillPaint(e);
                if (fill_paint != null) {
                    g2d.setPaint(fill_paint);
                    g2d.fill(edgeShape);
                }
                Paint draw_paint = edgePaintFunction.getDrawPaint(e);
                if (draw_paint != null) {
                    g2d.setPaint(draw_paint);
                    g2d.draw(edgeShape);
                }

                double scalex = g2d.getTransform().getScaleX();
                double scaley = g2d.getTransform().getScaleY();
                // see if arrows are too small to bother drawing
                if (scalex < .3 || scaley < .3)
                    return;

                if (edgeArrowPredicate.evaluate(e)) {

                    Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond());
                    AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2);
                    destVertexShape = xf.createTransformedShape(destVertexShape);

                    arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle);
                    if (arrowHit) {

                        AffineTransform at;
                        if (edgeShape instanceof GeneralPath)
                            at = getArrowTransform((GeneralPath) edgeShape, destVertexShape);
                        else
                            at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape);
                        if (at == null)
                            return;
                        Shape arrow = edgeArrowFunction.getArrow(e);
                        arrow = at.createTransformedShape(arrow);
                        // note that arrows implicitly use the edge's draw paint
                        g2d.fill(arrow);
                    }
                    assert !(e instanceof UndirectedEdge);
                }

                // For difference visualisation only
                boolean labelBelow = false;
                if (graphLayoutOptions.showDIFF && (draw_paint == null
                        || draw_paint instanceof Color && ((Color) draw_paint).equals(Color.BLACK)))
                    labelBelow = true;

                // Now draw the label.
                double xLabel = 0, yLabel = 0, xa = 0, ya = 0, rotation = thetaRadians;
                if (isLoop) {
                    double displacementY = labelBelow ? -yMin + d.height : -yMin + d.height,
                            displacementX = d.width / 2;
                    xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians);
                    ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians);
                    xLabel = xa - displacementX * Math.cos(thetaRadians);
                    yLabel = ya - displacementX * Math.sin(thetaRadians);
                } else if (dx < 0) {
                    double displacementY = labelBelow ? yMax - d.height : (-yMax - d.height),
                            displacementX = d.width / 2;
                    xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians);
                    ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians);
                    xLabel = xa + displacementX * Math.cos(thetaRadians);
                    yLabel = ya + displacementX * Math.sin(thetaRadians);
                    rotation = thetaRadians + Math.PI;
                } else {
                    double displacementY = labelBelow ? yMax : -yMax, displacementX = d.width / 2;
                    xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians);
                    ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians);
                    xLabel = xa - displacementX * Math.cos(thetaRadians);
                    yLabel = ya - displacementX * Math.sin(thetaRadians);
                }

                AffineTransform old = g2d.getTransform();
                AffineTransform labelTransform = new AffineTransform();
                // Debug code: 
                //g2d.drawLine((int)(x1+dx/2), (int)(y1+dy/2), (int)(xa), (int)(ya));g2d.drawLine((int)(xa), (int)(ya), (int)(xLabel), (int)(yLabel));
                labelTransform.translate(xLabel, yLabel);
                labelTransform.rotate(rotation);
                g2d.setTransform(labelTransform);
                rendererPane.paintComponent(g2d, labelComponent, screenDevice, 0, 0, d.width, d.height, true);
                g2d.setTransform(old);

                // restore old paint
                g2d.setPaint(oldPaint);
            } // if edgeHit == true
        }
    };
    r = labelEdges(g, r, graphLayoutOptions);
    r = labelVertices(r, g, graphLayoutOptions);
    r.setVertexIncludePredicate(new Predicate() {
        @Override
        public boolean evaluate(Object object) {
            if (!graphLayoutOptions.showIgnored && graphLayoutOptions.ignoredStates != null
                    && graphLayoutOptions.ignoredStates.contains(object.toString()))
                return false;

            if (graphLayoutOptions.showNegatives)
                return true;
            else
                return DeterministicDirectedSparseGraph.isAccept((Vertex) object);
        }
    });
    return r;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Draws the edge <code>e</code>, whose endpoints are at <code>(x1,y1)</code>
 * and <code>(x2,y2)</code>, on the graphics context <code>g</code>.
 * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code> instance
 * is scaled in the x-direction so that its width is equal to the distance between
 * <code>(x1,y1)</code> and <code>(x2,y2)</code>.
 *///from   w  ww  . j ava  2  s .co  m
protected void drawSimpleEdge(Graphics2D g, Edge e, int x1, int y1, int x2, int y2) {
    Pair endpoints = e.getEndpoints();
    Vertex v1 = (Vertex) endpoints.getFirst();
    Vertex v2 = (Vertex) endpoints.getSecond();
    boolean isLoop = v1.equals(v2);
    Shape s2 = vertexShapeFunction.getShape(v2);
    Shape edgeShape = edgeShapeFunction.getShape(e);

    boolean edgeHit = true;
    boolean arrowHit = true;
    Rectangle deviceRectangle = null;
    if (screenDevice != null) {
        Dimension d = screenDevice.getSize();
        if (d.width <= 0 || d.height <= 0) {
            d = screenDevice.getPreferredSize();
        }
        deviceRectangle = new Rectangle(0, 0, d.width, d.height);
    }

    AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);

    if (isLoop) {
        // this is a self-loop. scale it is larger than the vertex
        // it decorates and translate it so that its nadir is
        // at the center of the vertex.
        Rectangle2D s2Bounds = s2.getBounds2D();
        xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight());
        xform.translate(0, -edgeShape.getBounds2D().getWidth() / 2);
    } else {
        // this is a normal edge. Rotate it to the angle between
        // vertex endpoints, then scale it to the distance between
        // the vertices
        float dx = x2 - x1;
        float dy = y2 - y1;
        float thetaRadians = (float) Math.atan2(dy, dx);
        xform.rotate(thetaRadians);
        float dist = (float) Math.sqrt(dx * dx + dy * dy);
        xform.scale(dist, 1.0);
    }

    edgeShape = xform.createTransformedShape(edgeShape);

    edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle);

    if (edgeHit == true) {

        Paint oldPaint = g.getPaint();

        // get Paints for filling and drawing
        // (filling is done first so that drawing and label use same Paint)
        Paint fill_paint = edgePaintFunction.getFillPaint(e);
        if (fill_paint != null) {
            g.setPaint(fill_paint);
            g.fill(edgeShape);
        }
        Paint draw_paint = edgePaintFunction.getDrawPaint(e);
        if (draw_paint != null) {
            g.setPaint(draw_paint);
            g.draw(edgeShape);
        }

        float scalex = (float) g.getTransform().getScaleX();
        float scaley = (float) g.getTransform().getScaleY();
        // see if arrows are too small to bother drawing
        if (scalex < .3 || scaley < .3)
            return;

        if (edgeArrowPredicate.evaluate(e)) {

            Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond());
            AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2);
            destVertexShape = xf.createTransformedShape(destVertexShape);

            arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle);
            if (arrowHit) {

                AffineTransform at;
                if (edgeShape instanceof GeneralPath)
                    at = getArrowTransform((GeneralPath) edgeShape, destVertexShape);
                else
                    at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape);
                if (at == null)
                    return;
                Shape arrow = edgeArrowFunction.getArrow(e);
                arrow = at.createTransformedShape(arrow);
                // note that arrows implicitly use the edge's draw paint
                g.fill(arrow);
            }
            if (e instanceof UndirectedEdge) {
                Shape vertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getFirst());
                xf = AffineTransform.getTranslateInstance(x1, y1);
                vertexShape = xf.createTransformedShape(vertexShape);

                arrowHit = viewTransformer.transform(vertexShape).intersects(deviceRectangle);

                if (arrowHit) {
                    AffineTransform at;
                    if (edgeShape instanceof GeneralPath)
                        at = getReverseArrowTransform((GeneralPath) edgeShape, vertexShape, !isLoop);
                    else
                        at = getReverseArrowTransform(new GeneralPath(edgeShape), vertexShape, !isLoop);
                    if (at == null)
                        return;
                    Shape arrow = edgeArrowFunction.getArrow(e);
                    arrow = at.createTransformedShape(arrow);
                    g.fill(arrow);
                }
            }
        }
        // use existing paint for text if no draw paint specified
        if (draw_paint == null)
            g.setPaint(oldPaint);
        String label = edgeStringer.getLabel(e);
        if (label != null) {
            labelEdge(g, e, label, x1, x2, y1, y2);
        }

        // restore old paint
        g.setPaint(oldPaint);
    }
}

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

/**
 * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver)
 *///from  w  ww .  j a va2 s . c o  m
@Override
public boolean drawImage(final Image img, final int x, final int y, final int width, final int height,
        final Color bgcolor, final ImageObserver observer) {
    waitForImage(img);
    final double scalex = width / (double) img.getWidth(observer);
    final double scaley = height / (double) img.getHeight(observer);
    final AffineTransform tx = AffineTransform.getTranslateInstance(x, y);
    tx.scale(scalex, scaley);
    return drawImage(img, null, tx, bgcolor, observer);
}

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

/**
 * @see Graphics#drawImage(Image, int, int, int, int, int, int, int, int, Color, ImageObserver)
 *///from w  w  w . j  av  a2 s .  co m
@Override
public boolean drawImage(final Image img, final int dx1, final int dy1, final int dx2, final int dy2,
        final int sx1, final int sy1, final int sx2, final int sy2, final Color bgcolor,
        final ImageObserver observer) {
    waitForImage(img);
    final double dwidth = (double) dx2 - dx1;
    final double dheight = (double) dy2 - dy1;
    final double swidth = (double) sx2 - sx1;
    final double sheight = (double) sy2 - sy1;

    // if either width or height is 0, then there is nothing to draw
    if (dwidth == 0 || dheight == 0 || swidth == 0 || sheight == 0) {
        return true;
    }

    final double scalex = dwidth / swidth;
    final double scaley = dheight / sheight;

    final double transx = sx1 * scalex;
    final double transy = sy1 * scaley;
    final AffineTransform tx = AffineTransform.getTranslateInstance(dx1 - transx, dy1 - transy);
    tx.scale(scalex, scaley);

    final BufferedImage mask = new BufferedImage(img.getWidth(observer), img.getHeight(observer),
            BufferedImage.TYPE_BYTE_BINARY);
    final Graphics g = mask.getGraphics();
    g.fillRect(sx1, sy1, (int) swidth, (int) sheight);
    drawImage(img, mask, tx, null, observer);
    g.dispose();
    return true;
}

From source file:org.dwfa.ace.graph.AceGraphRenderer.java

/**
 * Draws the edge <code>e</code>, whose endpoints are at
 * <code>(x1,y1)</code> and <code>(x2,y2)</code>, on the graphics context
 * <code>g</code>.// www . j  a  v a  2 s  . c  o m
 * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code>
 * instance
 * is scaled in the x-direction so that its width is equal to the distance
 * between <code>(x1,y1)</code> and <code>(x2,y2)</code>.
 */
protected void drawSimpleEdge(Graphics2D g, Edge e, int x1, int y1, int x2, int y2) {
    Pair endpoints = e.getEndpoints();
    Vertex v1 = (Vertex) endpoints.getFirst();
    Vertex v2 = (Vertex) endpoints.getSecond();
    boolean isLoop = v1.equals(v2);
    Shape s2 = vertexShapeFunction.getShape(v2);
    Shape edgeShape = edgeShapeFunction.getShape(e);

    boolean edgeHit = true;
    boolean arrowHit = true;
    Rectangle deviceRectangle = null;
    if (screenDevice != null) {
        Dimension d = screenDevice.getSize();
        if (d.width <= 0 || d.height <= 0) {
            d = screenDevice.getPreferredSize();
        }
        deviceRectangle = new Rectangle(0, 0, d.width, d.height);
    }

    AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);

    if (isLoop) {
        // this is a self-loop. scale it is larger than the vertex
        // it decorates and translate it so that its nadir is
        // at the center of the vertex.
        Rectangle2D s2Bounds = s2.getBounds2D();
        xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight());
        xform.translate(0, -edgeShape.getBounds2D().getWidth() / 2);
    } else {
        // this is a normal edge. Rotate it to the angle between
        // vertex endpoints, then scale it to the distance between
        // the vertices
        float dx = x2 - x1;
        float dy = y2 - y1;
        float thetaRadians = (float) Math.atan2(dy, dx);
        xform.rotate(thetaRadians);
        float dist = (float) Math.sqrt(dx * dx + dy * dy);
        xform.scale(dist, 1.0);
    }

    edgeShape = xform.createTransformedShape(edgeShape);

    if (deviceRectangle == null) {
        edgeHit = false;
    } else {
        edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle);
    }

    if (edgeHit == true) {

        Paint oldPaint = g.getPaint();

        // get Paints for filling and drawing
        // (filling is done first so that drawing and label use same Paint)
        Paint fill_paint = edgePaintFunction.getFillPaint(e);
        if (fill_paint != null) {
            g.setPaint(fill_paint);
            g.fill(edgeShape);
        }
        Paint draw_paint = edgePaintFunction.getDrawPaint(e);
        if (draw_paint != null) {
            g.setPaint(draw_paint);
            g.draw(edgeShape);
        }

        float scalex = (float) g.getTransform().getScaleX();
        float scaley = (float) g.getTransform().getScaleY();
        // see if arrows are too small to bother drawing
        if (scalex < .3 || scaley < .3)
            return;

        if (edgeArrowPredicate.evaluate(e)) {

            Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond());
            AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2);
            destVertexShape = xf.createTransformedShape(destVertexShape);

            arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle);
            if (arrowHit) {

                AffineTransform at;
                if (edgeShape instanceof GeneralPath)
                    at = getArrowTransform((GeneralPath) edgeShape, destVertexShape);
                else
                    at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape);
                if (at == null)
                    return;
                Shape arrow = edgeArrowFunction.getArrow(e);
                arrow = at.createTransformedShape(arrow);
                // note that arrows implicitly use the edge's draw paint
                g.fill(arrow);
            }
            if (e instanceof UndirectedEdge) {
                Shape vertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getFirst());
                xf = AffineTransform.getTranslateInstance(x1, y1);
                vertexShape = xf.createTransformedShape(vertexShape);

                arrowHit = viewTransformer.transform(vertexShape).intersects(deviceRectangle);

                if (arrowHit) {
                    AffineTransform at;
                    if (edgeShape instanceof GeneralPath)
                        at = getReverseArrowTransform((GeneralPath) edgeShape, vertexShape, !isLoop);
                    else
                        at = getReverseArrowTransform(new GeneralPath(edgeShape), vertexShape, !isLoop);
                    if (at == null)
                        return;
                    Shape arrow = edgeArrowFunction.getArrow(e);
                    arrow = at.createTransformedShape(arrow);
                    g.fill(arrow);
                }
            }
        }
        // use existing paint for text if no draw paint specified
        if (draw_paint == null)
            g.setPaint(oldPaint);
        String label = edgeStringer.getLabel(e);
        if (label != null) {
            labelEdge(g, e, label, x1, x2, y1, y2);
        }

        // restore old paint
        g.setPaint(oldPaint);
    }
}

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

private void setPaint(final boolean invert, final double xoffset, final double yoffset, final boolean fill) {
    if (paint instanceof Color) {
        final Color color = (Color) paint;
        final int alpha = color.getAlpha();
        if (fill) {
            if (alpha != currentFillGState) {
                currentFillGState = alpha;
                PdfGState gs = fillGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setFillOpacity(alpha / 255.00f);
                    fillGState[alpha] = gs;
                }//from  w  w w  .j  a va 2 s  .c  o m
                cb.setGState(gs);
            }
            cb.setColorFill(color);
        } else {
            if (alpha != currentStrokeGState) {
                currentStrokeGState = alpha;
                PdfGState gs = strokeGState[alpha];
                if (gs == null) {
                    gs = new PdfGState();
                    gs.setStrokeOpacity(alpha / 255.0f);
                    strokeGState[alpha] = gs;
                }
                cb.setGState(gs);
            }
            cb.setColorStroke(color);
        }
    } else if (paint instanceof GradientPaint) {
        final GradientPaint gp = (GradientPaint) paint;
        final Point2D p1 = gp.getPoint1();
        transform.transform(p1, p1);
        final Point2D p2 = gp.getPoint2();
        transform.transform(p2, p2);
        final Color c1 = gp.getColor1();
        final Color c2 = gp.getColor2();
        final PdfShading shading = PdfShading.simpleAxial(cb.getPdfWriter(), (float) p1.getX(),
                normalizeY((float) p1.getY()), (float) p2.getX(), normalizeY((float) p2.getY()), c1, c2);
        final PdfShadingPattern pat = new PdfShadingPattern(shading);
        if (fill) {
            cb.setShadingFill(pat);
        } else {
            cb.setShadingStroke(pat);
        }
    } else if (paint instanceof TexturePaint) {
        try {
            final TexturePaint tp = (TexturePaint) paint;
            final BufferedImage img = tp.getImage();
            final Rectangle2D rect = tp.getAnchorRect();
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(image.getWidth(), image.getHeight());
            final AffineTransform inverse = this.normalizeMatrix();
            inverse.translate(rect.getX(), rect.getY());
            inverse.scale(rect.getWidth() / image.getWidth(), -rect.getHeight() / image.getHeight());
            final double[] mx = new double[6];
            inverse.getMatrix(mx);
            pattern.setPatternMatrix((float) mx[0], (float) mx[1], (float) mx[2], (float) mx[3], (float) mx[4],
                    (float) mx[5]);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }

        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    } else {
        try {
            int type = BufferedImage.TYPE_4BYTE_ABGR;
            if (paint.getTransparency() == Transparency.OPAQUE) {
                type = BufferedImage.TYPE_3BYTE_BGR;
            }
            final BufferedImage img = new BufferedImage((int) width, (int) height, type);
            final Graphics2D g = (Graphics2D) img.getGraphics();
            g.transform(transform);
            final AffineTransform inv = transform.createInverse();
            Shape fillRect = new Rectangle2D.Double(0, 0, img.getWidth(), img.getHeight());
            fillRect = inv.createTransformedShape(fillRect);
            g.setPaint(paint);
            g.fill(fillRect);
            if (invert) {
                final AffineTransform tx = new AffineTransform();
                tx.scale(1, -1);
                tx.translate(-xoffset, -yoffset);
                g.drawImage(img, tx, null);
            }
            g.dispose();
            // g = null;
            final com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(img, null);
            final PdfPatternPainter pattern = cb.createPattern(width, height);
            image.setAbsolutePosition(0, 0);
            pattern.addImage(image);
            if (fill) {
                cb.setPatternFill(pattern);
            } else {
                cb.setPatternStroke(pattern);
            }
        } catch (Exception ex) {
            if (fill) {
                cb.setColorFill(Color.gray);
            } else {
                cb.setColorStroke(Color.gray);
            }
        }
    }
}

From source file:neg.JRViewer.java

protected Image getPageErrorImage() {
    Image image = new BufferedImage((int) (jasperPrint.getPageWidth() * realZoom) + 1,
            (int) (jasperPrint.getPageHeight() * realZoom) + 1, BufferedImage.TYPE_INT_RGB);

    Graphics2D grx = (Graphics2D) image.getGraphics();
    AffineTransform transform = new AffineTransform();
    transform.scale(realZoom, realZoom);
    grx.transform(transform);/*from  w  ww.j  a  va 2 s  . c  om*/

    drawPageError((Graphics2D) grx);

    return image;
}