Java Code Examples for java.awt.Graphics2D#getDeviceConfiguration()

The following examples show how to use java.awt.Graphics2D#getDeviceConfiguration() . 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 File: CachedHiDPIIcon.java    From netbeans with 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
Source File: VolatileImageConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void drawBackingStoreImage(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    GraphicsConfiguration gc = g2d.getDeviceConfiguration();
    if (vImg == null ||
        vImg.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE) {
        /* Create a new volatile image */
        vImg = createVolatileImage(PANEL_WIDTH, PANEL_HEIGHT / 3);
    }

    Graphics vImgGraphics = vImg.createGraphics();
    vImgGraphics.setColor(Color.WHITE);
    vImgGraphics.fillRect(0, 0, PANEL_WIDTH, PANEL_HEIGHT / 3);
    drawInfo(vImgGraphics,
             PANEL_X,
             PANEL_Y,
             "Backbuffer",
             Color.MAGENTA);
    g.drawImage(vImg, 0, PANEL_Y * 2, this);
}
 
Example 3
Source File: PDFRenderer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private boolean isBitonal(Graphics2D graphics)
{
    GraphicsConfiguration deviceConfiguration = graphics.getDeviceConfiguration();
    if (deviceConfiguration == null)
    {
        return false;
    }
    GraphicsDevice device = deviceConfiguration.getDevice();
    if (device == null)
    {
        return false;
    }
    DisplayMode displayMode = device.getDisplayMode();
    if (displayMode == null)
    {
        return false;
    }
    return displayMode.getBitDepth() == 1;
}
 
Example 4
Source File: PDFRenderer.java    From sambox with Apache License 2.0 6 votes vote down vote up
private boolean isBitonal(Graphics2D graphics)
{
    GraphicsConfiguration deviceConfiguration = graphics.getDeviceConfiguration();
    if (deviceConfiguration == null)
    {
        return false;
    }
    GraphicsDevice device = deviceConfiguration.getDevice();
    if (device == null)
    {
        return false;
    }
    DisplayMode displayMode = device.getDisplayMode();
    if (displayMode == null)
    {
        return false;
    }
    return displayMode.getBitDepth() == 1;
}
 
Example 5
Source File: Img.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * @param width        The width to create.
 * @param height       The height to create.
 * @param transparency A constant from {@link Transparency}.
 * @return A new {@link Img} of the given size.
 */
public static Img create(int width, int height, int transparency) {
    Graphics2D            g2d = GraphicsUtilities.getGraphics();
    GraphicsConfiguration gc  = g2d.getDeviceConfiguration();
    Img                   img = create(gc, width, height, transparency);
    g2d.dispose();
    return img;
}
 
Example 6
Source File: ImageUtils.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
final public static BufferedImage resizeImage(BufferedImage img, int width, int height) {
    Graphics2D gin = img.createGraphics();
    GraphicsConfiguration gc = gin.getDeviceConfiguration();
    gin.dispose();

    BufferedImage dst = gc.createCompatibleImage(width, height, BufferedImage.BITMASK);
    Graphics2D gr = dst.createGraphics();
    gr.setComposite(AlphaComposite.Src);

    AffineTransform at = AffineTransform.getScaleInstance((double)width/img.getWidth(), (double)height/img.getHeight());
    gr.drawRenderedImage(img,at);
    return dst;
}
 
Example 7
Source File: SimpleCachedPainter.java    From Pixelitor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void paint(Graphics2D g, Object o, int width, int height) {
    GraphicsConfiguration gc = g.getDeviceConfiguration();
    if (cache == null) { // called for the first time or was invalidated
        createAndUseCachedImage(g, gc, width, height);
        return;
    }

    VolatileImage vi = cache.get();
    if (vi == null) { // soft reference collected
        createAndUseCachedImage(g, gc, width, height);
        return;
    }

    if (vi.getWidth() != width || vi.getHeight() != height) { // size changed
        vi.flush();
        createAndUseCachedImage(g, gc, width, height);
        return;
    }
    // at this point we have a cached image with the right size

    int safetyCounter = 0; // to be 100% sure that this is not an infinite loop
    do {
        int valCode = vi.validate(gc); // check before rendering
        if (valCode == VolatileImage.IMAGE_OK) {
            // simplest case, just use the image
            g.drawImage(vi, 0, 0, null);
        } else if (valCode == VolatileImage.IMAGE_RESTORED) {
            // memory loss, but the the image object can be reused
            renderAndUseCachedImage(vi, g, width, height);
        } else if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {
            // surface incompatibility: the image has to be recreated
            vi.flush();
            vi = createAndUseCachedImage(g, gc, width, height);
        }
    } while (vi.contentsLost() && safetyCounter++ < 3); // check after rendering
}