This Java source code defines a simple mouse tracking program using Swing which is a graphical user interface toolkit. The program displays a window with a drawing surface that captures and visualizes various mouse events.
The mouse’s current coordinates, modifier keys (Shift, Control, Meta, and Alt), and the type of the most recent mouse event are displayed in real-time on the window.
The code is organized into a main class, SimpleTrackMouse, and a nested class, Display, which handles the drawing surface. Key concepts covered in the code include event handling through interfaces (MouseListener and MouseMotionListener), drawing on a panel and updating graphical components dynamically. Additionally, it introduces the use of Swing’s JPanel for GUI components.
For a novice Java programmer, this code provides a practical example of basic GUI development and event handling, offering a foundation for understanding more complex Java applications in the future.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SimpleTrackMouse extends JPanel implements MouseListener, MouseMotionListener { Display display; // Drawing surface, defined by nested class Display. int mouse_x, mouse_y; // Position of the mouse. String modifierKeys = ""; // If non-null, gives special keys that are held down. String eventType = null; // If non-null, gives the type of the most recent mouse event. public class Display extends JPanel { // A nested class that defines the drawing surface of the applet. // This simply displays the information contained in the main class // instance variables mouse_x, mouse_y, modifierKeys, and eventType. @Override public void paintComponent(Graphics g) { // Draw the panel, showing information about mouse events. super.paintComponent(g); // Fills panel with background color. g.setColor(Color.blue); g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); g.drawRect(1, 1, getWidth() - 3, getHeight() - 3); g.setColor(Color.red); if (eventType == null) { // If eventType is null, no mouse event has yet occurred // on the panel, so don't display any information. return; } g.drawString("Mouse event type: " + eventType, 6, 18); if (modifierKeys.length() > 0) g.drawString("Modifier keys: " + modifierKeys, 6, 34); g.setColor(Color.black); g.drawString("(" + mouse_x + "," + mouse_y + ")", mouse_x, mouse_y); } // end of paintComponent() } // end nested class Display public SimpleTrackMouse() { // Set background color and arrange for the applet to listen for mouse events. display = new Display(); setLayout(new BorderLayout()); add(display); display.setBackground(Color.white); display.addMouseListener(this); display.addMouseMotionListener(this); } void setInfo(MouseEvent evt) { // set up the information about the event for display mouse_x = evt.getX(); mouse_y = evt.getY(); modifierKeys = ""; if (evt.isShiftDown()) modifierKeys += "Shift "; if (evt.isControlDown()) modifierKeys += "Control "; if (evt.isMetaDown()) modifierKeys += "Meta "; if (evt.isAltDown()) modifierKeys += "Alt"; display.repaint(); } // Implement all the events of the MouseListener and MouseMotionListener // interfaces. Each method sets eventType to record the type of event and // calls the setInfo method to extract other information from the event // for display. @Override public void mousePressed(MouseEvent evt) { eventType = "mousePressed"; setInfo(evt); } @Override public void mouseReleased(MouseEvent evt) { eventType = "mouseReleased"; setInfo(evt); } @Override public void mouseClicked(MouseEvent evt) { eventType = "mouseClicked"; setInfo(evt); } @Override public void mouseEntered(MouseEvent evt) { eventType = "mouseEntered"; setInfo(evt); } @Override public void mouseExited(MouseEvent evt) { eventType = "mouseExited"; setInfo(evt); } @Override public void mouseMoved(MouseEvent evt) { eventType = "mouseMoved"; setInfo(evt); } @Override public void mouseDragged(MouseEvent evt) { eventType = "mouseDragged"; setInfo(evt); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> { JFrame frame = new JFrame("SimpleTrackMouse"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SimpleTrackMouse()); frame.setSize(400, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); }); } } |