Java Code Examples for java.awt.geom.AffineTransform#TYPE_UNIFORM_SCALE

The following examples show how to use java.awt.geom.AffineTransform#TYPE_UNIFORM_SCALE . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   File: CachedHiDPIIcon.java    License: Apache License 2.0 6 votes vote down vote up
public static CachedImageKey create(Graphics2D g) {
    final AffineTransform tx = g.getTransform();
    final int txType = tx.getType();
    final double scale;
    if (txType == AffineTransform.TYPE_UNIFORM_SCALE ||
        txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION))
    {
        scale = tx.getScaleX();
    } else {
        scale = 1.0;
    }
    GraphicsConfiguration gconf = g.getDeviceConfiguration();
    /* Always use the same transparency mode for the cached images, so we don't end up with
    one set of cached images for each encountered mode. The TRANSLUCENT mode is the most
    generic one; presumably it should paint OK onto less capable surfaces. */
    ColorModel colorModel = gconf.getColorModel(Transparency.TRANSLUCENT);
    return new CachedImageKey(colorModel, scale);
}
 
Example 2
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 3
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 4
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 5
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 6
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 7
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 8
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 9
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 10
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 11
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 12
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 13
private int getNativePointSize() {
    /* Make a copy of the glyphTX in which we will store the
     * font transform, inverting the devTx if necessary
     */
    double[] mat = new double[4];
    desc.glyphTx.getMatrix(mat);
    fontTx = new AffineTransform(mat);

    /* Now work backwards to get the font transform */
    if (!desc.devTx.isIdentity() &&
        desc.devTx.getType() != AffineTransform.TYPE_TRANSLATION) {
        try {
            invertDevTx = desc.devTx.createInverse();
            fontTx.concatenate(invertDevTx);
        } catch (NoninvertibleTransformException e) {
            e.printStackTrace();
        }
    }

    /* At this point the fontTx may be a simple +ve scale, or it
     * may be something more complex.
     */
    Point2D.Float pt = new Point2D.Float(1f,1f);
    fontTx.deltaTransform(pt, pt);
    double ptSize = Math.abs(pt.y);
    int ttype = fontTx.getType();
    if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
        fontTx.getScaleY() <= 0) {
        /* We need to create an inverse transform that doesn't
         * include the point size (strictly the uniform scale)
         */
        fontTx.scale(1/ptSize, 1/ptSize);
    } else {
        fontTx = null; // no need
    }
    return (int)ptSize;
}
 
Example 14
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 15
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 16
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 17
public static int getModifiedType(AffineTransform at) {
    int type = at.getType();
    // Some of the vector methods can introduce a tiny uniform scale
    // at some angles...
    if ((type & AffineTransform.TYPE_UNIFORM_SCALE) != 0) {
        maxulps = Math.max(maxulps, ulps(at.getDeterminant(), 1.0));
        if (ulps(at.getDeterminant(), 1.0) <= MAX_ULPS) {
            // Really tiny - we will ignore it
            type &= (~AffineTransform.TYPE_UNIFORM_SCALE);
        }
    }
    return type;
}
 
Example 18
NativeStrike(NativeFont nativeFont, FontStrikeDesc desc) {
    super(nativeFont, desc);
    this.nativeFont = nativeFont;


    /* If this is a delegate for bitmaps, we expect to have
     * been invoked only for a simple scale. If that's not
     * true, just bail
     */
    if (nativeFont.isBitmapDelegate) {
        int ttype = desc.glyphTx.getType();
        if ((ttype & ~AffineTransform.TYPE_UNIFORM_SCALE) != 0 ||
            desc.glyphTx.getScaleX() <= 0) {
        numGlyphs = 0;
        return;
        }
    }

    int ptSize = getNativePointSize();
    byte [] nameBytes = nativeFont.getPlatformNameBytes(ptSize);
    double scale = Math.abs(desc.devTx.getScaleX());
    pScalerContext = createScalerContext(nameBytes, ptSize, scale);
    if (pScalerContext == 0L) {
        SunFontManager.getInstance().deRegisterBadFont(nativeFont);
        pScalerContext = createNullScalerContext();
        numGlyphs = 0;
        if (FontUtilities.isLogging()) {
            FontUtilities.getLogger()
                              .severe("Could not create native strike " +
                                      new String(nameBytes));
        }
        return;
    }
    numGlyphs = nativeFont.getMapper().getNumGlyphs();
    this.disposer = new NativeStrikeDisposer(nativeFont, desc,
                                             pScalerContext);
}
 
