Challenge: Design and implement the smallest program that knows 
and reports where the mouse is. (Consult any resources you may have
access to, talk to whoever you want, be as loud as you want/need to be). 

Raj R, Carlie B, Ben L, Alex N, Mingming L, Wenhao W, Alan R, Rob B, 
Tyler R, Xiahang L, Steven R, Jonah W, Peyton R, Kyle L, Shayan K, 
Mateus, Akash, Isaac, Jarod, Hongjian, Yaxin, Brent, Jeremy D, Nick H,
Sam Madden, Caige W, Liyuan H, Likang X, Kyle E, Tyler D, Clint S.,
Ethan L., Krista S., Robert Wagner, Nick R., Jinfan J., Vjatcheslav K. 


public class Point {
  private int x, y;
  public Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public int getX() { return x; }
  public int getY() { return y; }
  public double distanceTo(Point other) {
    int dx = this.x - other.getX();
    int dy = this.y - other.getY();
    return Math.sqrt( dx * dx + dy * dy );
  }
}

import javax.swing.*;

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





import java.awt.*;

public class Eye {
  int x, y;
  static int R = 30, r = 10;
  public Eye(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public void draw(Graphics g, Point target) {
    if (target == null) {

    } else {
      // g.drawLine(x, y, target.getX(), target.getY());
      Eye.drawLine(g, new Point(x, y), target);
    }
    // g.drawString("Eye", x, y);
    g.drawOval(x-R, y-R, 2*R, 2*R);
  }
  public static void drawLine(Graphics g, Point p0, Point p1) {
    for (int i = 0; i <= 10; i++) {
      int x = (int) (p0.getX() + i / 10.0 * (p1.getX() - p0.getX()));
      int y = (int) (p0.getY() + i / 10.0 * (p1.getY() - p0.getY()));
      g.fillOval(x-2, y-2, 4, 4);
    }
    double t = (double) R / p1.distanceTo(p0); // percent
    g.setColor(Color.RED);
    int x = (int) (p0.getX() + t * (p1.getX() - p0.getX()));
    int y = (int) (p0.getY() + t * (p1.getY() - p0.getY()));
    g.fillOval(x-r, y-r, 2*r, 2*r); // this is the pupil
    g.setColor(Color.BLACK);
  }
}



import java.awt.*;

public class Face {
  Eye left = new Eye(100, 100),
      right = new Eye(300, 100);
  Nose nose = new Nose(200, 200);
  Mouth mouth = new Mouth(200, 300);
  Point target;
  public void draw(Graphics g) {
    this.left.draw(g, this.target);
    this.right.draw(g, this.target);
    this.nose.draw(g);
    this.mouth.draw(g);
  }
  public void setTarget(Point p) {
    this.target = p;
  }
}


import java.awt.*;

public class Mouth {
  int x, y;
  public Mouth(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public void draw(Graphics g) {
    g.drawString("Mouth", x, y);
  }
}



import java.awt.*;

public class Nose {
  int x, y;
  public Nose(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public void draw(Graphics g) {
    g.drawString("Nose", x, y);
    // g.drawOval(x-25, y-10, 50, 20);
  }
}



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Screen extends JComponent implements MouseMotionListener, MouseListener {
  Face f = new Face();
  public Screen() {
    this.addMouseMotionListener(this);
    this.addMouseListener(this);
  }
  public void paintComponent(Graphics g) {
    this.f.draw(g);
  }
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY();
    System.out.println( "Mouse moved at (" + x + ", " + y + ")" );
    this.f.setTarget(new Point(x, y));
    this.repaint();
  }
  public void mouseDragged(MouseEvent e) { }
  public void mouseEntered(MouseEvent e) {

  }
  public void mouseExited(MouseEvent e) {
    this.f.setTarget(null);
  }
  public void mouseClicked(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { }
  public void mouseReleased(MouseEvent e) { }
}





C:\Users\dgerman\Desktop>cd chap13

C:\Users\dgerman\Desktop\chap13>cd chap13

C:\Users\dgerman\Desktop\chap13\chap13>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 12D1-0980

 Directory of C:\Users\dgerman\Desktop\chap13\chap13

04/06/2015  08:49 AM    <DIR>          .
04/06/2015  08:49 AM    <DIR>          ..
04/06/2015  08:49 AM             6,148 .DS_Store
04/06/2015  08:49 AM               866 BeatBox$MyDownTempoListener.class
04/06/2015  08:49 AM               667 BeatBox$MyStartListener.class
04/06/2015  08:49 AM               740 BeatBox$MyStopListener.class
04/06/2015  08:49 AM               860 BeatBox$MyUpTempoListener.class
04/06/2015  08:49 AM             5,840 BeatBox.class
04/06/2015  08:49 AM             5,917 BeatBox.java
               7 File(s)         21,038 bytes
               2 Dir(s)  314,075,086,848 bytes free

C:\Users\dgerman\Desktop\chap13\chap13>java BeatBox
Error: Could not find or load main class BeatBox

C:\Users\dgerman\Desktop\chap13\chap13>java -d . BeatBox
Unrecognized option: -d
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

C:\Users\dgerman\Desktop\chap13\chap13>javac -d . BeatBox.java

C:\Users\dgerman\Desktop\chap13\chap13>dir
 Volume in drive C is OSDisk
 Volume Serial Number is 12D1-0980

 Directory of C:\Users\dgerman\Desktop\chap13\chap13

04/06/2015  08:54 AM    <DIR>          .
04/06/2015  08:54 AM    <DIR>          ..
04/06/2015  08:49 AM             6,148 .DS_Store
04/06/2015  08:49 AM               866 BeatBox$MyDownTempoListener.class
04/06/2015  08:49 AM               667 BeatBox$MyStartListener.class
04/06/2015  08:49 AM               740 BeatBox$MyStopListener.class
04/06/2015  08:49 AM               860 BeatBox$MyUpTempoListener.class
04/06/2015  08:49 AM             5,840 BeatBox.class
04/06/2015  08:49 AM             5,917 BeatBox.java
04/06/2015  08:54 AM    <DIR>          chap13
               7 File(s)         21,038 bytes
               3 Dir(s)  314,075,025,408 bytes free

C:\Users\dgerman\Desktop\chap13\chap13>dir chap13
 Volume in drive C is OSDisk
 Volume Serial Number is 12D1-0980

 Directory of C:\Users\dgerman\Desktop\chap13\chap13\chap13

04/06/2015  08:54 AM    <DIR>          .
04/06/2015  08:54 AM    <DIR>          ..
04/06/2015  08:54 AM               690 BeatBox$MyDownTempoListener.class
04/06/2015  08:54 AM               523 BeatBox$MyStartListener.class
04/06/2015  08:54 AM               597 BeatBox$MyStopListener.class
04/06/2015  08:54 AM               686 BeatBox$MyUpTempoListener.class
04/06/2015  08:54 AM             5,185 BeatBox.class
               5 File(s)          7,681 bytes
               2 Dir(s)  314,075,025,408 bytes free

C:\Users\dgerman\Desktop\chap13\chap13>java chap13.BeatBox

C:\Users\dgerman\Desktop\chap13\chap13>



The answer:

import javax.swing.*; 

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

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 

public class Screen extends JComponent implements MouseMotionListener {
  public Screen() {
    this.addMouseMotionListener(this);   
  }
  public void paintComponent(Graphics g) {
    g.setColor(Color.BLUE); 
    g.drawOval(50, 50, 100, 100);    
  }
  public void mouseMoved(MouseEvent e) { 
    int x = e.getX(), y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")" ); 
  }
  public void mouseDragged(MouseEvent e) { }
}

--