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

Asked

Viewed 122 times

1

I have two Jlabels superimposed on 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));

The "label" is abaixo of "label1" and both are inserted in a 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 the "label" (what is below the "label1") I created an event on the mouse and when I pass the mouse over this label I want it to return the RGB value of its color.

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);
      }

};

As I don’t want it to bring any return from label1, i includes:

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

The code normally works with regard to Labels area. But the mouse still returns the RGB of the colors of the label1 about the label, when in fact I want the mouse to return only the RGB value to the color of the "label" that is below the "label1".

follows the full 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);
    }
}
  • Where does that come from robot?

  • Frame public void Frame1() throws AWTException
 {
 robot = new Robot();...}

  • You can add an executable chunk of this Jframe with the problem?

  • see if you get it!

1 answer

2


This is because what you are rescuing is the coordinates of the screen when the mouse moves, not the coordinates of the desired component. So it returns the color of the label that is overlaid and not the label that is underneath when the coordinates of both match.

Take the coordinates of the component instead of taking the whole screen, otherwise what will be taken into account is the color superimposed:

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 exit will be:

inserir a descrição da imagem aqui

Note that the color(in RGB) is always 255,0,0, that is to say, is the red color of JLabel that was superimposed.

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

  • Diego, you’re the guy!!! That’s what I was trying to solve! How am I Noob on this site as I thank you! :)

  • @vinuales dispo! : ) You can mark the answer as accepted by clicking on V, thus, it will serve as reference for others with similar problem,

Browser other questions tagged

You are not signed in. Login or sign up in order to post.