package secondswing;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author Burton Rosenberg
 * @version 9 April 2002 [creation date: 9 April 2002]
 */

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

// creates View of the Model (the flag)
// gets the current color and the current rectangles for Model
// (the flag) from the Controller.

class MyPicturePanel extends JPanel {

  MyController mc ;
  int xOrg ;
  int yOrg ;
  int barHeight ;

  MyPicturePanel( MyController mc, int [] geometry ) {
     this.mc = mc ;
     xOrg = geometry[0] ;
     yOrg = geometry[1] ;
     barHeight = geometry[2] ;
  }

  public void paintComponent(Graphics gc) {

      super.paintComponent(gc) ;

      // get current colors from Controller
      Color [] c = mc.currentColors() ;
      // get an array of rectangles from Controller
      int [][][] currentRectangles = mc.currentRectangles() ;

      for ( int j=0; j<currentRectangles.length; j++ ) {
         // set the color for these rectangles
         gc.setColor(c[j]) ;
         // consider the array of rectangels in this color
         int [][] theseRectangles = currentRectangles[j] ;
         for ( int i=0;i<theseRectangles.length;i++ )
            myDrawRect( gc, theseRectangles[i] ) ;
      }
  }

  private void myDrawRect( Graphics g, int [] r ) {
      // r is the four integers needed to describe a rectangle.
      // scale and offset these integers for exact window size
      g.fillRect( xOrg+barHeight*r[0], yOrg+barHeight*r[1],
                    barHeight*r[2], barHeight*r[3] ) ;
  }

}

