Search in sources :

Example 96 with Graphics

use of java.awt.Graphics in project jdk8u_jdk by JetBrains.

the class ProxyGraphics2D method getBufferedImageCopy.

/**
     * Return a new <code>BufferedImage</code>
     * that contains a copy of the provided
     * <code>Image</code> where its
     * transparent pixels have been replaced by
     * <code>bgcolor</code>. If the new
     * <code>BufferedImage</code> can not be created,
     * probably because the original image has not
     * finished loading, then <code>null</code> is
     * returned.
     */
private BufferedImage getBufferedImageCopy(Image img, Color bgcolor) {
    BufferedImage imageCopy = null;
    int width = img.getWidth(null);
    int height = img.getHeight(null);
    if (width > 0 && height > 0) {
        int imageType;
        /* Try to minimize the depth of the BufferedImage
             * we are about to create by, if possible, making
             * it the same depth as the original image.
             */
        if (img instanceof BufferedImage) {
            BufferedImage bufImage = (BufferedImage) img;
            imageType = bufImage.getType();
        } else {
            imageType = BufferedImage.TYPE_INT_ARGB;
        }
        imageCopy = new BufferedImage(width, height, imageType);
        /* Copy the original image into the new buffer
             * without any transformations.
             * This will replace the transparent pixels
             * in the original with background color.
             */
        Graphics g = imageCopy.createGraphics();
        g.drawImage(img, 0, 0, bgcolor, null);
        g.dispose();
    /* We couldn't get the width or height of the image
         * so just return null.
         */
    } else {
        imageCopy = null;
    }
    return imageCopy;
}
Also used : PrinterGraphics(java.awt.print.PrinterGraphics) Graphics(java.awt.Graphics) BufferedImage(java.awt.image.BufferedImage) Paint(java.awt.Paint)

Example 97 with Graphics

use of java.awt.Graphics in project jdk8u_jdk by JetBrains.

the class NoResizeEventOnDMChangeTest method main.

public static void main(String[] args) {
    final GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    if (!gd.isFullScreenSupported()) {
        System.out.println("Full screen not supported, test passed");
        return;
    }
    DisplayMode dm = gd.getDisplayMode();
    final DisplayMode[] dms = new DisplayMode[2];
    for (DisplayMode dm1 : gd.getDisplayModes()) {
        if (dm1.getWidth() != dm.getWidth() || dm1.getHeight() != dm.getHeight()) {
            dms[0] = dm1;
            break;
        }
    }
    if (dms[0] == null) {
        System.out.println("Test Passed: all DMs have same dimensions");
        return;
    }
    dms[1] = dm;
    Frame f = new Frame() {

        @Override
        public void paint(Graphics g) {
            g.setColor(Color.red);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.green);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        }
    };
    f.setUndecorated(true);
    testFSWindow(gd, dms, f);
    Window w = new Window(f) {

        @Override
        public void paint(Graphics g) {
            g.setColor(Color.magenta);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.cyan);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        }
    };
    testFSWindow(gd, dms, w);
    System.out.println("Test Passed.");
}
Also used : DisplayMode(java.awt.DisplayMode) Graphics(java.awt.Graphics) Window(java.awt.Window) Frame(java.awt.Frame) GraphicsDevice(java.awt.GraphicsDevice)

Example 98 with Graphics

use of java.awt.Graphics in project jdk8u_jdk by JetBrains.

the class OpCompatibleImageTest method createTestImage.

private BufferedImage createTestImage(int type) {
    BufferedImage img = new BufferedImage(100, 100, type);
    Graphics g = img.createGraphics();
    g.setColor(Color.red);
    g.fillRect(0, 0, 100, 100);
    g.dispose();
    return img;
}
Also used : Graphics(java.awt.Graphics) BufferedImage(java.awt.image.BufferedImage)

Example 99 with Graphics

use of java.awt.Graphics in project jdk8u_jdk by JetBrains.

the class HiDPIPropertiesWindowsTest method testScale.

private static void testScale(double scaleX, double scaleY) {
    Dialog dialog = new Dialog((Frame) null, true) {

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            AffineTransform tx = ((Graphics2D) g).getTransform();
            dispose();
            if (scaleX != tx.getScaleX() || scaleY != tx.getScaleY()) {
                throw new RuntimeException(String.format("Wrong scale:" + "[%f, %f] instead of [%f, %f].", tx.getScaleX(), tx.getScaleY(), scaleX, scaleY));
            }
        }
    };
    dialog.setSize(200, 300);
    dialog.setVisible(true);
}
Also used : Graphics(java.awt.Graphics) Dialog(java.awt.Dialog) AffineTransform(java.awt.geom.AffineTransform) Graphics2D(java.awt.Graphics2D)

Example 100 with Graphics

use of java.awt.Graphics in project jdk8u_jdk by JetBrains.

the class DrawImageCoordsTest method main.

public static void main(String[] args) {
    /* Create an image to draw, filled in solid red. */
    BufferedImage srcImg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics srcG = srcImg.createGraphics();
    srcG.setColor(Color.red);
    int w = srcImg.getWidth(null);
    int h = srcImg.getHeight(null);
    srcG.fillRect(0, 0, w, h);
    /* Create a destination image */
    BufferedImage dstImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
    Graphics2D dstG = dstImage.createGraphics();
    /* draw image under a scaling transform that overflows int */
    AffineTransform tx = new AffineTransform(0.5, 0, 0, 0.5, 0, 5.8658460197478485E9);
    dstG.setTransform(tx);
    dstG.drawImage(srcImg, 0, 0, null);
    /* draw image under the same overflowing transform, cancelling
         * out the 0.5 scale on the graphics
         */
    dstG.drawImage(srcImg, 0, 0, 2 * w, 2 * h, null);
    if (Color.red.getRGB() == dstImage.getRGB(w / 2, h / 2)) {
        throw new RuntimeException("Unexpected color: clipping failed.");
    }
    System.out.println("Test Thread Completed");
}
Also used : Graphics(java.awt.Graphics) AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

Graphics (java.awt.Graphics)158 BufferedImage (java.awt.image.BufferedImage)91 Graphics2D (java.awt.Graphics2D)22 Dimension (java.awt.Dimension)20 Color (java.awt.Color)19 File (java.io.File)18 Frame (java.awt.Frame)14 JFrame (javax.swing.JFrame)13 Point (java.awt.Point)12 Rectangle (java.awt.Rectangle)12 Component (java.awt.Component)11 IOException (java.io.IOException)11 JComponent (javax.swing.JComponent)9 FontMetrics (java.awt.FontMetrics)8 GraphicsConfiguration (java.awt.GraphicsConfiguration)8 Insets (java.awt.Insets)8 Panel (java.awt.Panel)8 ImageIcon (javax.swing.ImageIcon)8 GraphicsDevice (java.awt.GraphicsDevice)6 Window (java.awt.Window)6