// A class which defines a pupil and the constraints within which it can move. import java.awt.*; public class Pupil { private int pupil_size, pupil_x, pupil_y; private int center_x, center_y; private int radius, right_border, left_border; public Pupil (int r, int cx, int cy, int rb, int lb, int ps) { radius = r; pupil_x = center_x = cx; pupil_y = center_y = cy; right_border = rb; left_border = lb; pupil_size = ps; } // Update where the eye is looking. public void update (int stare_x, int stare_y) { float height = center_y - stare_y; float width = center_x - stare_x; // Calculate the position of the pupil within the radius of the eye. float hypot = (float)Math.sqrt(height * height + width * width); pupil_x = (int)(center_x - (radius / hypot) * width); pupil_y = (int)(center_y - (radius / hypot) * height); // Adjust the eye if it is outside the left or right borders. if (pupil_x < right_border) pupil_x = right_border; else if (pupil_x > left_border) pupil_x = left_border; } // Draw the pupil. public void draw (Graphics g) { // White background for eyeball. g.setColor(Color.WHITE); g.fillRect(center_x - radius*2, center_y - radius*2, radius*4, radius*4); // Draw the pupil. g.setColor(Color.BLACK); g.fillOval(pupil_x - pupil_size / 2, pupil_y - pupil_size / 2, pupil_size, pupil_size); } }