Search in sources :

Example 36 with Graphics

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

the class JPEGsNotAcceleratedTest method showRes.

private static void showRes(String desc, final BufferedImage src) {
    final int w = src.getWidth();
    final int h = src.getHeight();
    Frame f = new Frame(desc + ": dbl-click to exit");
    Component c;
    f.add(c = new Component() {

        public Dimension getPreferredSize() {
            return new Dimension(w, h);
        }

        public void paint(Graphics g) {
            g.clearRect(0, 0, getWidth(), getHeight());
            g.drawImage(src, 0, 0, null);
        }
    });
    c.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() > 1) {
                System.exit(0);
            }
        }
    });
    f.pack();
    synchronized (JPEGsNotAcceleratedTest.class) {
        f.setLocation(frameX, frameY);
        frameX += f.getWidth();
        if ((frameX + f.getWidth()) > f.getGraphicsConfiguration().getBounds().width) {
            frameY += TEST_H;
            if ((frameY + f.getHeight()) > f.getGraphicsConfiguration().getBounds().height) {
                startY += 30;
                startX += 30;
                frameY = startY;
            }
            frameX = startX;
        }
    }
    ;
    f.setVisible(true);
}
Also used : Graphics(java.awt.Graphics) Frame(java.awt.Frame) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) Dimension(java.awt.Dimension) Component(java.awt.Component)

Example 37 with Graphics

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

the class JPEGsNotAcceleratedTest method writeTestImage.

public static void writeTestImage(String fileName) {
    BufferedImage bi = new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    g.setColor(new Color(testRGB));
    g.fillRect(0, 0, TEST_W, TEST_H);
    try {
        System.err.printf("Writing %s\n", fileName);
        if (lowCompression) {
            ImageWriter iw = (ImageWriter) ImageIO.getImageWritersBySuffix("jpeg").next();
            if (iw == null) {
                throw new RuntimeException("No available image writer for " + "jpeg " + " Test failed.");
            }
            File file = new File(fileName);
            ImageOutputStream ios = ImageIO.createImageOutputStream(file);
            iw.setOutput(ios);
            ImageWriteParam param = iw.getDefaultWriteParam();
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(1);
            IIOImage iioImg = new IIOImage(bi, null, null);
            iw.write(null, iioImg, param);
        } else {
            ImageIO.write(bi, "jpeg", new File(fileName));
        }
    } catch (IOException e) {
        System.err.println("Error " + e + " when writing file: " + fileName);
        throw new RuntimeException(e);
    }
}
Also used : Graphics(java.awt.Graphics) Color(java.awt.Color) ImageWriter(javax.imageio.ImageWriter) IOException(java.io.IOException) ImageWriteParam(javax.imageio.ImageWriteParam) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageOutputStream(javax.imageio.stream.ImageOutputStream) IIOImage(javax.imageio.IIOImage)

Example 38 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 39 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 40 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)

Aggregations

Graphics (java.awt.Graphics)217 BufferedImage (java.awt.image.BufferedImage)104 Dimension (java.awt.Dimension)35 Point (java.awt.Point)32 Graphics2D (java.awt.Graphics2D)31 Color (java.awt.Color)28 JPanel (javax.swing.JPanel)22 Insets (java.awt.Insets)21 Rectangle (java.awt.Rectangle)21 JLabel (javax.swing.JLabel)19 BorderLayout (java.awt.BorderLayout)18 File (java.io.File)18 FontMetrics (java.awt.FontMetrics)16 Frame (java.awt.Frame)16 MolecularComponentPattern (org.vcell.model.rbm.MolecularComponentPattern)16 MolecularTypePattern (org.vcell.model.rbm.MolecularTypePattern)16 Component (java.awt.Component)15 Font (java.awt.Font)15 GridBagLayout (java.awt.GridBagLayout)15 JScrollPane (javax.swing.JScrollPane)15