Howdy. Let's start with a challenge: Design and implement the
simplest shortest program that notices where the mouse is and
reports that. (Work with any resources you may be able to access, 
be as loud as you need to be, talk to your peers if you have to). 

Alex D, Jeremy B, Daniel O, Laura H, Jake D, Clayton T, Noah P, 
Jay K, Sam McKay, Josh P, Santiago S, Thomas A, Steve B, Anna M, 
Andrew D, Will T, Pong C, David L, Andrzej K, Dimas M, Hao T.,
Xinning W, Meng Y, Danny N, Maro R, Rishu, Ryan P, Jacob C, 
Donovan M, Austn M, Anthony C, Seth W, Kellen A, William Z, 
Jiawei Z, Yangjun L, Troy S, Dhruv A, Bokai Z(*), Yitian Z (*), Sean O, 
Yingzhan Q, Jordan G, Alex T, Dustin K, Caleb G, Sam B, Nikita H, 
Megan H, Scott M, Brad V, Morgan N, Joey R, Chen C(*), Tian J,
Creighton H.

Homework Eight almost finished, to be used for midterm makeups

Nothing is late in this class until the last week so be proactive 

Homework Nine posted: story, pattern, program. 

Our challenge today is related to Homework Nine, Ten. 

Checkpoint Exam next week on Wed: three random problems from 
the 6-10 exercises posted in Homework Ten. Homework Ten will
summarize like Homework Eight (templates) the kind of things
needed for the project. 

Minimal program that creates a frame

Minimal program that notices and reports the mouse

Minimal program that draw something

Minimal program that sets up and uses a timer

Minimal program that uses buttons, key events

And so on...

Lab Twelve is about minimal GUIs in some sense

Lecture Nineteen helps with Homework Nine 

Lecture Twenty alos helps with Homework Nine 

Lecture Twenty also gives you alternative ideas about the project

It also gives you examples of assignments like the project stages. 

Finally we discuss the xeyes problem. 

Homework Help Sessions have started every Sunday @2-4pm in LH102

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) { }
}

So this draws a face. 

We have many examples of faces: pictures. 

Lecture Twenty also has a program shows one eye. 

It also describes some equations. 

import javax.swing.*; 

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

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) { }
}

So this more or less is the answer to the challenge we started with. 

See you on Wednesday. 

--