1. Assume all eyes are of the same size. Is that important?

2. Could we have eyes of different sizes? 

3. How do I put the pupil where it should be? 

Xiaohang, Nick R, Jiawei T., Patrick G, Wenhao, Abigail, Mingming, 
Alex N, Joe P., Carlie B., Jake P., Tyler R, Alan R, James A, Nick O, 
Steven R, Jonah W, Peyton R, Jinfan, Ethan L, Akash, Shayan, Kyle, 
Mateus, Robert W, Jarod, Clint, Hongjian, Yaxin, Jiawei Z, Dhruv, 
Lucas, Sam Maginot, Liyuan, Likang, Tyler, Daniel deSilva, Caige, 
Ben L, Tim M, Sam Madden, Nick H, Jeremy d. 

Today we worked out the following abstraction: 

import java.awt.*; 

public class Eye {
  int R, r;
  int x, y; 
  int xt, yt; 
  public Eye(int x, int y, int R, int r) {
    this.x = x;
    this.y = y; 
    this.R = R;
    this.r = r; 
  }
  public void draw(Graphics g) {
    // g.drawString("Eye", this.x, this.y);    
    g.setColor(Color.RED);
    int dx = this.x - this.xt, 
        dy = this.y - this.yt;
    double d = Math.sqrt(dx * dx + dy * dy);
    double p = (R - r) / d; 
    int x = (int) (this.x + p * (this.xt - this.x)), 
        y = (int) (this.y + p * (this.yt - this.y)); 
    if (d <= R-r) {
        x = this.xt;
        y = this.yt;
    }  
    g.fillOval(x - this.r, y - this.r, 2 * this.r, 2 * this.r); 
    g.setColor(Color.BLACK); 
    g.drawOval(this.x - this.R, this.y - this.R, 2 * this.R, 2 * this.R); 
    // g.drawLine(this.x, this.y, this.xt, this.yt); 
  }
  public void setTarget(int x, int y) {
    this.xt = x; 
    this.yt = y; 
  }
}

Then we need it withing a JComponent with this definition: 

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

public class Screen extends JComponent implements MouseMotionListener {
  Eye[] eyes; 
  public Screen() {
    this.addMouseMotionListener(this); 
    this.eyes = new Eye[6];
    for (int i = 0; i < this.eyes.length; i++) 
      this.eyes[i] = new Eye( (int) (Math.random() * 400), 
                              (int) (Math.random() * 400),
                              (int) (Math.random() * 10 + 30),
                              (int) (Math.random() * 4 + 8)
                            );       
  }
  public void mouseMoved(MouseEvent e) { 
    int x = e.getX(), y = e.getY();
    for (Eye eye : this.eyes) {
      eye.setTarget(x, y);  
    }
    this.repaint(); 
  }
  public void mouseDragged(MouseEvent e) { }
  public void paintComponent(Graphics g) {
    for (Eye e : this.eyes) 
      e.draw(g); 
  }
}

And this is the external  boilerplate: 

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

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

So that's it for today and Homework Nine. 

https://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

--