Search in sources :

Example 16 with Graphics

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

the class ValidWbmpTest method main.

public static void main(String[] args) {
    try {
        String[] formats = { "JPEG", "PNG", "BMP" };
        BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_GRAY);
        Graphics g = img.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, 100, 100);
        g.setColor(Color.black);
        g.fillRect(10, 10, 80, 80);
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName("WBMP").next();
        if (ir == null) {
            throw new RuntimeException("No readers for WBMP format!");
        }
        for (int i = 0; i < formats.length; i++) {
            System.out.println("Test " + formats[i] + " stream...");
            boolean passed = false;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(img, formats[i], baos);
            baos.close();
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ImageInputStream iis = null;
            iis = ImageIO.createImageInputStream(bais);
            ir.setInput(iis);
            try {
                BufferedImage res = ir.read(0);
            } catch (IIOException e) {
                StackTraceElement[] stack = e.getStackTrace();
                if (ir.getClass().getName().equals(stack[0].getClassName()) && "readHeader".equals(stack[0].getMethodName())) {
                    passed = true;
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
            if (!passed) {
                throw new RuntimeException("Test failed!");
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed.");
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IIOException(javax.imageio.IIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) IIOException(javax.imageio.IIOException) Graphics(java.awt.Graphics) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReader(javax.imageio.ImageReader)

Example 17 with Graphics

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

the class bug8057791 method main.

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel(new NimbusLookAndFeel());
        SwingUtilities.invokeAndWait(new Runnable() {

            @Override
            public void run() {
                final int listWidth = 50;
                final int listHeight = 50;
                final int selCellIndex = 0;
                JList<String> list = new JList<String>();
                list.setSize(listWidth, listHeight);
                DefaultListModel<String> listModel = new DefaultListModel<String>();
                listModel.add(selCellIndex, "E");
                list.setModel(listModel);
                list.setSelectedIndex(selCellIndex);
                BufferedImage img = new BufferedImage(listWidth, listHeight, BufferedImage.TYPE_INT_ARGB);
                Graphics g = img.getGraphics();
                list.paint(g);
                g.dispose();
                Rectangle cellRect = list.getCellBounds(selCellIndex, selCellIndex);
                HashSet<Color> cellColors = new HashSet<Color>();
                int uniqueColorIndex = 0;
                for (int x = cellRect.x; x < (cellRect.x + cellRect.width); x++) {
                    for (int y = cellRect.y; y < (cellRect.y + cellRect.height); y++) {
                        Color cellColor = new Color(img.getRGB(x, y), true);
                        if (cellColors.add(cellColor)) {
                            System.err.println(String.format("Cell color #%d: %s", uniqueColorIndex++, cellColor));
                        }
                    }
                }
                Color selForegroundColor = list.getSelectionForeground();
                Color selBackgroundColor = list.getSelectionBackground();
                if (!cellColors.contains(new Color(selForegroundColor.getRGB(), true))) {
                    throw new RuntimeException(String.format("Selected cell is drawn without selection foreground color '%s'.", selForegroundColor));
                }
                if (!cellColors.contains(new Color(selBackgroundColor.getRGB(), true))) {
                    throw new RuntimeException(String.format("Selected cell is drawn without selection background color '%s'.", selBackgroundColor));
                }
            }
        });
    } catch (UnsupportedLookAndFeelException | InterruptedException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }
}
Also used : Color(java.awt.Color) Rectangle(java.awt.Rectangle) DefaultListModel(javax.swing.DefaultListModel) BufferedImage(java.awt.image.BufferedImage) InvocationTargetException(java.lang.reflect.InvocationTargetException) Graphics(java.awt.Graphics) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException) NimbusLookAndFeel(javax.swing.plaf.nimbus.NimbusLookAndFeel) JList(javax.swing.JList) HashSet(java.util.HashSet)

Example 18 with Graphics

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

the class bug7181438 method createBufferedImage.

private static BufferedImage createBufferedImage() {
    final BufferedImage bi = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
    final Graphics bg = bi.getGraphics();
    //Black color and alpha = 0
    bg.setColor(new Color(0, 0, 0, 0));
    bg.fillRect(0, 0, SIZE, SIZE);
    bg.dispose();
    return bi;
}
Also used : Graphics(java.awt.Graphics) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage)

Example 19 with Graphics

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

the class HiDPIRobotScreenCaptureTest method main.

public static void main(String[] args) throws Exception {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception e) {
        return;
    }
    Frame frame = new Frame();
    frame.setBounds(40, 30, 400, 300);
    frame.setUndecorated(true);
    Panel panel = new Panel(new BorderLayout());
    Canvas canvas = new Canvas() {

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            int w = getWidth();
            int h = getHeight();
            g.setColor(COLORS[0]);
            g.fillRect(0, 0, w / 2, h / 2);
            g.setColor(COLORS[1]);
            g.fillRect(w / 2, 0, w / 2, h / 2);
            g.setColor(COLORS[2]);
            g.fillRect(0, h / 2, w / 2, h / 2);
            g.setColor(COLORS[3]);
            g.fillRect(w / 2, h / 2, w / 2, h / 2);
        }
    };
    panel.add(canvas);
    frame.add(panel);
    frame.setVisible(true);
    Robot robot = new Robot();
    robot.waitForIdle();
    Thread.sleep(200);
    Rectangle rect = canvas.getBounds();
    rect.setLocation(canvas.getLocationOnScreen());
    BufferedImage image = robot.createScreenCapture(rect);
    frame.dispose();
    int w = image.getWidth();
    int h = image.getHeight();
    if (w != frame.getWidth() || h != frame.getHeight()) {
        throw new RuntimeException("Wrong image size!");
    }
    if (image.getRGB(w / 4, h / 4) != COLORS[0].getRGB()) {
        throw new RuntimeException("Wrong image color!");
    }
    if (image.getRGB(3 * w / 4, h / 4) != COLORS[1].getRGB()) {
        throw new RuntimeException("Wrong image color!");
    }
    if (image.getRGB(w / 4, 3 * h / 4) != COLORS[2].getRGB()) {
        throw new RuntimeException("Wrong image color!");
    }
    if (image.getRGB(3 * w / 4, 3 * h / 4) != COLORS[3].getRGB()) {
        throw new RuntimeException("Wrong image color!");
    }
}
Also used : Graphics(java.awt.Graphics) Panel(java.awt.Panel) Frame(java.awt.Frame) BorderLayout(java.awt.BorderLayout) Canvas(java.awt.Canvas) Rectangle(java.awt.Rectangle) Robot(java.awt.Robot) BufferedImage(java.awt.image.BufferedImage)

Example 20 with Graphics

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

the class MultiResolutionSplashTest method getScaleFactor.

static float getScaleFactor() {
    final Dialog dialog = new Dialog((Window) null);
    dialog.setSize(100, 100);
    dialog.setModal(true);
    final float[] scaleFactors = new float[1];
    Panel panel = new Panel() {

        @Override
        public void paint(Graphics g) {
            float scaleFactor = 1;
            if (g instanceof SunGraphics2D) {
                scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale();
            }
            scaleFactors[0] = scaleFactor;
            dialog.setVisible(false);
        }
    };
    dialog.add(panel);
    dialog.setVisible(true);
    dialog.dispose();
    return scaleFactors[0];
}
Also used : Graphics(java.awt.Graphics) Panel(java.awt.Panel) Dialog(java.awt.Dialog) SunGraphics2D(sun.java2d.SunGraphics2D)

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