How to capture the colors of a Label that is behind another label?

1

I have two JLabels overlapping each other.

JLabel label = new JLabel();
label.setBounds(15, 15, 300, 300);
label.setOpaque(true);
label.setBackground(Color.red);

JLabel label1 = new JLabel();
label1.setBounds(60, 60, 300, 300);
label1.setOpaque(true);
label1.setBackground(new Color(0,0,0,125));

" label " is abaixo of " label1 " and both are inserted in GlassPane :

layeredPane.add(label, 0, 0);
layeredPane.add(label1, 1, 0);

GlassPane is perfect inside the frame

frame.setGlassPane(layeredPane);
layeredPane.setVisible(true);
frame.setVisible(true);

Only for " label " (which is below "label1") I created an event on the mouse and when I hover over this label I want it to return the RGB value of its color. p>

ml = new MouseMotionAdapter() {public void mouseMoved(MouseEvent evt) {

      Point point = evt.getLocationOnScreen();

      Color color = robot.getPixelColor((int)point.getX(),(int)point.getY());
      System.out.println(color);
      }

};

Since I do not want it to bring any return from label1 , I include:

 label1.removeMouseMotionListener(ml);
 label.addMouseMotionListener(ml);

The code usually works for the area of the labels . But the mouse still returns the RGB of the colors of label1 over label , when in fact I want the mouse to return only the RGB value for the color of " label " which is below " label1 " .

follows the complete code

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Robot; 
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;


public class Main extends JFrame {

public static void main(String[] args) throws AWTException
{
    JFrame frame;
    Robot robot;
    JLayeredPane layeredPane;
    MouseMotionListener ml;

    robot = new Robot();
    frame = new JFrame("Pc");
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    layeredPane = new JLayeredPane();
    layeredPane.setPreferredSize(new Dimension(300, 310));
    layeredPane.setBorder(BorderFactory.createTitledBorder(
                                "Move the Mouse to Move Duke"));
    JLabel label = new JLabel();
    label.setBounds(15, 15, 300, 300);
    label.setOpaque(true);
    label.setBackground(Color.red);

    JLabel label1 = new JLabel();
    label1.setBounds(60, 60, 300, 300);
    label1.setOpaque(true);
    label1.setBackground(new Color(0,0,0,125));

    layeredPane.add(label, 0, 0);
    layeredPane.add(label1, 1, 0);


    frame.setSize(660, 400);

    frame.getContentPane().setBackground(Color.YELLOW);
    frame.setGlassPane(layeredPane);
    layeredPane.setVisible(true);
    frame.setVisible(true); 

       ml = new MouseMotionAdapter() {

      public void mouseMoved(MouseEvent evt) {

                Point point = evt.getLocationOnScreen();

                Color color = robot.getPixelColor((int)point.getX(),(int)point.getY());
                System.out.println(color);
            }
  };        
            label1.removeMouseMotionListener(ml);
            label.addMouseMotionListener(ml);
    }
}
    
asked by anonymous 05.04.2017 / 22:35

1 answer

2

This is because what you are rescuing are the coordinates of the screen when the mouse moves, not the coordinates of the desired component. Therefore, the color of the label that is overlapped and not the label underneath is returned when the coordinates of the label match.

Take the coordinates of the component instead of taking the whole screen, otherwise what is going to be taken into account is the overlapping color:

ml = new MouseMotionAdapter() {

    @Override
    public void mouseMoved(MouseEvent evt) {

        JComponent comp = (JComponent) evt.getSource();
        Point point = comp.getLocationOnScreen();

        Color color = robot.getPixelColor((int) point.getX(), (int) point.getY());
        System.out.println(color);
    }
};

label.addMouseMotionListener(ml);

The output will be:

Noticethatthecolor(inRGB)isalways255,0,0,thatis, is the red color of JLabel that was superimposed.

Note: The label1.removeMouseMotionListener(ml); line is unnecessary in the code, since the listener is never applied to the component, so there is no need to remove it.

    
06.04.2017 / 17:27