Back to the 2019 paper

Module III: Basics of Web Programming

201914m

Write a program to display current cursor position of the mouse pointer on an Applet using MouseMotionListener interface.

Worked SolutionAI Assisted

Answer: Java Applet Program to Track Mouse Cursor Position

1. Overview

In Java AWT/Applet programming, tracking mouse movement is handled by the MouseMotionListener interface from the java.awt.event package.

The MouseMotionListener interface provides two callback methods:

  1. mouseMoved(MouseEvent e): Invoked every time the mouse cursor moves within the Applet component boundaries without buttons pressed.
  2. mouseDragged(MouseEvent e): Invoked when the mouse is moved while a mouse button is pressed.
[ User Moves Mouse ] ---> [ MouseEvent Fired ] ---> [ mouseMoved(e) extracts X, Y ] ---> [ repaint() ] ---> [ paint(g) draws text ]

2. Complete Java Applet Code

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/*
  <applet code="MouseTrackerApplet.class" width="500" height="400">
  </applet>
*/
public class MouseTrackerApplet extends Applet implements MouseMotionListener {
    // Variables to store mouse cursor coordinates and status
    private int mouseX = 0;
    private int mouseY = 0;
    private String statusMsg = "Move the mouse inside this Applet window...";

    // Initialization method of Applet life cycle
    @Override
    public void init() {
        // Set Applet background and foreground colors
        setBackground(new Color(245, 246, 242));
        setForeground(new Color(27, 36, 48));
        setFont(new Font("SansSerif", Font.BOLD, 14));

        // Register the Applet to listen to mouse motion events
        addMouseMotionListener(this);
    }

    // Invoked when the mouse is moved without pressing any button
    @Override
    public void mouseMoved(MouseEvent e) {
        mouseX = e.getX(); // Get current X coordinate
        mouseY = e.getY(); // Get current Y coordinate
        statusMsg = "Mouse Moving at: X = " + mouseX + ", Y = " + mouseY;
        repaint(); // Request JVM to re-render the Applet screen
    }

    // Invoked when the mouse is dragged with a button pressed
    @Override
    public void mouseDragged(MouseEvent e) {
        mouseX = e.getX();
        mouseY = e.getY();
        statusMsg = "Mouse Dragged at: X = " + mouseX + ", Y = " + mouseY;
        repaint(); // Request JVM to re-render
    }

    // Paint method to render graphics and text on the screen
    @Override
    public void paint(Graphics g) {
        // Display header banner
        g.setColor(new Color(31, 75, 67));
        g.fillRect(20, 20, 460, 40);
        
        g.setColor(Color.WHITE);
        g.drawString("JAVA APPLET MOUSE POSITION TRACKER", 80, 45);

        // Display current cursor coordinates
        g.setColor(new Color(178, 58, 46));
        g.drawString(statusMsg, 30, 100);

        // Draw crosshair indicator at current cursor position
        if (mouseX > 0 && mouseY > 0) {
            g.setColor(Color.BLUE);
            g.drawOval(mouseX - 10, mouseY - 10, 20, 20); // Circle around cursor
            g.drawLine(mouseX - 15, mouseY, mouseX + 15, mouseY); // Horizontal line
            g.drawLine(mouseX, mouseY - 15, mouseX, mouseY + 15); // Vertical line
            
            // Draw small coordinate tooltip near cursor
            g.setFont(new Font("Monospaced", Font.PLAIN, 11));
            g.drawString("(" + mouseX + ", " + mouseY + ")", mouseX + 12, mouseY - 8);
        }
    }
}

3. HTML Code to Embed the Applet (index.html)

<!DOCTYPE html>
<html>
<head>
  <title>Mouse Motion Applet</title>
</head>
<body>
  <h2>Java Applet: Real-Time Mouse Pointer Coordinates</h2>
  <applet code="MouseTrackerApplet.class" width="500" height="400">
    Your browser does not support Java Applets.
  </applet>
</body>
</html>

4. Execution Steps

  1. Compile the Applet:
    javac MouseTrackerApplet.java
    
  2. Run using AppletViewer (JDK tool):
    appletviewer MouseTrackerApplet.java
    
    (Or open index.html in an applet-compatible browser environment).

5. Method Responsibilities Summary

Method Role
init() Configures background color and attaches addMouseMotionListener(this).
mouseMoved(MouseEvent e) Extracts (X,Y)(X, Y) using e.getX() and e.getY() when cursor is moving freely, then calls repaint().
mouseDragged(MouseEvent e) Extracts (X,Y)(X, Y) during drag gestures.
paint(Graphics g) Draws status text and crosshair graphics using coordinates onto the graphics context.

Similar questions