• ¡Welcome to Square Theme!
  • This news are in header template.
  • Please ignore this message.
مهمان عزیز خوش‌آمدید. ورود عضــویت


امتیاز موضوع:
  • 39 رای - 2.62 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
Title: Find Color of Pixel at Mouse Pointer
حالت موضوعی
#1
Find Color of Pixel at Mouse Pointer
Yes, it's an awkwardly worded title, but what this does is gets information about where the pointer is, and then finds what color the pixel at the pointer is.

کد:
import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
/**
*
* @author Erik Price
*/
public class ColorGrabber {

    private Color pixelColor;
    private Robot bender /*yes. I am that awesome. Thanks for asking*/ = null;
    private PointerInfo pointer;
    private Point coord;
    private int length;
    private int width;

    public ColorGrabber() throws AWTException //AWTException is for the odd circumstance that no mouse is attached.
                                              //Don't forget to handle this
    {
        bender = new Robot();
        pointer = MouseInfo.getPointerInfo(); //get raw pointer info
        coord = pointer.getLocation(); //translate pointer info into a point
        length = coord.x; //separate the x and y values of the point
        width = coord.y;
        pixelColor = bender.getPixelColor(length, width); //get the color
    }

    public void updatePos() //update mouse position. Identical to constructor
    {
        pointer = MouseInfo.getPointerInfo();
        coord = pointer.getLocation();
        length = coord.x;
        width = coord.y;
        pixelColor = bender.getPixelColor(length, width);
    }

    //all of the following get methods return an int from 0-255 (besides getColor)
    public int getRed()
    {
        return pixelColor.getRed();
    }

    public int getGreen()
    {
        return pixelColor.getGreen();
    }

    public int getBlue()
    {
        return pixelColor.getBlue();
    }

    public Color getColor()
    {
        return pixelColor;
    }

    public int getX() //X value of pointer
    {
        return length;
    }

    public int getY() //Y value of pointer
    {
        return width;
    }  

}


/*example use*/


public class Main {
    public static void main(String[] args) throws IOException, InterruptedException, AWTException /*remember to actually handle these exceptions if you plan on doing something productive*/ {
        ColorGrabber grab = new ColorGrabber();
        while(true) //infinite loop
        {
            grab.updatePos(); //find the mouse
            System.out.println("Blue:\t" + grab.getBlue());
            System.out.println("Red:\t" + grab.getRed());
            System.out.println("Green:\t" + grab.getGreen());
            Thread.sleep(1000); //wait for one second
        }      
    }
}
گروه دور همی پارسی کدرز
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg
 
پاسخ
  


پرش به انجمن:


Browsing: 3 مهمان