Welcome to the lab!

In this lab we will develop something together. 

It will take about 30 minutes. Then you will be asked to extend it. 

Submit your code by Monday 11:30am in OnCourse. 

Let's get started: 

import javax.swing.JFrame; 

class One extends JFrame {
  One(int number) {
    this.getContentPane().add( new Two( number ) ); 
    this.setVisible(true); 
    this.setSize(500, 500); 
  }
  public static void main(String[] args) {
    JFrame one = new One(Integer.parseInt(args[0]));  
  }
}

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

class Two extends JComponent implements MouseListener, MouseMotionListener {
  Two(int number) {
    
  }
  public void mouseDragged (MouseEvent e) { }     
  public void mouseMoved   (MouseEvent e) { }     
  public void mousePressed (MouseEvent e) { }     
  public void mouseReleased(MouseEvent e) { }     
  public void mouseClicked (MouseEvent e) { }     
  public void mouseEntered (MouseEvent e) { }     
  public void mouseExited  (MouseEvent e) { }     
  public void paintComponent(Graphics g) {
    g.drawString("This is me.", 50, 50); 
  }
}

So this has the entire structure but only little function. 

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

import java.awt.*; 

abstract class Shape {
  Point center;
  Shape(Point center) {
    this.center = center; 
  }
  abstract void draw(Graphics g);  
}

import java.awt.*; 

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

import java.awt.event.*;

class Two extends JComponent implements MouseListener, MouseMotionListener {
  Shape[] shapes;
  Two(int number) {
    this.shapes = new Shape[number];
    for (int i = 0; i < this.shapes.length; i = i + 1) {
      this.shapes[i] = new Circle( new Point( (int) (500 * Math.random()), 
                                              (int) (500 * Math.random())), 
                                   (int) (10 + 40 * Math.random()) , 
                                   new Color( (float) (Math.random()),
                                              (float) (Math.random()),
                                              (float) (Math.random())) );  
    }
    this.addMouseMotionListener( this ); 
    this.addMouseListener( this ); 
  }
  public void mouseDragged (MouseEvent e) { }     
  public void mouseMoved   (MouseEvent e) { }     
  public void mousePressed (MouseEvent e) { }     
  public void mouseReleased(MouseEvent e) { }     
  public void mouseClicked (MouseEvent e) { }     
  public void mouseEntered (MouseEvent e) { }     
  public void mouseExited  (MouseEvent e) { }     
  public void paintComponent(Graphics g) {
    for (Shape c : this.shapes) {
      c.draw(g);  
    }
  }
}

import javax.swing.JFrame; 

class One extends JFrame {
  One(int number) {
    this.getContentPane().add( new Two( number ) ); 
    this.setVisible(true); 
    this.setSize(500, 500); 
  }
  public static void main(String[] args) {
    JFrame one = new One(Integer.parseInt(args[0]));  
  }
}

Now I have circles. 

I end up with this:

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

// Shape.java file 
import java.awt.*; 

abstract class Shape {
  Point center;
  Shape(Point center) {
    this.center = center; 
  }
  abstract void draw(Graphics g);  
  void move(int x, int y) {
    this.center = new Point(x, y);  
  }
  abstract boolean contains(int x, int y); 
}

// Circle.java file 

import java.awt.*; 

class Circle extends Shape {
  int radius;
  Color color; 
  Circle(Point center, int radius, Color color) {
    super( center );
    this.radius = radius;
    this.color = color; 
  }
  void draw(Graphics g) {
    g.setColor(this.color); 
    g.fillOval(this.center.x, this.center.y, this.radius * 2, this.radius * 2); 
    g.setColor(Color.BLACK);  
    g.drawOval(this.center.x, this.center.y, this.radius * 2, this.radius * 2); 
  }
  boolean contains(int x, int y) { 
    Point mouse = new Point(x, y); 
    return center.distanceTo(mouse) <= this.radius; 
  }
}

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

class Two extends JComponent implements MouseListener, MouseMotionListener {
  Shape[] shapes;
  Two(int number) {
    this.shapes = new Shape[number];
    for (int i = 0; i < this.shapes.length; i = i + 1) {
      this.shapes[i] = new Circle( new Point( (int) (500 * Math.random()), 
                                              (int) (500 * Math.random())), 
                                   (int) (10 + 40 * Math.random()) , 
                                   new Color( (float) (Math.random()),
                                              (float) (Math.random()),
                                              (float) (Math.random())) );  
    }
    this.addMouseMotionListener( this ); 
    this.addMouseListener( this ); 
  }
  public void mouseDragged (MouseEvent e) { 
    int x = e.getX(), y = e.getY(); 
    if (this.current != null) {
      this.current.move(x, y);
      this.repaint(); 
    }
  }     
  public void mouseMoved   (MouseEvent e) { }     
  Shape current; 
  public void mousePressed (MouseEvent e) {
    int x = e.getX(), y = e.getY(); 
    for (int i = 0; i < this.shapes.length; i = i + 1) {
      if (this.shapes[i].contains(x, y)) {
        this.current = this.shapes[i];  
        break; 
      }
    }
  }     
  public void mouseReleased(MouseEvent e) { 
    this.current = null; 
  }     
  public void mouseClicked (MouseEvent e) { }     
  public void mouseEntered (MouseEvent e) { }     
  public void mouseExited  (MouseEvent e) { }     
  public void paintComponent(Graphics g) {
    for (Shape c : this.shapes) {
      c.draw(g);  
    }
  }
}

// One.java 
import javax.swing.JFrame; 

class One extends JFrame {
  One(int number) {
    this.getContentPane().add( new Two( number ) ); 
    this.setVisible(true); 
    this.setSize(500, 500); 
  }
  public static void main(String[] args) {
    JFrame one = new One(Integer.parseInt(args[0]));  
  }
}

Your task is to add Rectangles to this. 

Lab is due on Monday. Adding Rectangles is a 15-20 minutes task. 

Let us know if you need any help. 

--