0

I want my program to start printing out RGB-values under my mouse ,the moment I enable a checkbox, called doos, and I want it to stop the moment it is deselected.

    private void doosActionPerformed(java.awt.event.ActionEvent evt) {

    if (doos.isEnabled()) {
        try {
            zwevendeMuis =true;
            robot = new Robot();

            while (zwevendeMuis==true) {

                pointer = MouseInfo.getPointerInfo();
                point = pointer.getLocation();
                color = robot.getPixelColor((int) point.getX(), (int) point.getY());
                System.out.println("Color at: " + point.getX() + "," + point.getY() + " is: " + color);
            }
        } catch (Exception e) {
        }            
    }
    else{
        zwevendeMuis =false;

    }
}

Thank you for your time

EDIT : I dont know if i should post another thread on this matter but i was able to take it one step further i think. I have tried some stuff out via multithreading ,below you can see the updated code. The Check class is the runnable parameter i give with the 'mythread' object to make my code alternate between EDT and worker thread. Now it kinda does what I want with the exception of my code spawning of numerous errors when leaving and entering the element:

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Thread.java:682)
    at testgui.TestGUI$1.mouseEntered(TestGUI.java:126)
    at java.awt.Component.processMouseEvent(Component.java:6514)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
Color at: 495.0,520.0 is: java.awt.Color[r=168,g=51,b=162]
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4620)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4474)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
Color at: 498.0,516.0 is: java.awt.Color[r=168,g=51,b=162]

    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
Color at: 499.0,515.0 is: java.awt.Color[r=168,g=51,b=162]
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

My updated code:

class Check implements Runnable {

            @Override
            public void run() {
                go();
            }

            public void go() {
                System.out.println("");

            }

        }

        @Override
        public void mousePressed(MouseEvent me) {
        }

        @Override
        public void mouseReleased(MouseEvent me) {
        }

        @Override
        public void mouseEntered(MouseEvent me) {
            zwevendeMuis = true;
            Runnable control = new Check();
            Thread mythread = new Thread(control);
            try {
                robot = new Robot();

            } catch (AWTException ex) {
                Logger.getLogger(TestGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            while (zwevendeMuis == true) {
                pointer = MouseInfo.getPointerInfo();
                point = pointer.getLocation();
                color = robot.getPixelColor((int) point.getX(), (int) point.getY());
                System.out.println("Color at: " + point.getX() + "," + point.getY() + " is: " + color);
                mythread.start();
            }
        }

        @Override
        public void mouseExited(MouseEvent me) {
            zwevendeMuis=false;
        }
BURNS
  • 711
  • 1
  • 9
  • 20
  • Are you grabbing pixel values at arbitrary points on the screen? IE not inside a Swing/AWT component. (Judging by your use of Robot this seems to be what you're doing.) – Radiodef Feb 14 '14 at 22:35

2 Answers2

2

You're blocking the Event Dispatching Thread with your infinite loop.

The EDT is responsible for many things, including event processing and painting. Any action which blocks/stops this thread will prevent your application from receiving new events or processing new paint requests.

Swing is a single threaded environment and isn't thread safe, so while you could spawn off a new Thread, you would need to ensure that any code that interacted with the UI was done so fro within the context of the EDT.

A simpler solution would be to use a SwingWorker, which would allow you to monitor the mouse position from within a background thread, but has publish and process methods which simplifies the EDT synchronization process.

Take a look at Concurrency in Swing for more details.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I reupdated my OP with some new code. I havent used SwingWorker however. Would my new code be sufficient after solving the errors or unsolvable mess? – BURNS Mar 01 '14 at 00:26
  • And looking at swingworker here :http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TumbleItemProject/src/components/TumbleItem.java Does the code from SwingWorker worker = new SwingWorker() need to be in a certain method or in main or ? – BURNS Mar 01 '14 at 00:46
  • 1
    Having a `MouseListener` wrapped around a `Runnable` won't help, the mouse events will still be raised from within the context of the EDT, putting you back at square one. You need to pool the `MouseInfo` from `run` method or `doInBackground` of a `SwingWorker` – MadProgrammer Mar 01 '14 at 05:26
  • 1
    For [example](http://stackoverflow.com/questions/13061122/getting-rgb-value-from-under-mouse-cursor/13061320#13061320) – MadProgrammer Mar 01 '14 at 05:28
  • And in which method do i need to instantiate my swingworker? in between methods? Or in line with main or ... ? – BURNS Mar 02 '14 at 01:54
  • I'm trying to say ,what method calls this swingworker? – BURNS Mar 02 '14 at 02:03
0

Your while loop is going to prevent your program from doing anything else. You should toggle a boolean value when clicking your checkbox (zwevendeMuis = doos.isEnabled()), and you should register a MouseMotionListener with one or more components of your application which will print the color value if zwevendeMuis is true.

Brian S
  • 4,878
  • 4
  • 27
  • 46