ThickButton.java
/** 陰影の深さを指定できるボタンの Bean */
package thickbutton;
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
public class ThickButton extends Canvas
implements Serializable {
/** ボタンの陰影の幅 */
protected static final int defaultBorderWidth=4;
private int borderWidth=defaultBorderWidth;
/** ボタンの陰影の幅の値を取り出すメソッド */
public int getBorderWidth() {
return borderWidth;
}
/** ボタンの陰影の幅の値を設定するメソッド */
public void setBorderWidth( int value ) {
int w = getSize().width;
int h = getSize().height;
int upper;
if( w > h ) upper = w/2 - 1;
else upper = h/2 - 1;
if( value < 1 ) value = 1;
else if( value > upper ) value = upper;
borderWidth = value;
}
/** ボタンのデフォルトのサイズ */
protected final static int defaultWidth=32;
protected final static int defaultHeight=32;
/** コンストラクタ */
public ThickButton() {
super();
setBackground( Color.lightGray );
setSize( defaultWidth, defaultHeight );
InnerAdapter adapter = new InnerAdapter();
addMouseListener( adapter );
}
/** ボタンが押し込まれているかどうかのフラグ */
private boolean pressedFlag=false;
/** ボタンの描画メソッド */
public void paint( Graphics g ) {
int w = getSize().width;
int h = getSize().height;
int b = borderWidth;
int xp1[]={ 0, 0, b-1, b-1, w-b, w-1 };
int yp1[]={ 0, h-1, h-b, b-1, b-1, 0 };
Polygon leftTop = new Polygon( xp1, yp1, 6 );
int xp2[]={ w-1, w-1, w-b, w-b, b-1, 0 };
int yp2[]={ h-1, 0, b-1, h-b, h-b, h-1 };
Polygon rightButtom = new Polygon( xp2, yp2, 6 );
if( pressedFlag ) {
g.setColor( getBackground().brighter() );
g.fillPolygon( rightButtom );
g.setColor( getBackground().darker() );
g.fillPolygon( leftTop );
g.setColor( getBackground() );
g.fillRect( b, b, w-2*b, h-2*b );
}
else {
g.setColor( getBackground().darker() );
g.fillPolygon( rightButtom );
g.setColor( getBackground().brighter() );
g.fillPolygon( leftTop );
g.setColor( getBackground() );
g.fillRect( b, b, w-2*b, h-2*b );
}
}
/** マウスボタンのイベントを処理する innerクラス */
class InnerAdapter extends MouseAdapter {
/** マウスボタンが押し込まれた時の処理のメソッド */
public void mousePressed( MouseEvent evt ) {
pressedFlag = true;
repaint();
}
/** マウスボタンが離された時の処理のメソッド */
public void mouseReleased( MouseEvent evt ) {
pressedFlag = false;
repaint();
}
}
}