Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Creating a simple Mouse Analyzer with Java Swing

Mouse analyzer will help you determine the coordinates of your current mouse location.Actually mouse analyzer application can be helpful in game programming app where you want to know about mouse activities or the current mouse position.

Today we are going to discuss about a program which can come real handy to the jobs which require good knowledge of screen coordinates and game programming.

With this program you can:

  • Track the current mouse coordinates
  • Determine events like Mouse Clicks, Mouse Pressed, and Mouse Dragged etc

Let’s start to see how we make this happen.

First of we will include all the imports :

Listing1 : Including all the imports

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;

Now, we will discuss these imports:

  • Dimension class is used to store the coordinates. For our program we will store the screen size coordinates in the Dimension object.
  • FlowLayout is the layout of all the frame components
  • ToolKit is used for obtaining the screen size
  • MouseEvent tells that an event occur inside the frame component.
  • MouseListener and MouseMotionListener are used to listen to any mouse event like mouse click, drag etc.
  • JFrame is used for defining a frame
  • JLabel is use for defining a label

Listing2: Defining class and main method

public class MouseTracker  extends JFrame
implements MouseListener, MouseMotionListener {

	JLabel mousePosition;
public static void main(String args[])
	{
		new MouseTracker().start();
		
	}

Here the name of class is MouseTracker. It implements MouseListener and MouseMotionListener classes which are used to listen to any mouse event like mouse click, drag etc. This is done by implementing the methods of these interfaces, but we will define those methods later.

We define a label named mousePosition which is used to show notifications on current mouse coordinates, define the main method and call the start method which will contain all the processing.

Now we will implement the methods inside these interfaces.

Listing3: Defining Methods of the interfaces

	@Override
	public void mouseClicked(MouseEvent e) {
		mousePosition.setText("Mouse clicked at coordinate : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		mousePosition.setText("Current mouse Coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		mousePosition.setText("Mouse outside access window");
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		mousePosition.setText("Mouse pressed at coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		mousePosition.setText("Current mouse coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		mousePosition.setText("Mouse dragged at coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mousePosition.setText("Mouse moved to coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

Now we will explain the code:

All of the methods contain an argument of type MouseEvent which contain the information like mouse coordinates information.

  1. The getX() and getY() are used to obtain the mouse x and y coordinates.
  2. mouseClicked methods is called once there is mouse movement inside the frame.
  3. mouseEntered method is called when mouse comes from an outside window to the frame.
  4. mouseExited method is called when mouse goes outside of the frame i.e. JFrame
  5. mousePressed is called when the mouse is pressed
  6. mouseReleased is called when mouse key is released
  7. mouseDragged method is called when mouse gets dragged
  8. mouseMoved is called when mouse is mouse from its location.

Now we will define the start method :

Listing4: Defining the start method

	public void start()
	{
		mousePosition=new JLabel();
		 addMouseListener( this );        // listens for own mouse and
	      addMouseMotionListener( this );  // mouse-motion events
	      setLayout(new FlowLayout(1));
	      add(mousePosition);
	      Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
	      setSize(size );
	      setUndecorated(true);
	      setVisible( true );
	      setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}

In the code above:

  • We add the mouselistener to our frame, so that whenever any mouse event occur inside the frame then it
  • can be reported.
  • We set the frame layout to be FlowLayout. We will add the label to our frame.
  • We obtain the screen size by using Toolkit.getDefaultToolkit().getScreenSize() method.
  • We set the size of the frame as screen size so that the frame is full sized frame.
  • We set the frame as Undecorated so that the frame will have no title bar.
  • We set the frame visibility as true.
  • We set setDefaultCloseOperation as EXITONCLOSE so that the program gets closed when frame is closed.

Output:

Mouse Analyzer With Java Swing

Full Source Code:

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class MouseTracker  extends JFrame
implements MouseListener, MouseMotionListener {

	JLabel mousePosition;
	@Override
	public void mouseClicked(MouseEvent e) {
		mousePosition.setText("Mouse clicked at coordinate : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		mousePosition.setText("Current mouse Coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		mousePosition.setText("Mouse outside access window");
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		mousePosition.setText("Mouse pressed at coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		mousePosition.setText("Current mouse coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		mousePosition.setText("Mouse dragged at coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mousePosition.setText("Mouse moved to coordinates : ["+e.getX()+","+e.getY()+"]");
		
	}

	public static void main(String args[])
	{
		new MouseTracker().start();
		
	}
	public void start()
	{
		mousePosition=new JLabel();
		 addMouseListener( this );        // listens for own mouse and
	      addMouseMotionListener( this );  // mouse-motion events
	      setLayout(new FlowLayout(1));
	      add(mousePosition);
	      Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
	      setSize(size );
	      setUndecorated(true);
	      setVisible( true );
	      setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
}

I Hope you like and in the next article I'll show how transfer files between 2 computers.

See you next time.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login