Search in sources :

Example 76 with Color

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

the class ShortHistogramTest method createTestImage.

protected BufferedImage createTestImage(int numColors) {
    IndexColorModel icm = createTestICM(numColors);
    int w = numColors * 10;
    int h = 20;
    BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);
    Graphics2D g = img.createGraphics();
    for (int i = 0; i < numColors; i++) {
        int rgb = icm.getRGB(i);
        //System.out.printf("pixel %d, rgb %x\n", i, rgb);
        g.setColor(new Color(rgb));
        g.fillRect(i * 10, 0, w - i * 10, h);
    }
    g.dispose();
    return img;
}
Also used : Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage) IndexColorModel(java.awt.image.IndexColorModel) Graphics2D(java.awt.Graphics2D)

Example 77 with Color

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

the class bug8016356 method main.

public static void main(String[] args) throws Exception {
    // Windows only test
    if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {
        // Retrieving top edge of Desktop
        GraphicsConfiguration grConf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        Rectangle scrRect = grConf.getBounds();
        Insets scrInsets = Toolkit.getDefaultToolkit().getScreenInsets(grConf);
        scrTop = scrRect.y + scrInsets.top;
        color = new Color(0, 255, 0);
        SwingUtilities.invokeAndWait(() -> {
            createAndShowUI();
        });
        try {
            Robot robot = new Robot();
            robot.setAutoDelay(500);
            robot.setAutoWaitForIdle(true);
            robot.delay(1000);
            // Resizing a window to invoke Windows Snap feature
            readFrameInfo();
            robot.mouseMove(frLoc.x + frSize.width / 2, frLoc.y);
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseMove(frLoc.x + frSize.width / 2, scrTop);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            // Retrieving the color of window expanded area
            readFrameInfo();
            Insets insets = frame.getInsets();
            Color bgColor = robot.getPixelColor(frLoc.x + frSize.width / 2, frLoc.y + frSize.height - insets.bottom - 1);
            frame.dispose();
            if (!bgColor.equals(color)) {
                throw new RuntimeException("TEST FAILED: got " + bgColor + " instead of " + color);
            }
            System.out.println("TEST PASSED!");
        } catch (AWTException ex) {
            throw new RuntimeException("TEST FAILED!");
        }
    }
}
Also used : Insets(java.awt.Insets) Color(java.awt.Color) Rectangle(java.awt.Rectangle) Robot(java.awt.Robot) GraphicsConfiguration(java.awt.GraphicsConfiguration) AWTException(java.awt.AWTException)

Example 78 with Color

use of java.awt.Color 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 79 with Color

use of java.awt.Color 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 80 with Color

use of java.awt.Color in project jna by java-native-access.

the class WindowUtilsTest method xtestReveal.

public void xtestReveal() throws Exception {
    final int SIZE = 200;
    System.setProperty("sun.java2d.noddraw", "true");
    GraphicsConfiguration gconfig = WindowUtils.getAlphaCompatibleGraphicsConfiguration();
    Window w;
    Container content;
    if (true) {
        JFrame frame = new JFrame(getName(), gconfig);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        content = frame.getContentPane();
        w = frame;
    } else {
        Frame frame = JOptionPane.getRootFrame();
        JWindow window = new JWindow(frame, gconfig);
        content = window.getContentPane();
        w = window;
    }
    final Window f = w;
    WindowUtils.setWindowTransparent(f, true);
    content.add(new JButton("Quit") {

        private static final long serialVersionUID = 1L;

        {
            addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            });
        }
    }, BorderLayout.SOUTH);
    content.add(new JComponent() {

        private static final long serialVersionUID = 1L;

        public Dimension getPreferredSize() {
            return new Dimension(SIZE, SIZE);
        }

        protected void paintComponent(Graphics graphics) {
            Graphics2D g = (Graphics2D) graphics.create();
            g.setComposite(AlphaComposite.Clear);
            g.fillRect(0, 0, SIZE, SIZE);
            g.dispose();
            g = (Graphics2D) graphics.create();
            Color[] colors = { new Color(0, 0, 0), new Color(0, 0, 0, 128), new Color(128, 128, 128), new Color(128, 128, 128, 128), new Color(255, 255, 255), new Color(255, 255, 255, 128) };
            for (int i = 0; i < colors.length; i++) {
                g.setColor(colors[i]);
                g.fillRect((SIZE * i) / colors.length, 0, (SIZE + colors.length - 1) / colors.length, SIZE);
            }
            g.setColor(Color.red);
            g.drawRect(0, 0, SIZE - 1, SIZE - 1);
            g.dispose();
            SwingUtilities.getWindowAncestor(this).toFront();
        }
    });
    f.pack();
    f.addMouseListener(handler);
    f.addMouseMotionListener(handler);
    f.setLocation(100, 100);
    f.setVisible(true);
    while (f.isVisible()) {
        Thread.sleep(1000);
    //f.repaint();
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) JFrame(javax.swing.JFrame) Frame(java.awt.Frame) ActionEvent(java.awt.event.ActionEvent) JWindow(javax.swing.JWindow) Color(java.awt.Color) JButton(javax.swing.JButton) JComponent(javax.swing.JComponent) Dimension(java.awt.Dimension) Point(java.awt.Point) GraphicsConfiguration(java.awt.GraphicsConfiguration) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) Container(java.awt.Container) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame)

Aggregations

Color (java.awt.Color)2973 Graphics2D (java.awt.Graphics2D)396 Font (java.awt.Font)250 BufferedImage (java.awt.image.BufferedImage)236 Dimension (java.awt.Dimension)193 JPanel (javax.swing.JPanel)193 Point (java.awt.Point)188 BasicStroke (java.awt.BasicStroke)177 ArrayList (java.util.ArrayList)173 Test (org.junit.Test)156 JLabel (javax.swing.JLabel)148 Paint (java.awt.Paint)140 Rectangle (java.awt.Rectangle)109 GradientPaint (java.awt.GradientPaint)108 Insets (java.awt.Insets)103 GridBagConstraints (java.awt.GridBagConstraints)96 IOException (java.io.IOException)96 Stroke (java.awt.Stroke)94 JScrollPane (javax.swing.JScrollPane)93 File (java.io.File)89