Search in sources :

Example 1 with Graphics2DDelegate

use of com.intellij.ui.Graphics2DDelegate 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)

Aggregations

Graphics2DDelegate (com.intellij.ui.Graphics2DDelegate)1 Graphics2D (java.awt.Graphics2D)1 Shape (java.awt.Shape)1 AffineTransform (java.awt.geom.AffineTransform)1 Area (java.awt.geom.Area)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 BufferedImage (java.awt.image.BufferedImage)1 Nullable (org.jetbrains.annotations.Nullable)1