Sunday, May 12, 2013

Cursor position

This is a program to find the cursor position when you click or move the mouse on the program window. There are some reasons that the cursor position must be tracked: fetching color value at the position, drawing shape at the cursor position, etc.

CursorPosition source code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CursorArea extends JFrame{
            JLabel lblcurPos;
            CursorArea(){
                       
1                      Toolkit tk=getToolkit();
2                      Dimension ds=tk.getScreenSize();
3                      setPreferredSize(ds);
4                      setLayout(null);
5                      lblcurPos=new JLabel();
6                      lblcurPos.setSize(100,20);
7                      add(lblcurPos);
8                      addMouseListener(new MouseList());
9                      addMouseMotionListener(new MouseMotionList());
10                    setDefaultCloseOperation(EXIT_ON_CLOSE);
11                    setTitle("Cursor Position");
12                    setVisible(true);
13                    pack();
                       
            }

           
14        class MouseList extends MouseAdapter{
                                    public void mousePressed(MouseEvent e){
                                                displayCursorPosition(e.getX(),e.getY());
                                    }
                        }
           
15        class MouseMotionList extends MouseMotionAdapter{
                                    public void mouseMoved(MouseEvent e){
                                                displayCursorPosition(e.getX(),e.getY());
                                    }
                        }

16        public void displayCursorPosition(int x,int y){
                                    lblcurPos.setLocation(x,y);            
                                    lblcurPos.setText("("+x+","+y+")");
                        }
}          

public class CursorPosition{
 
public static void main(String args[]){
    
            new CursorArea();
  
}


}

Cursor position in Java



Code Explanation
1. Create the Toolkit tk object from the current container (JFrame) by using getToolkit method.
2. Get the size or dimension (width and height) of the screen.
3. Set the size of the JFrame window component by using the setPreferredSize method.
4. Set layout of the JFrame window component to null so you can customize other components' locations.
5. Create the label object.
6 Specify the size of the label.
7. Add the label object tot to the current container.
8. Register the current container to the mouse event so you are able to track the cursor location when you click the mouse by implementing the mousePressed method of the MouseAdapter class.
9. Register the current container to the mouse motion event so you can track the cursor location by implementing the mouseMoved method of the MouseMotionAdapter class.
910 Allow the JFrame widnow component to close when the close button is clicked.
11. Set the title of the JFrame window component.
12. Make sure the JFrame window component visible when the program opens.
13. Make sure the JFrame window component resized to fit its preferred size.
14. The mousePressed method of the MouseAdapter class is implemented to record the current cursor  position when you click the mouse. To record the x-axis and y-axis of the cursor, use the getX and getY methods of the MouseEvent lass.
15. The mouseMoved method of the MouseMotionAdapter class is implemented to record the current cursor position when the mouse moves.
16. The displayCursorPosition method is invoked by the MousePressed and MouseMoved methods to display the current cursor position on the label.

No comments:

Post a Comment