Search in sources :

Example 6 with NoninvertibleTransformException

use of java.awt.geom.NoninvertibleTransformException in project android by JetBrains.

the class FakeImageFactory method getImage.

@Override
public BufferedImage getImage(final int w, final int h) {
    // only in the createGraphics call.
    return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB) {

        @Override
        public Graphics2D createGraphics() {
            myRequestedHeight = h;
            myRequestedWidth = w;
            // TODO: Check if we can stop layoutlib from resetting the transforms.
            final Shape originalClip = myGraphics.getClip();
            final AffineTransform originalTx = ((Graphics2D) myGraphics).getTransform();
            AffineTransform inverse = null;
            if (originalTx != null) {
                try {
                    inverse = originalTx.createInverse();
                } catch (NoninvertibleTransformException e) {
                    LOG.error(e);
                }
            }
            final AffineTransform originalTxInverse = inverse;
            return new Graphics2DDelegate((Graphics2D) myGraphics.create()) {

                @Nullable
                private Shape intersect(@Nullable Shape s1, @Nullable Shape s2) {
                    if (s1 == null || s2 == null) {
                        return s1 == null ? s2 : s1;
                    }
                    Area a1 = new Area(s1);
                    Area a2 = new Area(s2);
                    a1.intersect(a2);
                    return a1;
                }

                @Override
                public void clip(@Nullable Shape s) {
                    if (s == null) {
                        setClip(null);
                        return;
                    }
                    super.clip(s);
                }

                @Override
                public void setClip(@Nullable Shape sh) {
                    try {
                        super.setClip(intersect(getTransform().createInverse().createTransformedShape(originalClip), sh));
                    } catch (NoninvertibleTransformException e) {
                        LOG.error(e);
                    }
                }

                @Override
                public void setTransform(AffineTransform Tx) {
                    if (originalTx != null) {
                        AffineTransform transform = (AffineTransform) originalTx.clone();
                        transform.concatenate(Tx);
                        super.setTransform(transform);
                    } else {
                        super.setTransform(Tx);
                    }
                }

                @Override
                public AffineTransform getTransform() {
                    AffineTransform currentTransform = super.getTransform();
                    if (originalTxInverse != null) {
                        currentTransform.concatenate(originalTxInverse);
                    }
                    return currentTransform;
                }
            };
        }

        @Override
        public int getWidth() {
            return w;
        }

        @Override
        public int getHeight() {
            return h;
        }
    };
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) Area(java.awt.geom.Area) Shape(java.awt.Shape) Graphics2DDelegate(com.intellij.ui.Graphics2DDelegate) AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) Nullable(org.jetbrains.annotations.Nullable) Graphics2D(java.awt.Graphics2D)

Example 7 with NoninvertibleTransformException

use of java.awt.geom.NoninvertibleTransformException in project jdk8u_jdk by JetBrains.

the class SynthPainterImpl method paint.

/**
     * Paint the provided painter using the provided transform at the specified
     * position and size. Handles if g is a non 2D Graphics by painting via a
     * BufferedImage.
     */
private void paint(Painter p, SynthContext ctx, Graphics g, int x, int y, int w, int h, AffineTransform transform) {
    if (p != null) {
        if (g instanceof Graphics2D) {
            Graphics2D gfx = (Graphics2D) g;
            if (transform != null) {
                gfx.transform(transform);
            }
            gfx.translate(x, y);
            p.paint(gfx, ctx.getComponent(), w, h);
            gfx.translate(-x, -y);
            if (transform != null) {
                try {
                    gfx.transform(transform.createInverse());
                } catch (NoninvertibleTransformException e) {
                    // this should never happen as we are in control of all
                    // calls into this method and only ever pass in simple
                    // transforms of rotate, flip and translates
                    e.printStackTrace();
                }
            }
        } else {
            // use image if we are printing to a Java 1.1 PrintGraphics as
            // it is not a instance of Graphics2D
            BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D gfx = img.createGraphics();
            if (transform != null) {
                gfx.transform(transform);
            }
            p.paint(gfx, ctx.getComponent(), w, h);
            gfx.dispose();
            g.drawImage(img, x, y, null);
            img = null;
        }
    }
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) BufferedImage(java.awt.image.BufferedImage)

Example 8 with NoninvertibleTransformException

use of java.awt.geom.NoninvertibleTransformException in project jdk8u_jdk by JetBrains.

the class StandardGlyphVector method pixellate.

/**
     * !!! not used currently, but might be by getPixelbounds?
     */
public void pixellate(FontRenderContext renderFRC, Point2D loc, Point pxResult) {
    if (renderFRC == null) {
        renderFRC = frc;
    }
    // it is a total pain that you have to copy the transform.
    AffineTransform at = renderFRC.getTransform();
    at.transform(loc, loc);
    // but must not behave oddly around zero
    pxResult.x = (int) loc.getX();
    pxResult.y = (int) loc.getY();
    loc.setLocation(pxResult.x, pxResult.y);
    try {
        at.inverseTransform(loc, loc);
    } catch (NoninvertibleTransformException e) {
        throw new IllegalArgumentException("must be able to invert frc transform");
    }
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) AffineTransform(java.awt.geom.AffineTransform)

