import javax.swing.JFrame;

public class Example {
  public static void main(String[] args) {
    JFrame f = new JFrame(); 
    f.add(new Screen()); 
    f.setVisible(true); 
    f.setSize(400, 400); 
  }
}

--

import javax.swing.JComponent; 
import java.awt.Graphics; 
import java.util.ArrayList; 
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;

public class Screen extends JComponent implements MouseListener, MouseMotionListener {
  City current; 
  public void mouseMoved(MouseEvent e) { 
    System.out.println( this.current ); 
  } 
  public void mouseDragged(MouseEvent e) { 
    if (this.current != null) {
      int x = e.getX(), y = e.getY(); 
      Point mouseLocation = new Point(x, y); 
      this.current.moveTo( mouseLocation ); 
      this.repaint();
    }
    System.out.println( this.current ); 
  } 
  public void mouseEntered(MouseEvent e) { } 
  public void mouseExited(MouseEvent e) { } 
  public void mousePressed(MouseEvent e) { 
    int x = e.getX(), y = e.getY(); 
    Point mouseLocation = new Point(x, y); 
    for (City c : this.graph) {
      double d = c.location.distanceTo(mouseLocation);  
      // System.out.println( "Distance to " + c + " from your mouse is: " + d ); 
      if ( d < 20 ) {
        this.current = c; 
        // return;
        break; 
      }
    }
  } 
  public void mouseReleased(MouseEvent e) { 
     this.current = null; 
  } 
  public void mouseClicked(MouseEvent e) { } 
  ArrayList<City> graph = new ArrayList<City>(); 
  public Screen() {
    City a = new City("San Francisco", new Point( (int) (Math.random() * 300 + 50), 
                                                  (int) (Math.random() * 300 + 50) )); 
    City b = new City("Los Angeles", new Point( (int) (Math.random() * 300 + 50), 
                                                (int) (Math.random() * 300 + 50) )); 
    City c = new City("Portland", new Point( (int) (Math.random() * 300 + 50), 
                                             (int) (Math.random() * 300 + 50) )); 
    City d = new City("Salt Lake City", new Point( (int) (Math.random() * 300 + 50), 
                                                   (int) (Math.random() * 300 + 50) )); 
    City e = new City("Las Vegas", new Point( (int) (Math.random() * 300 + 50), 
                                              (int) (Math.random() * 300 + 50) )); 

    e.addNeighbor(d); 
    e.addNeighbor(b); 
    
    a.addNeighbor(b); 
    a.addNeighbor(c); 
    a.addNeighbor(d);
    b.addNeighbor(a);
    c.addNeighbor(a);
    d.addNeighbor(a);
    this.graph.add(a);
    this.graph.add(b);
    this.graph.add(c);
    this.graph.add(d);
    this.graph.add(e); 
    this.addMouseMotionListener(this); 
    this.addMouseListener(this); 
  }
  public void paintComponent(Graphics g) {
    // g.drawString("Howdy", 100, 200); 
    for (City c : this.graph) {
      c.draw(g);  
    }
  }
}

--

import java.awt.Graphics;
import java.util.ArrayList; 

public class City {
  public String toString() { return this.name; } 
  String name;
  Point location; 
  ArrayList<City> neighbors; 
  public City(String name, Point location) {
    this.name = name; 
    this.location = location; 
    this.neighbors = new ArrayList<City>();
  }
  public void draw(Graphics g) {
    g.drawString( this.name, this.location.x, this.location.y ); 
    for (City c : this.neighbors) { 
      g.drawLine( this.location.x, this.location.y, c.location.x, c.location.y); 
    }            
  }
  public void addNeighbor(City a) {
    this.neighbors.add(a); 
  }
  public void moveTo(Point somewhere) {
    this.location = somewhere;  
  }
}

--

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

--

Ben Chun Mary Murun JeVante Qi Zack Sai Nariman Dustin Daohui 
Sunghyun Grant Chase Jiongran Michael Namit LaBrian Young Hwan 
Peter W Peter F Emma Stephen K 

In lab today: Homework 08.