import javax.swing.*; // JFrame
import java.awt.event.*; // MouseMotionListener MouseEvent

public class One extends JFrame implements MouseMotionListener {
  public void mouseMoved(MouseEvent e) { 
    System.out.println("Mouse moved."); 
  } 
  public void mouseDragged(MouseEvent e) { 
    int x = e.getX(), 
      // https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html#getX()
        y = e.getY(); 
    System.out.println("Mouse dragged at (" + x + ", " + y + ")"); 
  } 
  public static void main(String[] args) {
    One a = new One();
    a.addMouseMotionListener(a); 
    a.setVisible(true); 
    a.setSize(300, 400); 
  }
}

Task: make your code draw the coordinates of the mouse when it's dragged. 

We invent a JComponent:

import javax.swing.*; // JComponent
import java.awt.*; // Graphics  
import java.awt.event.*; // MouseMotionListener MouseEvent
  
public class Screen extends JComponent 
                    implements MouseMotionListener {
  int x = 200, y = 200;  
  public Screen() {
    this.addMouseMotionListener( this );  
  }
  public void mouseMoved(MouseEvent e) { 
    System.out.println("From Screen: mouse moved."); 
  } 
  public void mouseDragged(MouseEvent e) { 
    this.x = e.getX(); 
    this.y = e.getY(); 
    this.repaint(); 
  } 
  public void paintComponent(Graphics g) {
    // https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString(java.lang.String,%20int,%20int)
    g.drawString("How are you?", this.x, this.y);   
  }
}

And we use it: 

import javax.swing.*; // JFrame 

public class Example extends JFrame {
  public Example() {
    this.setSize(400, 400); 
    this.setVisible(true); 
    this.add( new Screen() ); 
  }
  public static void main(String[] args) {
    Example q = new Example(); 
  }
}

Exercise: change so it prints the coordinates as it follows the mouse pointer. 

Solution: 

  public void paintComponent(Graphics g) {
    // https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString(java.lang.String,%20int,%20int)
    g.drawString("(" + this.x + ", " + this.y + ")", this.x, this.y);   
  }

You will notice that the coordinates reported are of the JComponent. 

The (0, 0) point is now accessible. 

--