Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Mouse Monitor
- // 5 October 2012
- // Niels Janssen & Martijn Claessens
- import javax.swing.*; //library for swing statements (JFrame, JPanel etc)
- import java.awt.*; //contains all of the classes for creating GUI's
- import java.awt.event.*; //needed so you can make events and use them in a GUI
- import javax.swing.BorderFactory; //library for the border function
- class MouseMonitor implements MouseListener { //creation of class MouseMonitor with a MouseListener
- JFrame window = new JFrame("MouseTracker"); //creation of the main window
- //North, center and south panel where we can put in labels
- JPanel positionPanel = new JPanel();
- JPanel centerPanel = new JPanel();
- JPanel countPanel = new JPanel();
- //the labels were we show the presses, releases, clicks and coordinates
- JLabel pressedCountLabel = new JLabel( "pressed: ");
- JLabel releasedCountLabel = new JLabel( "released: ");
- JLabel clickedCountLabel = new JLabel( "clicked: ");
- JLabel positionLabel = new JLabel("x-coordinate: y-coordinate:");
- //declaration of the variables; equal to 0 so you begin with a 'unused' mouse
- int x, y;
- int nofClicks = 0;
- int nofReleases = 0;
- int nofPresses = 0;
- //constructor of the class
- MouseMonitor() {
- //placement of the center panel, with a borderline and a background color
- window.add(centerPanel, BorderLayout.CENTER);
- centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- centerPanel.setBackground(Color.PINK);
- centerPanel.setOpaque(true);
- //placement of the countpanel on the south, with a borderline and the three countlabels
- window.add(countPanel, BorderLayout.SOUTH);
- countPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- countPanel.add( pressedCountLabel );
- countPanel.add( releasedCountLabel );
- countPanel.add( clickedCountLabel );
- //placement of the positionpanel on the north, with a borderline and the positionlabel
- window.add(positionPanel, BorderLayout.NORTH);
- positionPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- positionPanel.add( positionLabel );
- //default window size
- window.setSize(600,400);
- }
- //void to actually build everything
- void start() {
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //if the window is closed, the program will also close
- centerPanel.addMouseListener( this ); //the actual mouse listener
- window.setVisible( true ); //makes the window visible
- }
- //MouseListener methods
- //method for mouse pressing, containing the positioning of the mouse and the pressed counter
- public void mousePressed( MouseEvent e) {
- nofPresses++; //after a press, the counter adds 1
- int mouseX = e.getX(); //getting the x-coordinate
- int mouseY = e.getY(); //getting the y-coordinate
- x = mouseX;
- y = mouseY;
- positionLabel.setText("x-coordinate: "+x+", y-coordinate:"+y); //printing the coordinates in the position label
- pressedCountLabel.setText("pressed: "+nofPresses); //printing number of presses in the pressed Count label
- }
- // method for the counting the releases of the mousebuttons
- public void mouseReleased( MouseEvent e) {
- nofReleases++; //after each release, the counter adds 1
- releasedCountLabel.setText("release: "+nofReleases); // printing the number of releases
- }
- // method for the counting the number of clicks
- public void mouseClicked( MouseEvent e) {
- nofClicks++; //after each click, the counter adds one
- clickedCountLabel.setText("clicked: "+nofClicks); // printing the number of clicks
- }
- // empty methods of MouseListener
- public void mouseExited( MouseEvent e) {
- }
- public void mouseEntered( MouseEvent e) { }
- public static void main(String[] args) {
- new MouseMonitor().start();
- }
- }
Add Comment
Please, Sign In to add comment