Example 19
Source Project: netbeans   File: VectorIcon.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public final void paintIcon(Component c, Graphics g0, int x, int y) {
    final Graphics2D g2 = createGraphicsWithRenderingHintsConfigured(g0);
    try {
        // Make sure the subclass can't paint outside its stated dimensions.
        g2.clipRect(x, y, getIconWidth(), getIconHeight());
        g2.translate(x, y);
        /**
         * On HiDPI monitors, the Graphics object will have a default transform that maps
         * logical pixels, like those you'd pass to Graphics.drawLine, to a higher number of
         * device pixels on the screen. For instance, painting a line 10 pixels long on the
         * current Graphics object would actually produce a line 20 device pixels long on a
         * MacOS retina screen, which has a DPI scaling factor of 2.0. On Windows 10, many
         * different scaling factors may be encountered, including non-integral ones such as
         * 1.5. Detect the scaling factor here so we can use it to inform the drawing routines.
         */
        final double scaling;
        final AffineTransform tx = g2.getTransform();
        int txType = tx.getType();
        if (txType == AffineTransform.TYPE_UNIFORM_SCALE ||
            txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION))
        {
            scaling = tx.getScaleX();
        } else {
            // Unrecognized transform type. Don't do any custom scaling handling.
            paintIcon(c, g2, getIconWidth(), getIconHeight(), 1.0);
            return;
        }
        /* When using a non-integral scaling factor, such as 175%, preceding Swing components
        often end up being a non-integral number of device pixels tall or wide. This will cause
        our initial position to be "off the grid" with respect to device pixels, causing blurry
        graphics even if we subsequently take care to use only integral numbers of device pixels
        during painting. Fix this here by consuming a little bit of the top and left of the
        icon's dimensions to offset any error. */
        // The initial position, in device pixels.
        final double previousDevicePosX = tx.getTranslateX();
        final double previousDevicePosY = tx.getTranslateY();
        /* The new, aligned position, after a small portion of the icon's dimensions may have
        been consumed to correct it. */
        final double alignedDevicePosX = Math.ceil(previousDevicePosX);
        final double alignedDevicePosY = Math.ceil(previousDevicePosY);
        // Use the aligned position.
        g2.setTransform(new AffineTransform(
            1, 0, 0, 1, alignedDevicePosX, alignedDevicePosY));
        /* The portion of the icon's dimensions that was consumed to correct any initial
        translation misalignment, in device pixels. May be zero. */
        final double transDeviceAdjX = alignedDevicePosX - previousDevicePosX;
        final double transDeviceAdjY = alignedDevicePosY - previousDevicePosY;
        /* Now calculate the dimensions available for painting, also aligned to an integral
        number of device pixels. */
        final int deviceWidth  = (int) Math.floor(getIconWidth()  * scaling - transDeviceAdjX);
        final int deviceHeight = (int) Math.floor(getIconHeight() * scaling - transDeviceAdjY);
        paintIcon(c, g2, deviceWidth, deviceHeight, scaling);
    } finally {
        g2.dispose();
    }
}
 
Example 20
Source Project: netbeans   File: DPIUnscaledBorder.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void paintBorder(Component c, Graphics g0, int x, int y, int width, int height) {
    final Graphics2D g = (Graphics2D) g0;
    final AffineTransform oldTransform = g.getTransform();
    g.translate(x, y);
    final AffineTransform tx = g.getTransform();
    final int txType = tx.getType();
    /* On fractional DPI scaling factors, such as 150%, a logical pixel position, e.g. (5,0),
    may end up being translated to a non-integral device pixel position, e.g. (7.5, 0). The same
    goes for border thicknesses, which are specified in logical pixels. In this method, we do
    all calculations and painting in device pixels, to avoid rounding errors causing visible
    artifacts. On screens without HiDPI scaling, logical pixel values and device pixel values
    are identical, and always integral (whole number) values. */
    final int deviceWidth;
    final int deviceHeight;
    if (txType == AffineTransform.TYPE_UNIFORM_SCALE ||
        txType == (AffineTransform.TYPE_UNIFORM_SCALE | AffineTransform.TYPE_TRANSLATION))
    {
        // HiDPI scaling is active.
        double scale = tx.getScaleX();
        /* To be completely safe from overpainting the previous adjacent component, we would
        probably need to round up here. But for borders to work properly on JTextField, we
        must round down. And it seems to work fine in the
        EDITOR_TAB_CONTENT_BORDER/VIEW_TAB_CONTENT_BORDER cases as well. */
        int deviceX = (int) tx.getTranslateX();
        int deviceY = (int) tx.getTranslateY();
        /* Rounding down here should guarantee that we do not paint in an area that will be
        painted over by the next adjacent component. Rounding up, or to the nearest integer,
        is confirmed to cause problems. */
        int deviceXend = (int) (tx.getTranslateX() + width * scale);
        int deviceYend = (int) (tx.getTranslateY() + height * scale);
        deviceWidth = deviceXend - deviceX;
        deviceHeight = deviceYend - deviceY;
        /* Deactivate the HiDPI scaling transform so we can do paint operations in the device
        pixel coordinate system instead of in the logical coordinate system. */
        g.setTransform(new AffineTransform(1, 0, 0, 1, deviceX, deviceY));
    } else {
        deviceWidth = width;
        deviceHeight = height;
    }
    delegate.paintBorder(c, g, 0, 0, deviceWidth, deviceHeight);
    g.setTransform(oldTransform);
}