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:
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:
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.
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:
Output:

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.







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