0

I'm new here, and I have a problem.
Several months ago, I've made a simple Color Eyedropper that picks colors from anywhere from the screen.
Now I'm making a Color Utility app that has many features like average color generating and mixing colors. Anyway, I thought adding this eyedropper would be cool, so I have a JButton in my JFrame that calls

DigitalEyedropper.init();

and creating another JFrame with my eyedropper.
It opens and runs, but I can't seem to get it to close. The default close button does not work. It doesn't seem listen for any JComponents. I tried to add a JButton to close the window, but it failed.

Can somebody help me?

public static JFrame window;
private static boolean cancel;

public void startProcess(){
    Thread t = new Thread(this);
    t.start();
}

public DigitalEyedropper(){
    window = new JFrame();
    window.setTitle("Color Utilty");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(640,360);
    window.setVisible(true);
    window.setLocationRelativeTo(null);
}

public static void init() throws InterruptedException, AWTException{
    SwingUtilities.invokeLater(new DigitalEyedropper());
}
@Override
public void run() {
    do{
        PointerInfo a = MouseInfo.getPointerInfo();
        Point b = a.getLocation();
        int x = (int) b.getX();
        int y = (int) b.getY();
        try {
            Robot picker = new Robot();
            Color color = picker.getPixelColor(x, y);
            window.setBackground(color);
        }
        catch (AWTException e) {
        }
        if(cancel){
            System.out.println("breaking...");
            //How to close the JFrame?
        } while(/*What to do?*/)
    }
}   
Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
jangdan
  • 23
  • 5
  • 1) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 3) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jul 14 '13 at 13:59
  • @andrew-thompson Thank you sir for your help! I'm quite new to Java, and I'm trying to work with a 'SwingWorker'. Can you give me some guidelines about doing this? – jangdan Jul 16 '13 at 07:33

0 Answers0