Wim!, a Wiggle Mouse Application

Welcome to Wim!

Image 1. Wim! Screenshot

It is a simple java application to keep your computer in awake condition and not sleep (or maybe auto log off). This application will wiggle the mouse in certain period of time automatically. I usually use this app when I am in computer laboratory in my faculty, so auto log off on my computer login will not happens.:mrgreen:

You can try this app. Download here. Furthermore, I will show my code here. Learn it for fun. It is just simple code, just 3 java files.😉

The following is Main.java that contains main method.

//--------------------------------------------------------------------------
// Wim! (c) 2011. v01.
// By Ardi, Computer Science UI 2008
//
// Main class for running application.
//--------------------------------------------------------------------------

import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

public class Main
{
	//constructor
	public Main()
	{
                //create instance from JFrame
		JFrame frame = new JFrame ("Wim!");

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Panel application = new Panel();
                //add instance of application to frame
		frame.getContentPane().add(application);
		//set frame imageIcon
		ImageIcon icon = new ImageIcon("image/swii_logo.png");
		frame.setIconImage(icon.getImage());

                //set window size
                frame.setSize(220,105);
		frame.setVisible(true);
	}

	//main method is executed first by compiler.
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable(){
			public void run()
			{
				new Main();
			}
		});
	}
}

Next is Panel.java that extends JPanel. Here GUI component like JButton, JTextField, and JLabel, is constructed. Inner class Listener for event-handling is in Panel.java.

//--------------------------------------------------------------------------
// Wim! (c) 2011.
// By Ardi, Computer Science UI 2008
//
// Panel class for managing and drawing
// GUI components like button, textfield, and toolbar.
//--------------------------------------------------------------------------

import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.JOptionPane;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.awt.Color;

public class Panel extends JPanel
{
	//button to start wiggle process
	private JButton btnStart;
	//button to end wiggle process
	private JButton btnEnd;
	//button for state
	private JButton state;
	//button to exit from program
	private JButton btnExit;
	//field to type time in seconds for wiggling
	private JTextField tfTime;
	//instance of WiggleProcess class
	private WiggleProcess wp;

	//constructor to initialize GUI component and add listener
	public Panel()
	{
		//set border layout
		setLayout(new BorderLayout());
		//create instance for wiggle process
		wp = new WiggleProcess();

		//listener for button
		Listener lis = new Listener();

		//button start
		btnStart = new JButton(" Start ");
		btnStart.addActionListener(lis);

		//button end, disabled in beginning
		btnEnd = new JButton(" End ");
		btnEnd.addActionListener(lis);
		btnEnd.setEnabled(false);

		//textfield for time
		tfTime = new JTextField(5);

		//button for state
		state = new JButton(" off ");
		state.addActionListener(lis);
		state.setBackground(Color.DARK_GRAY);
		state.setForeground(Color.WHITE);

		//button for exit from program
		btnExit = new JButton(" Exit ");
		btnExit.addActionListener(lis);

		//panel for button
		JToolBar tb = new JToolBar();
		tb.add(btnStart);
		tb.add(btnEnd);
		tb.add(btnExit);
		tb.addSeparator();
		tb.add(state);

		//panel for time textfield
		JPanel panel = new JPanel();
		panel.add(new JLabel("wiggle every: "));
		panel.add(tfTime);
		panel.add(new JLabel("sec"));

		//add component
		add(tb, BorderLayout.NORTH);
		add(panel, BorderLayout.SOUTH);
	}

	//inner class for event-handling
	private class Listener implements ActionListener
	{
		//invoked when action occurs
		public void actionPerformed(ActionEvent e)
		{
			//stop wiggle process when btnEnd is clicked
			if(e.getSource() == btnEnd)
			{
				wp.stop();
				//setting button
				btnStart.setEnabled(true);
				btnEnd.setEnabled(false);
				//set state
				state.setText(" off ");
				state.setBackground(Color.DARK_GRAY);
			}
			//start wiggle proces when btnStart is clicked
			if(e.getSource() == btnStart)
			{
				try
				{
					//get value from textfield
					int sec = Integer.parseInt(tfTime.getText());
					//it is valid condition
					if(sec > 0 && sec <= 600)
					{
						//set time for wiggle mouse
						wp.setTime(sec);
						//running process with thread
						new Thread(wp).start();
						//setting button
						btnStart.setEnabled(false);
						btnEnd.setEnabled(true);
						//set state
						state.setText(" on ");
						state.setBackground(new Color(15, 130, 5));
					}
					//if more than 600 sec, not valid
					else if(sec > 600)
					{
						JOptionPane.showMessageDialog(Panel.this,
						"Number too big", "Warning", JOptionPane.WARNING_MESSAGE);
					}
					else
					{
						JOptionPane.showMessageDialog(Panel.this,
						"Error: Negative number or zero", "Warning", JOptionPane.WARNING_MESSAGE);
					}
				}
				catch(Exception exc)
				{
					JOptionPane.showMessageDialog(Panel.this,
						"Error: "+exc.getMessage(), "Warning", JOptionPane.WARNING_MESSAGE);
				}
			}
			//show information when it is clicked
			if(e.getSource() == state)
			{
				//author info
                                String aboutAuthor =
                                        "Wim! wiggle ur mouse. \n\n"+
				        "Author:\n"+
                                        "Ardi \n\n"+
                                        "Email:\n"+
                                        "Ardialhaidar@yahoo.co.id\n\n"+
                                        "Computer Science UI 2008\n"+
                                        "Visit: Ardialhaidar.wordpress.com\n";
                                JOptionPane.showMessageDialog(Panel.this, aboutAuthor,
                                        "About Author", JOptionPane.INFORMATION_MESSAGE);
			}
			//exit from program when btnExit is clicked
			if(e.getSource() == btnExit)
			{
				System.exit(0);
			}
		}
	}
}

The last is WiggleProcess.java. In this class, wiggling process happens. WiggleProcess is handled by thread, so it implements class Runnable. With separated thread, wiggling process will not disturb UI thread. Oh ya, main class for wiggling process is in Robot in java.awt package.

//--------------------------------------------------------------------------
// Wim! (c) 2011.
// By Ardi, Computer Science UI 2008
//
// Wiggle Process for wiggling mouse automatically in periodic time.
//--------------------------------------------------------------------------

import java.awt.PointerInfo;
import java.awt.Point;
import java.awt.MouseInfo;

import java.awt.Robot;
import java.awt.AWTException;

public class WiggleProcess implements Runnable
{
	//a flag for running process
	private boolean isRunning;
	//field periodic time
	private int sec;
	//instance of Robot
	private Robot robot;

	//default constructor for initializing
	public WiggleProcess()
	{
		//false means process is not running
		isRunning = false;
		sec = 1;
		try
		{
			//create instance of Robot class
			robot = new Robot();
		}
		catch(AWTException e)
		{ e.printStackTrace();}
	}

	//run main code
	public void run()
	{
		//set isRunning true
		isRunning = true;

		//check what process is running
		while(isRunning)
		{
			//get location of pointer
			Point p = MouseInfo.getPointerInfo().getLocation();
			int x = (int) p.getX();
			int y = (int) p.getY();

			//move mouse by robot
			robot.mouseMove(x,y);
			robot.delay(250);
			robot.mouseMove(x+20, y+20);
			robot.delay(250);
			robot.mouseMove(x,y);
			robot.delay(sec*1000);
		}
	}

	//stop running process
	public void stop()
	{
		isRunning = false;
	}

	//set time for wiggling
	public void setTime(int sec)
	{
		this.sec = sec;
	}
}

That’s all the code. It simple, is’nt it?😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s