ZooMap.java



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

  /** 動物の住居のクラス */
    public AnimalHouse[] houses;

  /** コンストラクタ */
    public ZooMap() {

        setLayout( null );

        Color bg = new Color( 0.3f, 0.6f, 0.0f );
        setBackground( bg );

        Animal[] anis = {
                           new Elephant( "Hanako", 7 ),
                           new Elephant( "Tarou", 8 ),
                           new Elephant( "Gonta", 1 ),
                           new Panda( "Wakwak", 6 ),
                           new Panda( "Hokhok", 5 )
                          };
        AnimalBoard[] boards
           = new AnimalBoard[ anis.length ];
        for( int i=0; i<anis.length; i++ ) {
            boards[i] = new AnimalBoard( anis[i] );
        }
        Point[] poses = {
                           new Point( 100, 100 ),
                           new Point( 500, 200 ),
                           new Point( 300, 400 ),
                           new Point( 50, 450 ),
                           new Point( 600, 350 ),
                         };

        houses = new AnimalHouse[ anis.length ];
        for( int i=0; i<anis.length; i++ ) {
            houses[i] = new AnimalHouse( anis[i],
                               boards[i], poses[i] );
            add( boards[i] );
        }
    }

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

  /** 実行時に最初に呼び出されるメソッド */
    public static void main( String[] argv ) {
        ZooMap map = new ZooMap();
        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();
    }
}