Example 9 with NoninvertibleTransformException

use of java.awt.geom.NoninvertibleTransformException in project jdk8u_jdk by JetBrains.

the class SunGraphics2D method drawRenderableImage.

public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
    if (img == null) {
        return;
    }
    AffineTransform pipeTransform = transform;
    AffineTransform concatTransform = new AffineTransform(xform);
    concatTransform.concatenate(pipeTransform);
    AffineTransform reverseTransform;
    RenderContext rc = new RenderContext(concatTransform);
    try {
        reverseTransform = pipeTransform.createInverse();
    } catch (NoninvertibleTransformException nte) {
        rc = new RenderContext(pipeTransform);
        reverseTransform = new AffineTransform();
    }
    RenderedImage rendering = img.createRendering(rc);
    drawRenderedImage(rendering, reverseTransform);
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) RenderContext(java.awt.image.renderable.RenderContext) FontRenderContext(java.awt.font.FontRenderContext) AffineTransform(java.awt.geom.AffineTransform) RenderedImage(java.awt.image.RenderedImage)

Example 10 with NoninvertibleTransformException

use of java.awt.geom.NoninvertibleTransformException in project jdk8u_jdk by JetBrains.

the class SunGraphics2D method getImageRegion.

/**
     * Returns a rectangle in image coordinates that may be required
     * in order to draw the given image into the given clipping region
     * through a pair of AffineTransforms.  In addition, horizontal and
     * vertical padding factors for antialising and interpolation may
     * be used.
     */
private static Rectangle getImageRegion(RenderedImage img, Region compClip, AffineTransform transform, AffineTransform xform, int padX, int padY) {
    Rectangle imageRect = new Rectangle(img.getMinX(), img.getMinY(), img.getWidth(), img.getHeight());
    Rectangle result = null;
    try {
        double[] p = new double[8];
        p[0] = p[2] = compClip.getLoX();
        p[4] = p[6] = compClip.getHiX();
        p[1] = p[5] = compClip.getLoY();
        p[3] = p[7] = compClip.getHiY();
        // Inverse transform the output bounding rect
        transform.inverseTransform(p, 0, p, 0, 4);
        xform.inverseTransform(p, 0, p, 0, 4);
        // Determine a bounding box for the inverse transformed region
        double x0, x1, y0, y1;
        x0 = x1 = p[0];
        y0 = y1 = p[1];
        for (int i = 2; i < 8; ) {
            double pt = p[i++];
            if (pt < x0) {
                x0 = pt;
            } else if (pt > x1) {
                x1 = pt;
            }
            pt = p[i++];
            if (pt < y0) {
                y0 = pt;
            } else if (pt > y1) {
                y1 = pt;
            }
        }
        // This is padding for anti-aliasing and such.  It may
        // be more than is needed.
        int x = (int) x0 - padX;
        int w = (int) (x1 - x0 + 2 * padX);
        int y = (int) y0 - padY;
        int h = (int) (y1 - y0 + 2 * padY);
        Rectangle clipRect = new Rectangle(x, y, w, h);
        result = clipRect.intersection(imageRect);
    } catch (NoninvertibleTransformException nte) {
        // Worst case bounds are the bounds of the image.
        result = imageRect;
    }
    return result;
}
Also used : NoninvertibleTransformException(java.awt.geom.NoninvertibleTransformException) Rectangle(java.awt.Rectangle) RadialGradientPaint(java.awt.RadialGradientPaint) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint) LinearGradientPaint(java.awt.LinearGradientPaint) GradientPaint(java.awt.GradientPaint)

Aggregations

NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)19 AffineTransform (java.awt.geom.AffineTransform)17 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)5 BufferedImage (java.awt.image.BufferedImage)3 AlphaComposite (java.awt.AlphaComposite)2 BasicStroke (java.awt.BasicStroke)2 Composite (java.awt.Composite)2 Rectangle (java.awt.Rectangle)2 Shape (java.awt.Shape)2 TexturePaint (java.awt.TexturePaint)2 FontRenderContext (java.awt.font.FontRenderContext)2 Point2D (java.awt.geom.Point2D)2 RenderedImage (java.awt.image.RenderedImage)2 RenderContext (java.awt.image.renderable.RenderContext)2 Graphics2DDelegate (com.intellij.ui.Graphics2DDelegate)1 Color (java.awt.Color)1 GradientPaint (java.awt.GradientPaint)1 Graphics2D (java.awt.Graphics2D)1 LinearGradientPaint (java.awt.LinearGradientPaint)1 Paint (java.awt.Paint)1