Bonjour. 

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

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

Most difficult problem survey: P6.27 (5), P6.28 (4), P6.2 (2), P6.18 (2), 
P6.11 (2), P6.19, P6.7, P6.24, R6.4, R6.31, R6.29, actually most of them.

Let's get started. 

My first attempt is creative and wrong:

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

public class Game extends JFrame {
  public Game() {
    // super();  
    this.setVisible( true ); 
    this.setSize( 500, 500 ); 
    Container c = this.getContentPane(); 
    c.add( new Screen() ); 
  }
  public static void main(String[] args) {
    JFrame f = new Game(); // polymorphism  
  }
}

That was unchanged. How about the other one: 

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

public class Screen extends JComponent implements MouseMotionListener {
  public Screen() { // constructor chaining
    super(); 
    this.addMouseMotionListener( this ); 
  }
  public void paintComponent(Graphics g) {
      g.drawString("This is great.", 50, 50);

  }
  public void mouseDragged(MouseEvent e) { }
  public void mouseMoved(MouseEvent e) { 
    int x = e.getX(), y = e.getY(); 
    Graphics g = this.getGraphics(); 
    g.drawString("This is great.", x, y);
  }
}

So this is flawed and I need to ackowledge the MVC pattern existing. 

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

public class Game extends JFrame {
  public Game() {
    // super();  
    this.setVisible( true ); 
    this.setSize( 500, 500 ); 
    Container c = this.getContentPane(); 
    c.add( new Screen() ); 
  }
  public static void main(String[] args) {
    JFrame f = new Game(); // polymorphism  
  }
}

That is still unchanged but here's a good Screen now:


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

public class Screen extends JComponent implements MouseMotionListener {
  int x=-1, y=-1; // instance variables initialized a 0 
  public Screen() { // constructor chaining
    super(); 
    this.addMouseMotionListener( this ); 
  }
  public void paintComponent(Graphics g) {
    if (this.x < 0 || this.y < 0) {
      
    } else {
      g.drawString("(" + this.x + ", " + this.y + ")", this.x, this.y);
    }
  }
  public void mouseDragged(MouseEvent e) { }
  public void mouseMoved(MouseEvent e) { 
    this.x = e.getX();
    this.y = e.getY(); 
    this.repaint(); // clears then redraws 
  }
}

Now I want to leave circles behind. 

How do I use the active MVC pattern for that? 

Mark, Daniel, Jared, Nathan, Trevor, Seth, Judy, Hallie, 
Adam, Gabriela, James, Brennan, Jacquelyn, Nick, Yiming, 
Jingzhe, Zac, Paul, Morgan, Alex, William, Lauren, Jack,
Mohan, Grant, MAR, Jon. 

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

public class Game extends JFrame {
  public Game() {
    // super();  
    this.setVisible( true ); 
    this.setSize( 500, 500 ); 
    Container c = this.getContentPane(); 
    c.add( new Screen() ); 
  }
  public static void main(String[] args) {
    JFrame f = new Game(); // polymorphism  
  }
}

import java.awt.*;

public class Point {
  private int x, y;  
  public Point(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  public int getX() {
    return this.x;  
  }
  public int getY() {
    return this.y;  
  }
  public void draw(Graphics g) {
    g.drawOval(this.x, this.y, 10, 10);  
  }
}

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

public class Screen extends JComponent implements MouseMotionListener {
  ArrayList<Point> points; // null originally 
  public Screen() { // constructor chaining
    super(); 
    this.addMouseMotionListener( this ); 
    this.points = new ArrayList<Point>(); 
  }
  public void paintComponent(Graphics g) {
    if (this.points == null) {
      
    } else {
      for (Point p: this.points) {
        p.draw(g);  
      }
    }
  }
  public void mouseDragged(MouseEvent e) { 
    this.points.add( new Point(e.getX(), e.getY()) ); 
    this.repaint(); // clears then redraws   
  }
  public void mouseMoved(MouseEvent e) { 

  }
}

Now if the points are 2x2 let's draw lines between consecutive points...

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

public class Game extends JFrame {
  public Game() {
    // super();  
    this.setVisible( true ); 
    this.setSize( 500, 500 ); 
    Container c = this.getContentPane(); 
    c.add( new Screen() ); 
  }
  public static void main(String[] args) {
    JFrame f = new Game(); // polymorphism  
  }
}

import java.awt.*;

public class Point {
  private int x, y;  
  public Point(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  public int getX() {
    return this.x;  
  }
  public int getY() {
    return this.y;  
  }
  public void draw(Graphics g) {
    g.drawOval(this.x, this.y, 2, 2);  
  }
}

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

public class Screen extends JComponent implements MouseMotionListener {
  ArrayList<Point> points; // null originally 
  public Screen() { // constructor chaining
    super(); 
    this.addMouseMotionListener( this ); 
    this.points = new ArrayList<Point>(); 
  }
  public void paintComponent(Graphics g) {
    if (this.points == null) {
      
    } else {
      for (int i = 0; i < this.points.size() - 2; i++) {
        Point a = this.points.get(i); 
        Point b = this.points.get(i+1); 
        g.drawLine( a.getX(), a.getY(), b.getX(), b.getY() );
      }
    }
  }
  public void mouseDragged(MouseEvent e) { 
    this.points.add( new Point(e.getX(), e.getY()) ); 
    this.repaint(); // clears then redraws   
  }
  public void mouseMoved(MouseEvent e) { 

  }
}

How do you fix the last bit? 

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

--