0

How can i get my program to read the rgb values under my mouse while i hover over the screen and have a Jframe display the color itself. the rgb values. and possibly the name of the color

So as my title displays i need a pixel color detector

here is what i have so far it opens the jframe but does nothing else

package project;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import javax.swing.JFrame;


public class Project {
    private static int EXIT_ON_CLOSE;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws AWTException {

//timer = new Timer(1000,this); 
JFrame frame= new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//JLabel lable = new JLabel(); 
//JScrollPane jsp = new JScrollPane(lable);
//frame.getContentPane().add(jsp); 
frame. setSize(1000, 700);
frame.setVisible(true);

while(true) {


       PointerInfo cursorLocation = MouseInfo.getPointerInfo();
        Point position = cursorLocation.getLocation(); 
        int x = (int)position.getX(); 
        int y = (int)position.getY();

Robot bob = new Robot();

Color pixelColor = bob.getPixelColor(x, y);
int colorRed = pixelColor.getRed(); 
int colorGreen = pixelColor.getGreen(); 
int colorBlue = pixelColor.getBlue();
//System.out.print("Red " + colorRed + " Green " + colorGreen + " Blue " + colorBlue + "\n" );

 frame.setName("Red " + colorRed + " Green " + colorGreen + " Blue " + colorBlue );
        }
    }
}

1 Answers1

0

It is never a good idea to use while(true). Somewhere in there you should probably have a Thread.sleep(...). I would definitely check out MadProgrammer's solution.

Anyway the problem with your posted code is:

frame.setName("...");

You want:

frame.setTitle("...");
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thank you but how do i get the rgb values to show inside the box not as the name of the box ? – user2950612 Nov 03 '13 at 22:28
  • @user2950612, start by reading the [Swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html) for the basics of creating a GUI in Swing. Maybe the section on `How to Use Labels` would be a good place to start. – camickr Nov 03 '13 at 23:02