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
?– user28595
Frame
public void Frame1() throws AWTException
 {
 robot = new Robot();...}
– vinuales
You can add an executable chunk of this Jframe with the problem?
– user28595
see if you get it!
– vinuales