/* DiagramPanel */
/* Simeon Warner - 3June96 */

import java.awt.*;

public class DiagramPanel extends Canvas {
  //double-buffering
  private Image image2;
  private Graphics g2;
  
  private Dimension mySize;
  private Configuration conf;

  DiagramPanel() {
  }

  public void init(Configuration c) {
    conf = c;
    setBackground(Color.white);
    repaint();
  }


  public boolean handleEvent(Event evt) {
    switch (evt.id) {
    case Event.MOUSE_DOWN:
      return true;
    case Event.MOUSE_DRAG:
      return true;
    case Event.MOUSE_UP:
      return true;
    case Event.MOUSE_MOVE:
      return true;
    case Event.WINDOW_DEICONIFY:
    case Event.WINDOW_MOVED:
      g2=null;
      return true;
    default:
      return false;
    }
  }


  public void update(Graphics g) {
    paint(g);
  }


  public void paint(Graphics g) {
    int[] facex = new int[4];
    int[] facey = new int[4];
    /* If the second image doesn't exist then we must now create it, the
       size method will now give sensible answers so we may also set the
       default positions based on panel size.  It would be nicer to put 
       this bit of code elsewhere */
    if (g2 == null) paintSetup();
    else if ((this.size().width!=mySize.width) ||
             (this.size().height!=mySize.height)) paintSetup();
    /* now lets paint a picture to the second buffer (g2) and then copy */
    g2.setColor(this.getBackground());
    g2.fillRect(0,0,mySize.width,mySize.height);
    g2.setColor(Color.blue);
    /* The real guts goes here, first setup new drawing */
    conf.newPaint(mySize.width,mySize.height);
    /* second, get all faces and draw wireframe */
    while (conf.nextFace(facex,facey)) {
      g2.setColor(this.getBackground());
      g2.fillPolygon(facex,facey,4);
      g2.setColor(Color.blue);
      g2.drawPolygon(facex,facey,4);
      //g2.drawLine(face[0],face[1],face[2],face[3]);
      //g2.drawLine(face[0],face[1],face[4],face[5]);
      //g2.drawLine(face[2],face[3],face[4],face[5]);
    }
    /* Copy image to live version */
    g.drawImage(image2,0,0,this); 
  }
 

  private void paintSetup() {
    mySize=this.size();
    /* Create second image space */
    image2 = createImage(mySize.width,mySize.height);
    g2=image2.getGraphics();
  }

} /*end of DiagramPanel class*/




