Exams will come to class on Thu

Fall Break

Thu lab is optional 

No lab assignment this week

New homework assignment to be posted by Thu, will be due next Thu -- chapter 10 (GUIs etc)

Today: explore Java

http://docs.oracle.com/javase/6/docs/api/javax/swing/JFrame.html

http://docs.oracle.com/javase/tutorial/

http://docs.oracle.com/javase/tutorial/reallybigindex.html

http://www.cs.indiana.edu/classes/c212/spr2011/notes/l23.html

With what I know so far I can write a class that extends JFrame. 

Do you remember the composition of a Triangle? 

We started with modeling Points as pairs of ints. 

Model a Line as a pair of two Points.

A Triangle needs to be created from three Points for the sake of the customer. 

However we need to calculate the area and we do that with Heron's formula. 

So turn the Points into Lines so you have them as sides. 

In my application I want put a JComponent inside the JFrame. 

I also want to be able to react to the mouse input: 

http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseMotionListener.html

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

class One extends JFrame {
  One() {
    Two a = new Two(); 
    Container hook = this.getContentPane(); 
    hook.add( a ); 
    this.setVisible(true); 
    this.setSize(300, 500); 
    this.setDefaultCloseOperation( EXIT_ON_CLOSE ); 
  }
  public static void main(String[] args) {
    JFrame a = new One(); 
  } 
}

class Two extends JComponent {
  int count = 0; 
  Two() {
    this.addMouseMotionListener(new Three()); 
  } 
  public void paintComponent(Graphics g) {
    this.count += 1; 
    System.out.println( "I am here." + this.count); 
  } 
}

class Three implements MouseMotionListener {
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY();  
    System.out.println("Mouse being moved at (" + x + ", " + y + ")"); 
  }
  public void mouseDragged(MouseEvent e) { 
    System.out.println("Mouse being dragged "); 
  }
}

Geoff let's draw, so I change my style a little bit. 

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

class One extends JFrame {
  One() {
    Two a = new Two(); 
    Container hook = this.getContentPane(); 
    hook.add( a ); 
    this.setVisible(true); 
    this.setSize(300, 500); 
    this.setDefaultCloseOperation( EXIT_ON_CLOSE ); 
  }
  public static void main(String[] args) {
    JFrame a = new One(); 
  } 
}

class Two extends JComponent implements MouseMotionListener {
  int count = 0; 
  Two() {
    this.addMouseMotionListener(this); 
  } 
  public void paintComponent(Graphics g) {
    this.count += 1; 
    System.out.println( "I am here." + this.count); 
  } 
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY();  
    System.out.println("Mouse being moved at (" + x + ", " + y + ")"); 
    Graphics g = this.getGraphics(); 
    g.drawOval(x, y, 10, 10); 
  }
  public void mouseDragged(MouseEvent e) { 
    System.out.println("Mouse being dragged "); 
  }
}

TMTOWTDI

Now let's draw. 

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

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

class Circle {
  Point center;
  int radius; 
  Color color; 
  Circle( Point center, int radius, Color color ) {
    this.center = center; 
    this.radius = radius; 
    this.color = color; 
  } 
  void draw(Graphics g) {
    g.setColor(this.color);
    g.fillOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               this.radius * 2, 
               this.radius * 2);    
    g.setColor(Color.BLACK);
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               this.radius * 2, 
               this.radius * 2);    

  }
}

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

class Two extends JComponent {
  ArrayList<Circle> circles; 
  Two() {
    this.circles = new ArrayList<Circle>(); 
    for (int i = 0; i < 10; i = i + 1) {
      int x = (int) (Math.random() * 400 + 100);
      int y = (int) (Math.random() * 400 + 100);
      Circle c = new Circle( new Point(x, y), 
                             (int) (Math.random() * 40 + 20), 
                             new Color( 
                               (int) (Math.random() * 255), 
                               (int) (Math.random() * 255), 
                               (int) (Math.random() * 255) 
                             )
                           ); 
      this.circles.add( c );
    } 
  }
  public void paintComponent(Graphics g) {
    for (Circle c : this.circles) {
      c.draw( g ); 
    }
  } 
}

--