PhotoButton.java



/** イメージの写真を指定できるボタンの Bean */

package photobutton;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.Serializable;
import java.net.URL;

public class PhotoButton extends Canvas
                         implements Serializable {

  /** イメージのサイズ */

     protected static final int defaultImageWidth=48;
     private int imageWidth=defaultImageWidth;
     protected static final int defaultImageHeight=64;
     private int imageHeight=defaultImageHeight;

  /** ボタンの写真のイメージ */

     private Image photo;

  /** ボタンの写真のイメージを取り出すメソッド */

     public Image getPhoto() {

          return photo;
     }

  /** ボタンの写真のイメージを設定するメソッド */

     public void setPhoto( Image image ) {

          photo = image;
          repaint();
     }

  /** ボタンのデフォルトのサイズ */

     protected final static int defaultWidth
                               = defaultImageWidth+4;
     protected final static int defaultHeight
                               = defaultImageHeight+4;

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

     public PhotoButton() {

          this( "fuji.gif" );
     }

     public PhotoButton( String fileName ) {

          super();
          setBackground( Color.lightGray );
          setSize( defaultWidth, defaultHeight );

          InnerAdapter adapter = new InnerAdapter( this );
          addMouseListener( adapter );

          try {
                Toolkit tk = Toolkit.getDefaultToolkit();
                URL url = getClass().getResource( fileName );
                photo = tk.createImage( (ImageProducer)url.getContent() );
          }
          catch( Exception e ){
          }
     }

  /** ボタンが押し込まれているかどうかのフラグ */

     private boolean pressedFlag=false;

  /** ボタンの描画メソッド */

     public void paint( Graphics g ) {

          int w = getSize().width;
          int h = getSize().height;
          imageWidth = w - 4;
          imageHeight = h - 4;
          g.setColor( getBackground() );
          g.fill3DRect( 0, 0, w-1, h-1, !pressedFlag );
          g.drawImage( photo, 2, 2, imageWidth, imageHeight, this );
     }

  /** マウスボタンのイベントを処理する innerクラス */

     class InnerAdapter extends MouseAdapter {

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

           PhotoButton parent;

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

           InnerAdapter( PhotoButton parent ) {

                this.parent = parent;
           }

        /** マウスボタンが押し込まれた時の処理のメソッド */

           public void mousePressed( MouseEvent evt ) {

                pressedFlag = true;
                repaint();
           }

        /** マウスボタンが離された時の処理のメソッド */

           public void mouseReleased( MouseEvent evt ) {

                pressedFlag = false;
                repaint();
           }

        /** マウスボタンがクリックされた時の処理のメソッド */

           public void mouseClicked( MouseEvent evt ) {

                processActionEvent( new ActionEvent( parent,
                       ActionEvent.ACTION_PERFORMED, "selected" ) );
           }
     }

   /** アクション処理を呼び出すメソッド */

     protected void processActionEvent( ActionEvent evt ) {

           if( actionListener != null )
               actionListener.actionPerformed( evt );
     }

   /** ActionListenerの登録相手 */

     transient ActionListener actionListener;

   /** ActionListenerの追加(AWTEventMulticasterに登録) */

     public synchronized
            void addActionListener( ActionListener lst ) {

          actionListener
           = AWTEventMulticaster.add( actionListener, lst );
     }

   /** ActionListenerの追加(AWTEventMulticasterに登録) */

     public synchronized
            void removeActionListener( ActionListener lst ) {

          actionListener
           = AWTEventMulticaster.remove( actionListener, lst );
     }
}