views:

145

answers:

5

Hello.

Im trying to write a program which moves the mouse every 3 minutes (to stop a screen saver coming on) but I want to be able to stop and start it at will. As you can see below I have create the buttons and method but when you click run it steps into the while loop and because its in effect an infinite loop it won't look and see if you have clicked the end button.

I have tried system.exit(0) on click for the end button, having the end button pass in false to the method run() and as you can see from the code ive tried an if statement in the while loop to see if it will take notice of me!

Any help would be greatly appreciated!

code:

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Test 
{
boolean loop;
static boolean exit;

public static void main(String[] args) throws AWTException
{
    System.out.println("before");
    makeButtons();
    System.out.println("after");
}

public static void makeButtons()
{
    JFrame jfrMain = new JFrame ("Mouse Robot");
    JPanel jplMain = new JPanel();
    final JButton run = new JButton("Run");
    final JButton end = new JButton("End");

    run.setEnabled(true);
    end.setEnabled(true);

    run.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //run.setEnabled(false);
            //end.setEnabled(true);
            try {
                run(true);
            } catch (AWTException e1) 
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

    end.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            exit = true;
        }
    });

    jplMain.setLayout(new FlowLayout());
    jplMain.add(run);
    jplMain.add(end);

    jfrMain.getContentPane().add(jplMain, BorderLayout.CENTER);
    jfrMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jfrMain.pack();
    jfrMain.setVisible(true);

    }   

public static void run(boolean loop) throws AWTException
{
    Robot r2d2 = new Robot();

    while(loop)
    {

        System.out.println("1");
        Point mousePoint = MouseInfo.getPointerInfo().getLocation();
        mousePoint.translate(0, 1);
        r2d2.mouseMove(mousePoint.x, mousePoint.y);
        r2d2.delay(60000);
        //r2d2.delay(60000);
        //r2d2.delay(60000);
        System.out.println("2");
        mousePoint = MouseInfo.getPointerInfo().getLocation();
        mousePoint.translate(0, -1);
        r2d2.mouseMove(mousePoint.x, mousePoint.y);
        r2d2.delay(60000);
        //r2d2.delay(60000);
        //r2d2.delay(60000);
        System.out.println("looping");
        if (exit = true)
        {
            break;
        }
    }
}

}
+2  A: 

Try

if (exit == true)
{
   break;
}
Dheeraj Joshi
or simply: `if(exit){break;}`
dogbane
... `(exit = true) == true` - there *is* a problem here but it is not responsible for the 'infinite' loop (although I guess, infinity is < 120 seconds in this case ;-) )
Andreas_D
Hello. I cant turn off the screensaver as this will be running on a sunray thin client which automatically powersaves the screen after 5 or so minutes of inactivity to try and save the planet and all that jazz. Ive wracked my brains and went through all the settings and theres nothing I can do on it. This solution doesnt work either :( Thanks for your help though!
Katie
Or: `while (!exit) { … }`
Bombe
Tried that, no luck :(
Katie
A: 

if (exit == true) { break; }

Dhrumil Shah
hehe rooky mistake on my part! Still not working though :(
Katie
don't think even this is going to fix your problem because you are making an infinite loop in the actionPerformed which gets called by EDT (Even Dispatch Thread) and this will halt the event processing altogether. So instead start a new Thread inside the actionPerformed method that moves the mouse. Keep a reference to that thread so that you can stop/interrupt the thread or you can also set the exit condition to stop the thread.
Dhrumil Shah
A: 

I have some doubt about that working out, though I have yet to try it. Wouldn't Java run while without ever listening to mouse, which will end up as infinite loop?

I think it'll have to be some equivalent of javascript function setInterval() to move the mouse and clearInterval() once the button has been clicked.

mmhan
+1  A: 

Well first correct the condition for exit i.e., make it exit == true as mentioned in the first answer.

Second I don't think even this is going to fix your problem because you are making an infinite loop in the actionPerformed which gets called by EDT (Even Dispatch Thread) and this will halt the event processing altogether. So instead start a new Thread inside the actionPerformed method that moves the mouse. Keep a reference to that thread so that you can stop/interrupt the thread or you can also set the exit condition to stop the thread.

Let me know if you need a code example for this.

Faisal Feroz
an example would be fab. I did threads last year for my exams but once the exam was over *woosh* its all gone :P
Katie
A: 

first of all make it exit == true;

instead of using exit = true; you can use loop = false.

Still it will not stop while loop as soon as you click on end button. To stop it immediately after button clicked you must use two different threads. 1. Handling your events. 2. another one running the while loop.

In event handling thread you have to maintain a object of while loop thread, by which you can set appropriate value of loop or exit variable to stop the loop.

krishna