Skip to content
Advertisement

Java: How got check colors from an invisible panel

is it possible to read colors of you mouse position of an invisible panel? I want to have an “invisible overlay” with an color coded image. I used the Robot class to get my mouse position and the color.

public void mouseClicked(MouseEvent evt){
        Robot r = new Robot(); 
        Point p = evt.getLocationOnScreen();
        Color color = r.getPixelColor(p.x,p.y);
        System.out.println(color);                  
 }

It works fine on my visible image, but if I set the image to invisible, it completely gets ignored. Is there a way to get the same result (get the color of my mouses position), but for an invisible image/panel?

Edit: Additional question (hope it’s allowed) that would also help me: Is is possible to get the color (of my mouses position) of a single JLayeredPane? I use several JLayeredPanes with small images. All have the same size, but are partially transparent, so they create a big image, where I can change single parts of. My Problem would also be solved, if I were able to get the color (of my mouses position) of a single JLayeredPane, respectively could check if it’s transparent or not.

Advertisement

Answer

You can create a BufferedImage of any component. Once you have the BufferedImage you can then use the getRGB(…) method to get the color of the pixel.

The easiest way to create a BufferedImage of any component is to use the Screen Image class. You can create an image of the frame, any component on the frame or any rectangular area of the component.

In the example below the label on the right is an image of your invisible component. As you move your mouse around the empty label on the left the background will be changed to indicate the color of the pixel on the invisible component:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;

public class InvisibleImage extends JPanel
{
    public InvisibleImage()
    {
        setLayout( new GridLayout(0, 2) );

        JLabel invisible = new JLabel( new ImageIcon("mickeyred.jpg") );
        invisible.setVisible( false );

        BufferedImage image = ScreenImage.createImage( invisible );

        JLabel panel = new JLabel();
        panel.setOpaque( true );
        add(panel);

        JLabel label = new JLabel( new ImageIcon( image ) );
        add(label);

        panel.addMouseMotionListener( new MouseMotionAdapter()
        {
            @Override
            public void mouseMoved(MouseEvent e)
            {
                int pixelColor = image.getRGB(e.getX(), e.getY());
                Color color = new Color( pixelColor );
                panel.setText( color.toString() );
                panel.setBackground( color );
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("InvisibleImage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new InvisibleImage() );
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

If you don’t want to use the ScreenImage class then you can create the BufferedImage using:

invisible.setSize( invisible.getPreferredSize() );
Dimension d = invisible.getSize();
BufferedImage image = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
invisible.print( g2d );
g2d.dispose();
Advertisement