Frage

I want to pick up the RBG value of the color in the image when the mouse clicks the position of the color. Actually, I put the image on the top left corner in the jFrame. I try to get the mouse location, for example, x= 190, y=80, which is near the last pixel of the image. However, the image size is 200x24. Therefore, I cannot convert the mouse pointer position to the pixel of the image. Is there any method to do this? Thank you.

Add more information:

I create a jframe and put a jlabel which is the image at the top left corner of the jframe. What I want to do it is: when I use the mouse point and click the position on the image, I would get the color of this position. screen capture: http://i.stack.imgur.com/SjFhr.png [when i use the mouse point to the black position of the image, it shows r=240,g=240,b=240]

frame.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
            try {
                System.out.println(getPointerColor());
                Thread.sleep(1000);
            } catch (AWTException awte) {
                System.out.println("Error while getting pointer's color!");
            } catch (InterruptedException ie) {
                System.out.println("Error while sleeping!");
            }

        }
});
War es hilfreich?

Lösung

You can use the Robot class (see Documentation) to get the colour for a set of coordinates relative to the screen or the GraphicsDevice you want:

public Color getPixelColor(int x, int y) throws AWTException {
    Robot robot = new Robot();
    return robot.getPixelColor(x, y);
}

You can then retrieve the RGB values from that returned Color object. Make sure your coordinates are good though!

As an additional test, you can try running the following, which displays the pointer's pointed colour relative to the screen (in other words, the absolute cursor coordinates) every second:

import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;

public class Test {

    public static void main(String[] args) throws Exception {
        while (true) {
            try {
                System.out.println(getPointerColor());
                Thread.sleep(1000);
            } catch (AWTException awte) {
                System.out.println("Error while getting pointer's color!");
            } catch (InterruptedException ie) {
                System.out.println("Error while sleeping!");
            }
        }
    }

    public static Color getPointerColor() throws AWTException {
        Point coordinates = MouseInfo.getPointerInfo().getLocation();
        Robot robot = new Robot();
        return robot.getPixelColor((int) coordinates.getX(), (int) coordinates.getX());
    }
}

Andere Tipps

If it's a Swing UI, use JColorChooser for this: http://docs.oracle.com/javase/tutorial/uiswing/components/colorchooser.html. If you must do it manually, convert the mouse pointer position by subtracting a hard-coded offset.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top