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 );

        
        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 );

        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++ ) {
            if( anis[i] instanceof Elephant ) {
                boards[i]
                  = new AnimalBoard( anis[i], efg, ebg, efont );
            }
            else if( anis[i] instanceof Panda ) {
                boards[i]
                  = new AnimalBoard( anis[i], pfg, pbg, pfont );
            }
        }
        Point[] poses = {
                           new Point( 100, 100 ),
                           new Point( 400, 200 ),
                           new Point( 300, 400 ),
                           new Point( 50, 450 ),
                           new Point( 500, 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();
    }
}