http://www.cs.indiana.edu/classes/c212/sum2014/patterns.pdf

import javax.swing.JFrame;

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

import javax.swing.JComponent; 
import java.awt.Color;
import java.awt.Graphics; 
// http://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html
class Two extends JComponent {
  public void paintComponent(Graphics g) {
    g.setColor(Color.YELLOW); 
    g.fillOval(30, 30, 40, 40); 
    g.setColor(Color.BLUE); 
    g.drawOval(30, 30, 40, 40);  
  }
}

I want to add Circles when I click with the mouse.

Caulin, Longhua, Chris, Aaron, Brian, Mitch, Tyler, Ke, Timmy, 
Sara, Xinhui, Yiming, Daozhen, Yifan, John, Roy,
Andrew, Kasey, Collin, Anthony, Brandon

             Ingrid                     Ben L
Victor, Sam, Annette, Xinya, Nathaniel Scott Shipman, Hunter, Rob, Jacob, Shuanghao
Tristan, Ben L, Abby, Jack, Ivy, Carson, 
Sergio, John, Joseph, Drake, Duncan

import javax.swing.JFrame;

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

import javax.swing.JComponent; 
import java.awt.Color;
import java.awt.Graphics; 
// http://docs.oracle.com/javase/8/docs/api/javax/swing/JComponent.html
import java.awt.event.*; 
class Two extends JComponent implements MouseListener, MouseMotionListener {
  Two() {
    super(); 
    this.addMouseListener( this ); 
    this.addMouseMotionListener( this ); 
  }
  public void mouseMoved(MouseEvent m) { }

  String message = "";  // these two represent my model 
  int x, y; 

  public void mouseDragged(MouseEvent m) { 
    // g.drawString("Howdy.", m.getX(), m.getY()); 
    this.x = m.getX(); 
    this.y = m.getY(); 
    this.message = "(" + this.x + ", " + this.y + ")"; 
    this.repaint();
  }
  public void mouseEntered(MouseEvent m) { }
  public void mouseExited(MouseEvent m) { }
  public void mousePressed(MouseEvent m) { 
    System.out.println("Ouch."); 
  }
  public void mouseReleased(MouseEvent m) { }
  public void mouseClicked(MouseEvent m) { }
  public void paintComponent(Graphics g) {
    g.setColor(Color.YELLOW); 
    g.fillOval(30, 30, 40, 40); 
    g.setColor(Color.BLUE); 
    g.drawOval(30, 30, 40, 40);  
    g.setColor(Color.BLACK); 
    g.drawString(this.message, this.x, this.y); 
  }
}

I start extending this model:

import java.awt.Graphics; 
import java.awt.Color; 

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

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

How do we want this to look in the end? 

See the notes for today (both Racket and Java):

http://www.cs.indiana.edu/classes/c212-dgerman/fall2015/lectureFifteen.html

See you next time!

--

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

class BigBang extends JComponent implements KeyListener, ActionListener, MouseListener {
  Timer timer; 
  World world; 
  BigBang(int delay, World world) {
    timer = new Timer(delay, this); 
    this.world = world;
  } 
  public void start() {
    timer.start();  
  }
  BigBang(World world) {
    this(1000, world);  
  }
  public void paintComponent(Graphics g) {
    world.draw(g);  
  }
  public void actionPerformed(ActionEvent e) {
    world.update(); 
    if (world.hasEnded())
      timer.stop(); 
    this.repaint(); 
  }
  public void keyPressed(KeyEvent e) { 
    world.keyPressed(e); 
    this.repaint(); 
  } 
  public void keyTyped(KeyEvent e) { } 
  public void keyReleased(KeyEvent e) { } 

  public void mousePressed(MouseEvent e) { 
    world.mousePressed(e); 
    this.repaint(); 
  } 
  public void mouseReleased(MouseEvent e) { } 
  public void mouseClicked(MouseEvent e) { } 
  public void mouseEntered(MouseEvent e) { } 
  public void mouseExited(MouseEvent e) { } 

}

import java.awt.*; 
import java.awt.event.*; 

interface World  {
  public void draw(Graphics g);
  public void update(); 
  public boolean hasEnded();
  public void keyPressed(KeyEvent e); 
  public void mousePressed(MouseEvent e); 
}

// Ripples.java 

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

public class Ripples implements World {
  ArrayList<Circle> circles = new ArrayList<Circle>(); 
  public void update() {
    for (Circle c : this.circles) 
      c.enlarge(); 
  }
  public void draw(Graphics g) {
    for (Circle c : this.circles) 
      c.draw(g); 
  }
  public void mousePressed(MouseEvent e) { 
    int x = e.getX(), y = e.getY(); 
    this.circles.add( new Circle( x, y, 1, Color.RED ) ); 
  } 
  public void keyPressed(KeyEvent e) { }
  public static void main(String[] args) {
    BigBang game = new BigBang(30, new Ripples());  
    JFrame frame = new JFrame("Ripples"); 
    frame.getContentPane().add( game ); 
    game.addMouseListener( game ); 
    frame.setVisible(true); 
    frame.setSize(400, 400); 
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
    game.start(); 
  }
  public boolean hasEnded() {
    return false; // never!! 
  }
}// Ripples.java 

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

public class Ripples implements World {
  ArrayList<Circle> circles = new ArrayList<Circle>(); 
  public void update() {
    for (Circle c : this.circles) 
      c.enlarge(); 
  }
  public void draw(Graphics g) {
    for (Circle c : this.circles) 
      c.draw(g); 
  }
  public void mousePressed(MouseEvent e) { 
    int x = e.getX(), y = e.getY(); 
    this.circles.add( new Circle( x, y, 1, Color.RED ) ); 
  } 
  public void keyPressed(KeyEvent e) { }
  public static void main(String[] args) {
    BigBang game = new BigBang(30, new Ripples());  
    JFrame frame = new JFrame("Ripples"); 
    frame.getContentPane().add( game ); 
    game.addMouseListener( game ); 
    frame.setVisible(true); 
    frame.setSize(400, 400); 
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 
    game.start(); 
  }
  public boolean hasEnded() {
    return false; // never!! 
  }
}

// Circle.java 

import java.awt.*; 

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

Run this and it's like the C211 Ripples. Furthermore we understand most of it!

--