ZooMap.java



/** 動物園の地図のクラス */
import java.awt.*;
public class ZooMap extends Panel {

  /** 動物の案内板のクラス */
    public AnimalBoard[] boards;

  /** 地図上の位置 */
    public Point[] points;

  /** コンストラクタ */
    public ZooMap( Animal[] animals, Point[] points ) {

        setLayout( null );
        this.points = points;

        Color bg = new Color( 0.3f, 0.6f, 0.0f );
        setBackground( bg );
        
        Color efg = new Color( 255, 255, 245 );
        Color ebg = new Color( 128, 128, 128 );
        Font efont = new Font( "TimesRoman", Font.BOLD, 24 );

        Color pfg = new Color( 255, 255, 255 );
        Color pbg = new Color( 0, 0, 0 );
        Font pfont = new Font( "TimesRoman", Font.ITALIC, 24 );

        boards = new AnimalBoard[ animals.length ];
        for( int i=0; i<animals.length; i++ ) {
            if( animals[i] instanceof Elephant ) {
                boards[i]
                  = new AnimalBoard( animals[i], efg, ebg, efont );
            }
            else if( animals[i] instanceof Panda ) {
                boards[i]
                  = new AnimalBoard( animals[i], pfg, pbg, pfont );
            }
            add( boards[i] );
        }
    }

  /** 位置の設定のメソッド */
    public void doLayout() {
        for( int i=0; i<boards.length; i++ ) {
            Dimension d = boards[i].getPreferredSize();
            boards[i].setSize( d.width, d.height );
            boards[i].doLayout();
            boards[i].setLocation( points[i] );
        }
    }

  /** 実行時に最初に呼び出されるメソッド */
    public static void main( String[] argv ) {

        Animal[] animals = {
                           new Elephant( "Hanako", 7 ),
                           new Elephant( "Tarou", 8 ),
                           new Elephant( "Gonta", 1 ),
                           new Panda( "Wakwak", 6 ),
                           new Panda( "Hokhok", 5 )
                          };
        Point[] points = {
                           new Point( 100, 100 ),
                           new Point( 400, 200 ),
                           new Point( 300, 400 ),
                           new Point( 50, 450 ),
                           new Point( 500, 350 ),
                         };
        ZooMap map = new ZooMap( animals, points );

        Frame frame = new Frame( "Map of the Zoo" );
        frame.setLayout( new GridLayout(1,1) );
        frame.add( map );
        frame.setSize( 800, 600 );
        map.doLayout();
        frame.show();
    }
}