/**
 * 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.* ;

/* we use a Model-View-Controller pattern, with
 * an empty Model and an empty Controller
 */

public class FirstSwingTest {

  static final String FRAME_TITLE = "FirstSwingTest" ;
  static final int FRAME_X = 600 ;
  static final int FRAME_Y = 400 ;

  public static void main(String[] args) {

    JFrame jf = new JFrame(FRAME_TITLE) ;  // top-level container
    JPanel mv = new MyView() ;
    jf.getContentPane().add(mv) ;  // a view is a panel, attach panel to container
    jf.setSize(FRAME_X, FRAME_Y) ;
    jf.setVisible(true) ;           // make visible

  }
}

class MyView extends JPanel {    // provides canvas to override "paint"

  private static final int X_ORG = 40 ;
  private static final int Y_ORG = 40 ;
  private static final int BAR_HEIGHT = 20 ;

  // blue square is 3x3 (in bar-height).
  // flag is 5x6 (in bar-height)

  // each entry in RECTANGLE is ( x-org, y-org, width, height )
  // in units of BAR_HEIGHT, and (x-org, y-org) is relative
  // to (X_ORG, Y_ORG)
  private static final int [][] RED_RECTANGLES =
     { { 3,0,4,1 }, { 3,2,4,1 }, { 0,4,7,1 } } ;
  private static final int [][] WHITE_RECTANGLES =
     { { 3,1,4,1  }, { 0,3,7,1 } } ;
  private static final int [][] BLUE_RECTANGLES =
     { { 0,0,3,3  } } ;

  public void paintComponent(Graphics gc) {        // call-back on re-draw
      super.paintComponent(gc) ;
      gc.setColor(Color.red) ;
      for ( int i=0;i<RED_RECTANGLES.length;i++ )
         myDrawRect( gc, RED_RECTANGLES[i] ) ;
      gc.setColor(Color.white) ;
      for ( int i=0;i<WHITE_RECTANGLES.length;i++ )
         myDrawRect( gc, WHITE_RECTANGLES[i] ) ;
      gc.setColor(Color.blue) ;
      for ( int i=0;i<BLUE_RECTANGLES.length;i++ )
         myDrawRect( gc, BLUE_RECTANGLES[i] ) ;
  }

  private void myDrawRect( Graphics g, int [] r ) {  // private helper
      g.fillRect( X_ORG+BAR_HEIGHT*r[0], Y_ORG+BAR_HEIGHT*r[1],
                    BAR_HEIGHT*r[2], BAR_HEIGHT*r[3] ) ;
  }

}
