package common;import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;public class ImageButton extends JButton{

/**本类为图片按钮,继承自JButton,除了构造方法以外的所有方法都和JButton相同
 */
//序列化版本号
private static final long serialVersionUID = 3490878168295783256L;

private Image mouseOnImage;
private boolean lock;

/**根据三个Image对象以及指定的宽度和高度,创建一个ImageButton对象
 * @param exitedImage 鼠标不在按钮上时,按钮的显示图片
 * @param enteredImage 鼠标在按钮上时,按钮的显示图片
 * @param pressedImage 鼠标按下时,按钮的显示图片
 * @param width,宽度
 * @param height,高度
 */
public ImageButton(Image exitedImage, Image enteredImage, Image pressedImage, int width, int height){
//创建image的缩放版本,并指定给ImageIcon相应的属性
ImageIcon enteredImageIcon = new ImageIcon(enteredImage.getScaledInstance(width, height, Image.SCALE_SMOOTH));
ImageIcon exitedImageIcon = new ImageIcon(exitedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH));
    ImageIcon pressedImageIcon = new ImageIcon(pressedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH));
    mouseOnImage = exitedImageIcon.getImage();
//设置鼠标不在按钮上时的图标
    setIcon(exitedImageIcon);
    //设置鼠标移到按钮上时的图标
    setRolloverIcon(enteredImageIcon);
    //设置鼠标点击时的图标
    setPressedIcon(pressedImageIcon);
    //设置按钮的大小
    Dimension d = new Dimension(width, height);
setPreferredSize(d);
setSize(d);
// 是否显示外围矩形区域 选否
    setContentAreaFilled(false);
    //去掉按钮的聚焦框
    setFocusable(false);
    //去掉边框
    setBorderPainted(false);
    repaint();
}

/**根据三个ImageIcom对象以及指定的宽度和高度,创建一个ImageButton对象
 * @param exitedImageIcon 鼠标不在按钮上时,按钮的显示Icon
 * @param enteredImageIcon 鼠标在按钮上时,按钮的显示Icon
 * @param pressedImageIcon 鼠标按下时,按钮的显示Icon
 * @param width,宽度
 * @param height,高度
 */
public ImageButton(ImageIcon exitedImageIcon, ImageIcon enteredImageIcon,
ImageIcon pressedImageIcon, int width, int height) {
this(exitedImageIcon.getImage(), enteredImageIcon.getImage(), pressedImageIcon.getImage(), width, height);
}

/**根据三个ImageIcom对象以及指定的宽度和高度,创建一个ImageButton对象
 * @param exitedImageIcon 鼠标不在按钮上时,按钮的显示Icon
 * @param enteredImageIcon 鼠标在按钮上时,按钮的显示Icon
 * @param pressedImageIcon 鼠标按下时,按钮的显示Icon
 * @param width,宽度
 * @param height,高度
 * @param text,文本
 */
public ImageButton(String text,ImageIcon exitedImageIcon, ImageIcon enteredImageIcon,
ImageIcon pressedImageIcon, int width, int height) {
this(exitedImageIcon, enteredImageIcon, pressedImageIcon, width, height);
setText(text);
}

/**根据三个图像名以及指定的宽度和高度,创建一个ImageButton对象
 * @param exitedImageName 鼠标不在按钮上时,按钮的显示Icon名
 * @param enteredImageName 鼠标在按钮上时,按钮的显示Icon名
 * @param pressedImageName 鼠标按下时,按钮的显示Icon名
 * @param width,宽度
 * @param height,高度
 */
public ImageButton(String enteredImageName, String exitedImageName, 
           String pressedImageName, int width, int height){
this(new ImageIcon(enteredImageName), new ImageIcon(exitedImageName), new ImageIcon(pressedImageName), width, height);
} public boolean isLock() {
return lock;
} public void setLock(boolean loca) {
this.lock = loca;
} /**判断点是否在按钮内
 * @param x,x坐标
 * @param y,y坐标
 */
 public boolean contains(int x, int y) {
 if (super.contains(x, y)){
 if (!lock){
 //创建存储点(x,y)所在的像素的数组
 int[] pixels = new int[1];
 int alpha = 0;
 try {
 //抓取缓冲区图像的像素到数组
 PixelGrabber pg = new PixelGrabber(mouseOnImage, x, y, 1, 1, pixels, 0, 1);
 pg.grabPixels();
 //获得点(x,y)的alpha值
 ColorModel cm = ColorModel.getRGBdefault();
 alpha = cm.getAlpha(pixels[0]);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 if (alpha != 0){
 if (getModel().isPressed()){
//鼠标在按钮上,并且左键被按下
 mouseOnImage = ((ImageIcon)getPressedIcon()).getImage();
 } else {
//鼠标在按钮上,并且左键没被按下
 mouseOnImage = ((ImageIcon)getRolloverIcon()).getImage();
 }
 return true;
 } else {
 mouseOnImage = ((ImageIcon)getIcon()).getImage();
 return false;
 }
 }
 return true;
 }
 return false;
 }
 
}