FileIcon.java



import java.awt.Graphics;
import java.awt.Label;
import java.awt.Canvas;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.io.File;

/** ファイルのアイコンのクラス */

public class FileIcon extends Panel {

  /** 使用するレイアウトのオブジェクト */

       protected BorderLayout lay;

  /** 記憶するファイルのオブジェクト */

       public File file;

  /** 絵を表示するキャンバスのオブジェクト */

       public FileIconCanvas canvas;

  /** ファイル名を表示するラベルのオブジェクト */

       public Label label;

  /** サイズを決める数値 */

       protected static int canvasWidth=64;
       protected static int canvasHeight=64;
       protected static int labelWidth=96;
       protected static int labelHeight=32;

  /** コンストラクタ */

       FileIcon( File file ) {

              super();  // Panelの生成

              this.file = file;
              if( file.exists() ) {

                    setSize( labelWidth, canvasHeight+labelHeight );
                    lay = new BorderLayout();
                    setLayout( lay ); // レイアウトの再設定

                    canvas = new FileIconCanvas( this );
                    canvas.setSize( canvasWidth, canvasHeight );
                    add( "North", canvas );

                    if( file.isFile() ) {
                          label = new Label( file.getName() );
                          add( "South", label );
                    }
                    else if( file.isDirectory() ) {
                          label = new Label( file.getName()
                                             + File.separatorChar );
                          add( "South", label );
                    }
              }
       }

  /** グラフィックスの表示を行うメソッド */

       public void paint( Graphics g ){

              canvas.paint( g );  // キャンバスの描き換え
       }
} // クラス FileIconの定義の終わり

/** アイコンを表示するための FileIconCanvas クラス */

class FileIconCanvas extends Canvas {

  /** 親の FileIcon のオブジェクト */

      FileIcon parent;

  /** コンストラクタ */

      FileIconCanvas( FileIcon parent ) {

              this.parent = parent;
      }

  /** グラフィックスの表示を行うメソッド */

       public void paint( Graphics g ){

              if( parent.file.isFile() ) {

                    g.drawRect( 16, 4, 32, 56 );
              }
              else if( parent.file.isDirectory() ) {

                    g.drawRect( 4, 16, 56, 32 );
              }
       }
} // クラス FileIconCanvas の定義の終わり