-2

我在使用此代码时遇到问题。我希望能够移动鼠标并获得鼠标所在位置的像素值(红色、绿色、蓝色)。我无法使用“getRGB”。你将如何开始。有人可以指出我正确的方向。

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

   public class PixelColor {
   public static void main(String[] args) throws Exception {
    PointerInfo pointer;
    pointer = MouseInfo.getPointerInfo();
    Point coord = pointer.getLocation();

    Robot robot = new Robot();
    robot.delay(2000);


    int red = getred();
    int green= getgreen();
    int blue= getblue();
    int color = new Color (red,green,blue).getRGB();

    while(true) {
        coord = MouseInfo.getPointerInfo().getLocation();  
        color = getred();

        {
    System.out.println(getRGB());




        }
4

2 回答 2

1

研究使用 Robot 的 getPixelColor 方法:

color = robot.getPixelColor(coord.x, coord.y);

例如,

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

public class PixelColor {
   private static final long SLEEP_DELAY = 400L;

   public static void main(String[] args) {
      Point coord;
      Robot robot = null;

      try {
         robot = new Robot();
      } catch (AWTException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      Color color = null;
      while (true) {
         coord = MouseInfo.getPointerInfo().getLocation();
         color = robot.getPixelColor(coord.x, coord.y);
         System.out.println(color);
         try {
            Thread.sleep(SLEEP_DELAY);
         } catch (InterruptedException e) {}
      }
   }
}
于 2013-04-25T15:43:24.427 回答
0

要获取鼠标指针所在的像素颜色,需要在循环的每一次迭代中获取鼠标位置,然后使用robot.js的方法getPixelColor

while(true) 
{
    pointer = MouseInfo.getPointerInfo();
    coord = pointer.getLocation();
    System.out.print(coord.x + " " + coord.y);
    System.out.println(" color: " + robot.getPixelColor(coord.x, coord.y).getRed() +                                             
                           ", " + robot.getPixelColor(coord.x, coord.y).getGreen() + 
                ", " + robot.getPixelColor(coord.x, coord.y).getBlue());
}
于 2013-04-25T15:44:34.660 回答