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;
}