Howdy.

Next homework assignment: applet or standalone program. 

Face that has two eyes following the mouse pointer. 

How do you find the intersection between a circle and a line. 

The line connects center with outside (given) point. 

Lab. 

Exam. 

Modeling (defining classes): Fraction, BigDecimal. 

class Fraction {
  int num, den; 
  Fraction(int num, int den) {
    this.num = num; 
    this.den = den; 
  } 
  Fraction add(Fraction other) {
    int n = this.num * other.den + this.den * other.num; 
    int d = this.den * other.den; 
    return new Fraction(n, d); 
  } 
  boolean greaterThan(Fraction other) {
    return (this.sub(other)).positive();
  } 
  boolean positive() {
    return this.num * this.den > 0; 
  } 
  Fraction sub(Fraction other) {
    int n = this.num * other.den - this.den * other.num; 
    int d = this.den * other.den; 
    return new Fraction(n, d); 
  } 
  public String toString() {
    return this.num + "/" + this.den; 
  }
  public static void main(String[] args) {
    Fraction a = new Fraction(1, 2);
    Fraction b = new Fraction(1, 4); 
    System.out.println( a + " + " + b + " = " + a.add(b) ); 
    System.out.println( a.sub(b) ); 
    System.out.println( a.greaterThan(b) ); 

  } 
}

So compile and run and maybe finish. 

Lab: the first three chapters of this book

https://www.cs.indiana.edu/classes/c212-dgerman/spr2014/ofwj5e.pdf

What about the current homework? 

Lab Ten has a summary with an example that's a good start:

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

