Guest User

Untitled

a guest
Dec 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. // Mouse Monitor
  2. // 5 October 2012
  3. // Niels Janssen & Martijn Claessens
  4.  
  5. import javax.swing.*; //library for swing statements (JFrame, JPanel etc)
  6. import java.awt.*; //contains all of the classes for creating GUI's
  7. import java.awt.event.*; //needed so you can make events and use them in a GUI
  8. import javax.swing.BorderFactory; //library for the border function
  9.  
  10. class MouseMonitor implements MouseListener { //creation of class MouseMonitor with a MouseListener
  11. JFrame window = new JFrame("MouseTracker"); //creation of the main window
  12.  
  13. //North, center and south panel where we can put in labels
  14. JPanel positionPanel = new JPanel();
  15. JPanel centerPanel = new JPanel();
  16. JPanel countPanel = new JPanel();
  17.  
  18. //the labels were we show the presses, releases, clicks and coordinates
  19. JLabel pressedCountLabel = new JLabel( "pressed: ");
  20. JLabel releasedCountLabel = new JLabel( "released: ");
  21. JLabel clickedCountLabel = new JLabel( "clicked: ");
  22. JLabel positionLabel = new JLabel("x-coordinate: y-coordinate:");
  23.  
  24. //declaration of the variables; equal to 0 so you begin with a 'unused' mouse
  25. int x, y;
  26. int nofClicks = 0;
  27. int nofReleases = 0;
  28. int nofPresses = 0;
  29.  
  30. //constructor of the class
  31.  
  32. MouseMonitor() {
  33. //placement of the center panel, with a borderline and a background color
  34. window.add(centerPanel, BorderLayout.CENTER);
  35. centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  36. centerPanel.setBackground(Color.PINK);
  37. centerPanel.setOpaque(true);
  38.  
  39. //placement of the countpanel on the south, with a borderline and the three countlabels
  40. window.add(countPanel, BorderLayout.SOUTH);
  41. countPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  42. countPanel.add( pressedCountLabel );
  43. countPanel.add( releasedCountLabel );
  44. countPanel.add( clickedCountLabel );
  45.  
  46. //placement of the positionpanel on the north, with a borderline and the positionlabel
  47. window.add(positionPanel, BorderLayout.NORTH);
  48. positionPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  49. positionPanel.add( positionLabel );
  50.  
  51. //default window size
  52. window.setSize(600,400);
  53. }
  54.  
  55. //void to actually build everything
  56.  
  57. void start() {
  58. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //if the window is closed, the program will also close
  59. centerPanel.addMouseListener( this ); //the actual mouse listener
  60. window.setVisible( true ); //makes the window visible
  61. }
  62.  
  63. //MouseListener methods
  64.  
  65. //method for mouse pressing, containing the positioning of the mouse and the pressed counter
  66. public void mousePressed( MouseEvent e) {
  67. nofPresses++; //after a press, the counter adds 1
  68. int mouseX = e.getX(); //getting the x-coordinate
  69. int mouseY = e.getY(); //getting the y-coordinate
  70. x = mouseX;
  71. y = mouseY;
  72. positionLabel.setText("x-coordinate: "+x+", y-coordinate:"+y); //printing the coordinates in the position label
  73. pressedCountLabel.setText("pressed: "+nofPresses); //printing number of presses in the pressed Count label
  74. }
  75.  
  76. // method for the counting the releases of the mousebuttons
  77. public void mouseReleased( MouseEvent e) {
  78. nofReleases++; //after each release, the counter adds 1
  79. releasedCountLabel.setText("release: "+nofReleases); // printing the number of releases
  80. }
  81.  
  82. // method for the counting the number of clicks
  83. public void mouseClicked( MouseEvent e) {
  84. nofClicks++; //after each click, the counter adds one
  85. clickedCountLabel.setText("clicked: "+nofClicks); // printing the number of clicks
  86.  
  87. }
  88.  
  89. // empty methods of MouseListener
  90. public void mouseExited( MouseEvent e) {
  91. }
  92. public void mouseEntered( MouseEvent e) { }
  93.  
  94. public static void main(String[] args) {
  95. new MouseMonitor().start();
  96. }
  97. }
Add Comment
Please, Sign In to add comment