Do you even remember how you can create a JFrame? 

import javax.swing.JFrame; 

class Biankuang extends JFrame {
  Biankuang() {
    this.setVisible(true); 
    this.setSize(500, 600); 
  }
  public static void main(String[] args) {
    JFrame a = new Biankuang(); 
    
  }
}

Do you even remember how we draw Graphics? 

import javax.swing.JComponent; 
import java.awt.Graphics;

class Screen extends JComponent {
  public void paintComponent(Graphics g) {
    // http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString%28java.lang.String,%20int,%20int%29
    g.drawString("How are you all doing?", 100, 100); 
  }
}

Now I want my first program to call the Screen program:

import javax.swing.JFrame; 

class Biankuang extends JFrame {
  Biankuang() {
    // http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html
    this.getContentPane().add( new Screen() ); 
    this.setVisible(true); 
    this.setSize(500, 600); 
  }
  public static void main(String[] args) {
    JFrame a = new Biankuang(); 
    
  }
}

So now I have what I had yesterday. 

I would now like to make the Screen sensitive to the mouse movement. 

There are the following steps you need to take:

  (a) first you need to have a MouseMotionListener

    1. you need to define such an abstraction

    2. you need to instantiate it

  (b) then you need to assign it (the object from 2.) to a source of events 

There is an interface in Java called java.awt.even.MouseMotionListener

http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseMotionListener.html

So now we write this as the Screen class:

import javax.swing.JComponent; 
import java.awt.Graphics;
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

class Screen extends JComponent implements MouseMotionListener {
  Screen() {
    this.addMouseMotionListener( this );  
  }
  public void mouseMoved(MouseEvent e) { }
  public void mouseDragged(MouseEvent e) { 
    int x = e.getX(); 
    int y = e.getY(); 
    System.out.println( "Mouse is being dragged...(" + x + ", " + y + ")" ); 
  }
  public void paintComponent(Graphics g) {
    // http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString%28java.lang.String,%20int,%20int%29
    g.drawString("How are you all doing?", 100, 100); 
  }
}

So now I want to have the text follow the mouse:

import javax.swing.JComponent; 
import java.awt.Graphics;
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

class Screen extends JComponent implements MouseMotionListener {
  Screen() {
    this.addMouseMotionListener( this );  
  }
  public void mouseMoved(MouseEvent e) { }
  public void mouseDragged(MouseEvent e) { 
    int x = e.getX(); 
    int y = e.getY(); 
    // System.out.println( "Mouse is being dragged...(" + x + ", " + y + ")" ); 
    this.x = x; 
    this.y = y; 
    this.repaint(); 
  }
  String text = "How are you all doing?"; 
  int x = 250, y = 300; 
  public void paintComponent(Graphics g) {
    // http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString%28java.lang.String,%20int,%20int%29
    g.drawString(this.text, this.x, this.y); 
  }
}

The only thing that changed was: Screen. 

It's a simple change that allows us to show the coordinates:

import javax.swing.JComponent; 
import java.awt.Graphics;
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

class Screen extends JComponent implements MouseMotionListener {
  Screen() {
    this.addMouseMotionListener( this );  
  }
  public void mouseMoved(MouseEvent e) { }
  public void mouseDragged(MouseEvent e) { 
    int x = e.getX(); 
    int y = e.getY(); 
    // System.out.println( "Mouse is being dragged...(" + x + ", " + y + ")" ); 
    this.x = x; 
    this.y = y; 
    this.text = "(" + this.x + ", " + this.y + ")"; 
    this.repaint(); 
  }
  String text = "How are you all doing?"; 
  int x = 250, y = 300; 
  public void paintComponent(Graphics g) {
    // http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#drawString%28java.lang.String,%20int,%20int%29
    g.drawString(this.text, this.x, this.y); 
  }
}

So now we have the following set up: 

  (a) the source of events is the JComponent

  (b) which is also the MouseMotionListener 

  (c) when the mouse is dragged, the text with coordinates is updated
      The method repaint() is then invoked to have it painted. 
      
  (d) so the instance methods communicate through the instance variables

So I am asking myself: can I use the mouse as a drawing instrument? 

My first experiment looks like this: 

import javax.swing.JComponent; 
import java.awt.Graphics;
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

class Screen extends JComponent implements MouseMotionListener {
  Screen() {
    this.addMouseMotionListener( this );  
  }
  public void mouseMoved(MouseEvent e) { }
  public void mouseDragged(MouseEvent e) { 
    Graphics g = this.getGraphics();
    g.drawOval( e.getX(), e.getY(), 10, 10 ); 
  }
  public void paintComponent(Graphics g) {
    // ... 
  }
}

What's wrong with it? 

I am fighting the structure that's been set up for me!

What am I supposed to do? 

I try this at Jason's suggestion: 

import javax.swing.JComponent; 
import java.awt.Graphics;
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

class Screen extends JComponent implements MouseMotionListener {
  int x, y; 
  Screen() {
    this.addMouseMotionListener( this );  
  }
  public void mouseMoved(MouseEvent e) { }
  public void mouseDragged(MouseEvent e) { 
    this.x = e.getX(); 
    this.y = e.getY(); 
    this.repaint(); 
  }
  public void paintComponent(Graphics g) {
    g.drawOval(this.x, this.y, 15, 15); 
  }
}

This is better in some sense and worse in others. 

The question remains: what can I do? 

Please e-mail me (dgerman@indiana.edu) and tell me how you're 
doing in this class and what we can do to make it easier or better
for you. This is the attendance for today. We'll see you in lab. 

--

We need to generate circles and store them so paintComponent(...) 
always prints all of them. We will have to use ArrayList<...> 

http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html

http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseMotionListener.html