public class Example extends JFrame implements ActionListener 
{  private JLabel nameJLabel, ageJLabel; 
   private JTextField nameJTextField, ageJTextField, totalResultJTextField;
   private JButton calculateJButton;
   public Example() 
   {  Container contentPane = getContentPane(); 
      contentPane.setLayout( null );            
      nameJLabel = new JLabel();             
      nameJLabel.setText( "Please enter your name: " );
      nameJLabel.setBounds( 16, 16, 160, 21 );
      contentPane.add( nameJLabel );
      nameJTextField = new JTextField(); 
      nameJTextField.setBounds( 170, 16, 70, 21 );
      nameJTextField.setHorizontalAlignment( JTextField.RIGHT );
      nameJTextField.setText("Laura"); 
      contentPane.add( nameJTextField );
      ageJLabel = new JLabel(); 
      ageJLabel.setText( "How old are you (in years)?" );
      ageJLabel.setBounds( 16, 48, 164, 21 );
      contentPane.add( ageJLabel );
      ageJTextField = new JTextField(); 
      ageJTextField.setText( "8" );
      ageJTextField.setBounds( 200, 48, 40, 21 );
      ageJTextField.setHorizontalAlignment( JTextField.RIGHT );
      contentPane.add( ageJTextField );
      totalResultJTextField = new JTextField(); 
      totalResultJTextField.setBounds( 244, 16, 186, 21 );
      totalResultJTextField.setHorizontalAlignment( JTextField.RIGHT );
      totalResultJTextField.setEditable( false ); 
      contentPane.add( totalResultJTextField );
      calculateJButton = new JButton(); 
      calculateJButton.setText( "Calculate Total" );
      calculateJButton.setBounds( 304, 48, 126, 24 );
      contentPane.add( calculateJButton ); 
      calculateJButton.addActionListener( this ); 
      setTitle( "Inventory" );
      setSize( 454, 132 );    
      setVisible( true );     
   } 
   public void actionPerformed(ActionEvent e) 
   {  String name = this.nameJTextField.getText(), age = this.ageJTextField.getText(); 
      this.totalResultJTextField.setText( name + " you will be " + (Integer.parseInt(age) + 1) + " next year."); 

   }
   public static void main( String[] args ) 
   {  Example application = new Example();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }


Try it. 

If you enhance this with info found in

http://www.cs.indiana.edu/classes/c212/fall2010/notes/ouch.jpg

... you have your assignment. 

Now about the lab and the exam again:

http://www.bluej.org/objects-first/

First download BlueJ

http://www.bluej.org/download/files/bluej-311.jar

http://www.bluej.org/

Then go back to the book website

http://www.bluej.org/objects-first/resources/projects.zip

Download and unpack the projects. 

Go through the first three chapters. 

Deliverable: .txt file with notes as explained. 

Drawing the eye that follows the mouse pointer: in lab notes. 

--

Second class we look at Fraction and run it:

C:\Users\dgerman\Desktop>javac Fraction.java

C:\Users\dgerman\Desktop>java Fraction
1/2 + 1/4 = 6/8
2/8
true

C:\Users\dgerman\Desktop>

Next we start answering the minute paper:

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

class One extends JFrame {
  One() {
    Two b = new Two();
    Container c = this.getContentPane(); 
    c.add(b); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    this.setDefaultCloseOperation( 3 ); 
  } 
  public static void main(String[] args) {
    One a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener {
  public void mouseMoved(MouseEvent e) {

  }
  public void mouseDragged(MouseEvent e) {

  }

  public void paintComponent(Graphics g) {

  }
}

No answer so far but we make it possible. 

Next we react properly to the mouse:

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

class One extends JFrame {
  One() {
    Two b = new Two();
    Container c = this.getContentPane(); 
    c.add(b); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    this.setDefaultCloseOperation( 3 ); 
  } 
  public static void main(String[] args) {
    One a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener {
  Two() {
    this.addMouseMotionListener(this); 
  } 
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")" ); 
  }
  public void mouseDragged(MouseEvent e) {
    System.out.println("Mouse dragged..."); 
  }

  public void paintComponent(Graphics g) {

  }
}

I suppose we need a circle next. 

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

class One extends JFrame {
  One() {
    Two b = new Two();
    Container c = this.getContentPane(); 
    c.add(b); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    this.setDefaultCloseOperation( 3 ); 
  } 
  public static void main(String[] args) {
    One a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener {
  Circle eye; 
  Two() {
    this.eye = new Circle( new Point(150, 150), 30 ); 
    this.addMouseMotionListener(this); 
  } 
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")" ); 
  }
  public void mouseDragged(MouseEvent e) {
    System.out.println("Mouse dragged..."); 
  }
  public void paintComponent(Graphics g) {
    this.eye.draw(g);  
  }
}

class Circle {
  Point center;
  int radius;
  Circle(Point c, int r) {
    this.center = c;
    this.radius = r; 
  }  
  void draw(Graphics g) {
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               this.radius * 2, 
               this.radius * 2); 
  }
}

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  }
}

I see an empty eye, how does it follow the mouse? 

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

class One extends JFrame {
  One() {
    Two b = new Two();
    Container c = this.getContentPane(); 
    c.add(b); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    this.setDefaultCloseOperation( 3 ); 
  } 
  public static void main(String[] args) {
    One a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener {
  Circle eye; 
  Line line; 
  Two() {
    this.eye = new Circle( new Point(150, 150), 30 ); 
    this.addMouseMotionListener(this); 
  } 
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")" ); 
    this.line = new Line( this.eye.center, new Point( x, y ) ); 
    this.repaint(); 
  }
  public void mouseDragged(MouseEvent e) {
    System.out.println("Mouse dragged..."); 
  }
  public void paintComponent(Graphics g) {
    this.eye.draw(g); 
    if (line != null) 
      this.line.draw(g); 
  }
}

class Circle {
  Point center;
  int radius;
  Circle(Point c, int r) {
    this.center = c;
    this.radius = r; 
  }  
  void draw(Graphics g) {
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               this.radius * 2, 
               this.radius * 2); 
  }
}

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  }
}

class Line {
  Point a, b; 
  Line(Point a, Point b) {
    this.a = a; 
    this.b = b; 
  }
  void draw(Graphics g) {
    g.drawLine(a.x, a.y, b.x, b.y); 
  } 
}

