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

  public class DrawingCanvasView extends JComponent
    implements ModelListener {

  protected MiniDrawModel model;
  protected DrawingCanvasController DCcontroller;

  /* constructor */
  public DrawingCanvasView(MiniDrawModel m) {
    setBackground(Color.white);
    model = m;
    DCcontroller = createDrawingCanvasController();
    addDrawingCanvasListener(DCcontroller);
    model.addModelListener(this);
  }
  
  protected DrawingCanvasController
               createDrawingCanvasController() {
      return new DrawingCanvasController(this, model);
    }


  protected void addDrawingCanvasListener(EventListener listener) {
    addMouseListener((MouseListener) listener);
    addMouseMotionListener((MouseMotionListener) listener);
  }
  
  public void ModelUpdated() {
    repaint();
    
  }

  public void update(Graphics g){
     paint(g);
  }
  public void paint(Graphics g) {
    g.drawImage(model.getimageBuffer(),0, 0, this);
  }
  
  public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x, y, width, height);
    /* 
       Minor kludge here.  Ideallly we'd just like to pass
       the new canvas size to the model and let the model
       instatiate a new image buffer.  But, it will be difficult
       to impossible to instantiate a new image with properties
       (background, transparency, etc) that exactly match those
       of the displayed image.  To get around this, we use the
       creatImage method of this component to instatniate the
       new imageBuffer.  the createImage method will instatntiate
       a new image buffer with properties that exactly match the
       displayed image.  Then we must pass this new image to the
       model.  Not pretty, but it works.
     */
    Image newimageBuffer = createImage(width, height);
    model.resizeCanvas(newimageBuffer, width, height);
  }
}