That's too basic, can I draw the line point by point. 

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

class One extends JFrame {
  One() {
    Two b = new Two();
    Container c = this.getContentPane(); 
    c.add(b); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    this.setDefaultCloseOperation( 3 ); 
  } 
  public static void main(String[] args) {
    One a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener {
  Circle eye; 
  Line line; 
  Two() {
    this.eye = new Circle( new Point(150, 150), 30 ); 
    this.addMouseMotionListener(this); 
  } 
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")" ); 
    this.line = new Line( this.eye.center, new Point( x, y ) ); 
    this.repaint(); 
  }
  public void mouseDragged(MouseEvent e) {
    System.out.println("Mouse dragged..."); 
  }
  public void paintComponent(Graphics g) {
    this.eye.draw(g); 
    if (line != null) 
      this.line.draw(g); 
  }
}

class Circle {
  Point center;
  int radius;
  Circle(Point c, int r) {
    this.center = c;
    this.radius = r; 
  }  
  void draw(Graphics g) {
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               this.radius * 2, 
               this.radius * 2); 
  }
}

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  }
}

class Line {
  Point a, b; 
  Line(Point a, Point b) {
    this.a = a; 
    this.b = b; 
  }
  void draw(Graphics g) {
    // g.drawLine(a.x, a.y, b.x, b.y); 
    for (int i = 0; i <= 10; i++) {
      int x, y;
      double t = (double) i / 10;  
      x = (int) (a.x + t * (b.x - a.x)); 
      y = (int) (a.y + t * (b.y - a.y)); 
      g.fillOval( x-3, y-3, 6, 6 ); 
    }   
  } 
}

Now the problem has been simplified a lot. 


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

class One extends JFrame {
  One() {
    Two b = new Two();
    Container c = this.getContentPane(); 
    c.add(b); 
    this.setVisible(true); 
    this.setSize(400, 400); 
    this.setDefaultCloseOperation( 3 ); 
  } 
  public static void main(String[] args) {
    One a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener {
  Circle eye; 
  Line line; 
  Two() {
    this.eye = new Circle( new Point(150, 150), 30 ); 
    this.addMouseMotionListener(this); 
  } 
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY(); 
    System.out.println( "(" + x + ", " + y + ")" ); 
    this.line = new Line( this.eye.center, new Point( x, y ) ); 
    this.repaint(); 
  }
  public void mouseDragged(MouseEvent e) {
    System.out.println("Mouse dragged..."); 
  }
  public void paintComponent(Graphics g) {
    this.eye.draw(g); 
    if (line != null) {
      this.line.draw(g);
      this.line.drawPupil(g, this.eye.radius); 
    } 
  }
}

class Circle {
  Point center;
  int radius;
  Circle(Point c, int r) {
    this.center = c;
    this.radius = r; 
  }  
  void draw(Graphics g) {
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               this.radius * 2, 
               this.radius * 2); 
  }
}

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

class Line {
  Point a, b; 
  Line(Point a, Point b) {
    this.a = a; 
    this.b = b; 
  }
  void draw(Graphics g) {
    // g.drawLine(a.x, a.y, b.x, b.y); 
    for (int i = 0; i <= 10; i++) {
      int x, y;
      double t = (double) i / 10;  
      x = (int) (a.x + t * (b.x - a.x)); 
      y = (int) (a.y + t * (b.y - a.y)); 
      g.fillOval( x-3, y-3, 6, 6 ); 
    }   
  } 
  double length() {
   return this.a.distanceTo(this.b); 
  }
  void drawPupil(Graphics g, int r) {
    int x, y;
    double t = (double) r / this.length();  
    x = (int) (a.x + t * (b.x - a.x)); 
    y = (int) (a.y + t * (b.y - a.y)); 
    g.setColor(Color.RED); 
    g.fillOval( x-6, y-6, 12, 12 ); 
  }
}

So we now have the eye. Ha! Ha!