用JAVA设计浮动按钮程序
类别: JSP教程
---- 当 今 计 算 机 的 界 面 越 来 越 漂 亮。 随 着IE 和Office 97 的 推 出, 又 出 现 一 个 新 颖 的 控 件: 浮 动 按 纽。 现 在vc 中 已 经 可 以 实 现 这 种 设 计, 而Java 中 目 前 还 没 有 类 似 的 按 纽。 笔 者 最 近 分 析 浮 动 按 纽 的 特 点, 完 成 了 这 个 设 计。
---- 浮 动 按 纽 特 点 分 析:
---- 不 难 发 现, 浮 动 按 纽 其 实 就 是 一 种 欺 骗 视 觉 的 设 计。 当 按 纽 处 于 有 效 状 态 时, 鼠 标 进 入 按 纽 区 域, 按 纽 的 左 侧 和 上 侧 产 生 一 条 白 色 线, 右 侧 和 下 侧 产 生 一 条 灰 色 线( 窗 体 的 背 景 为 明 灰 色), 就 产 生 了 按 纽 浮 起 的 感 觉; 当 按 下 鼠 标 时, 按 纽 对 侧 的 颜 色 交 换, 同 时 按 纽 中 的 图 象 和 文 字 也 向 右 下 移 动 一 个 象 素 点, 则 产 生 按 纽 凹 入 的 感 觉; 移 出 鼠 标, 灰 白 两 色 消 失, 图 象 和 文 字 回 移 一 个 象 素 点, 恢 复 原 状。 根 据 这 些 特 点, 浮 动 按 纽 存 在 五 种 状 态: 平 面、 浮 起、 按 下、 失 效、 消 失。
---- 据 此, 设 计 一 个 完 整 的 浮 动 按 纽 主 要 围 绕 这 五 种 状 态 来 进 行, 由 另 一 个 类 提 供 提 示 信 息; 如 果 按 纽 没 有 提 供 失 效 状 态 下 的 图 象, 则 使 用 平 面 状 态 下 的 图 像, 过 滤 出 一 个 失 效 图 像, 因 此 又 需 要 一 个 类 完 成 图 像 过 滤。
---- 设 计 浮 动 按 纽 要 点 说 明:
---- 浮 动 按 纽 类 和 工 具 提 示 类 均 由java.awt.Canvas 类 派 生, 失 效 图 像 类 由java.awt.image.RGBImageFilter 派 生。
---- 浮 动 按 纽 最 多 需 要 四 个 图 像, 最 少 需 要 一 个 图 像。 图 像 可 以 是 一 个image 或 它 的byte 数 组 形 式。
---- 若 是byte 数 组 形 式, 由java.awt.toolkit 类 下 的createImage 方 法 转 换 为image 形 式。 浮 动 按 纽 消 失 调 用component 类 的setVisible 方 法 完 成, 其 余 四 种 状 态 由paint 方 法 完 成。 设 置 文 本 使 用setText 方 法, 设 置 工 具 提 示 类 实 例 使 用setTooltips 方 法( 不 设 置 这 两 项 亦 可)。 产 生 提 示 调 用 工 具 提 示 类 实 例 的setVisible 方 法。 为 感 应 鼠 标 事 件, 对 应mouseListener 接 口 中 的 方 法, 浮 动 按 纽 中 亦 有 五 个 方 法, 并 返 回 一 个 布 尔 值 确 定 是 否 已 执 行。
---- 提 示 信 息 由 工 具 提 示 类 中 的setVisible-run-repaint-update-paint 过 程 完 成。 调 用 类 实 例 时, 若 线 程 处 于 停 止 或 暂 停 状 态, 将 先 启 动 或 恢 复 再 执 行。 提 示 信 息 达 到 提 示 留 屏 时 间 后, 被 清 除( 在 此 期 间, 若 浮 动 按 纽 产 生 第 二 个 事 件, 提 示 亦 会 消 失)。 之 后 达 到 线 程 空 运 行 时 间, 线 程 将 被 暂 停, 直 到 再 有 浮 动 按 纽 使 用 工 具 提 示。 提 示 留 屏 时 间 和 线 程 空 运 行 时 间 的 参 数 通 过setKeep 和setDelay 方 法 修 改, 单 位 为 秒。 在 一 个 窗 体 中, 一 个 工 具 提 示 类 实 例 由 所 有 浮 动 按 纽 类 实 例 共 用。
---- 为 保 证 程 序 正 确 输 出, 还 应 注 意 以 下 几 点:
---- 1. 为 保 证 工 具 提 示 类 实 例 可 以 覆 盖 在 窗 体 的 所 有 控 件 上, 工 具 提 示 类 实 例 应 第 一 个 使 用add 方 法 添 加 到 窗 体 中。
---- 2. 浮 动 按 纽 必 须 调 用setVisible 方 法 才 能 显 示 或 消 失。
---- 3. 在 添 加 浮 动 按 纽 前, 应 调 用repaint 方 法 刷 新 屏 幕, 否 则 可 能NullPointerException 异 常。
---- 4. 此 按 纽 是 针 对frame 设 计, 若 用 到Applet 中, 请 修 改 浮 动 按 纽 中 的setEntered 方 法 中 的 鼠 标 位 置 计 算。
---- 以 下 是 程 序 代 码:
---- 浮 动 按 纽 类:
import java.awt.*;
import java.awt.image.*;
import java.lang.String;
public class ImageButton extends
Canvas implements java.lang.Runnable {
// h_bosomSpace,v_bosomSpace
变量是图像与字体之间
按左右或上下分布时它们之间的间隔
// h_sideSpace,v_sideSpace
变量是左右两侧和上下两侧的空白区域
// Status变量代表五个按纽
状态变量,初始值为消失
// Alignment变量代表按纽图像
和文本是水平分布还是上下分布
int x,y,width,height,imageX,imageY,fontX,fontY;
private Image NormalImage,
EnterImage,PressImage,DisableImage;
private String text;
private Font font = new Font("Dialog",Font.PLAIN,12);
private Color basicColor = Color.lightGray;
private java.lang.Thread draw;
private boolean imageLoaded;
private int h_bosomSpace=1,v_bosomSpace=1;
private int h_sideSpace=3,v_sideSpace=3;
private int Status = 5;
private final static int Normal=1;
private final static int Enter=2;
private final static int Press=3;
private final static int Disable=4;
private final static int Hide = 5;
private int Alignment;
public final static int HORIZON = 1;
//图象和字体左右分布
public final static int VERTICAL = 2;
//图象和字体上下分布
private Tooltips tooltips; //工具提示例实例
private String explain; //工具提示文本
public ImageButton(byte NormalIcon[]) {
this(NormalIcon,NormalIcon,NormalIcon,
NormalIcon,null,HORIZON);
}
public ImageButton(byte NormalIcon[],
byte EnterIcon[],byte PressIcon[],
byte DisableIcon[],String text,int Alignment) {
if(Alignment!=HORIZON && Alignment!=VERTICAL)
throw new NumberFormatException
("Alignment format error,at the
ImageButton construction");
this.text = text; this.Alignment = Alignment;
if(NormalIcon.length!=0)
this.NormalImage = getToolkit().createImage
(NormalIcon,0,NormalIcon.length);
if(!(new String(EnterIcon).equals
(new String(NormalIcon))))
this.EnterImage = getToolkit().createImage
(EnterIcon,0,EnterIcon.length);
if(!(new String(PressIcon).equals
(new String(NormalIcon))))
this.PressImage = getToolkit().createImage
(PressIcon,0,PressIcon.length);
if(!(new String(DisableIcon).equals
(new String(NormalIcon))))
this.DisableImage = getToolkit().createImage
(DisableIcon,0,DisableIcon.length);
LoadImage(); definePoint();
}
public ImageButton(Image NormalImage) {
this(NormalImage,null,null,null,null,HORIZON);
}
public ImageButton(Image NormalImage,
Image EnterImage,Image PressImage,
Image DisableImage,String text,int Alignment) {
if(Alignment!=HORIZON && Alignment!=VERTICAL)
throw new NumberFormatException
("Alignment format error,at the
ImageButton construction");
this.text = text; this.Alignment = Alignment;
this.NormalImage = NormalImage;
this.EnterImage = EnterImage;
this.PressImage = PressImage;
this.DisableImage = DisableImage;
LoadImage(); definePoint();
}
private void LoadImage() {
java.awt.MediaTracker tracker =
new java.awt.MediaTracker(this);
if(DisableImage==null) {
ImageFilter filter = new DisableFilter();
DisableImage = createImage( new FilteredImageSource(
NormalImage.getSource(),filter));
}
if(EnterImage==null) EnterImage = NormalImage;
if(PressImage==null) PressImage = EnterImage;
tracker.addImage(NormalImage,0);
tracker.addImage(EnterImage,1);
tracker.addImage(PressImage,2);
tracker.addImage(DisableImage,3);
try {
tracker.waitForAll();
if(tracker.isErrorAny()) throw
new InterruptedException
("image Load error,at the LoadImage method");
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
//下面这个方法确定按纽的区域,图象、文本的显示位置
private void definePoint() {
int fontWidth=0,fontHeight=0,imageWidth,imageHeight;
FontMetrics fm = getFontMetrics(font);
if(this.text!=null) {
fontWidth = fm.stringWidth(this.text);
fontHeight = fm.getHeight();
}
imageWidth = NormalImage.getWidth(this);
imageHeight = NormalImage.getHeight(this);
switch(Alignment) {
case HORIZON:
//当按纽中的图象和文字是左右分布时
if(h_sideSpace*2 + imageWidth +
h_bosomSpace + fontWidth >width)
width = h_sideSpace*2 + imageWidth +
h_bosomSpace + fontWidth;
if(imageHeight + v_sideSpace*2 >height)
height = imageHeight + v_sideSpace*2;
if(fontHeight + v_sideSpace*2 >height)
height = fontHeight + v_sideSpace*2;
if(this.text == null) {
imageX = (width - imageWidth)/2;
imageY = (height - imageHeight)/2;
fontX=fontY=0;
} else {
imageX = (width - imageWidth
- h_bosomSpace - fontWidth)/2;
imageY = (height - imageHeight)/2;
fontX = imageX + imageWidth + h_bosomSpace;
fontY = (height - fontHeight)/2+ fm.getAscent();
}
break;
case VERTICAL: //当按纽中的图象和文字是按上下分布时
if(imageWidth + h_sideSpace*2 >width)
width = imageWidth + h_sideSpace*2;
if(fontWidth + h_sideSpace*2 >width)
width = fontWidth + h_sideSpace*2;
if(v_sideSpace*2 + imageHeight +
v_bosomSpace + fontHeight >height)
height = v_sideSpace*2 + imageHeight +
v_bosomSpace + fontHeight;
if(this.text == null) {
imageX = (width - imageWidth)/2;
imageY = (height - imageHeight)/2;
fontX=fontY=0;
} else {
imageX = (width-imageWidth)/2;
imageY = (height-imageHeight-v_
bosomSpace-fontHeight)/2;
fontX = (width-fontWidth)/2;
fontY = imageY+imageHeight+v_
bosomSpace+fm.getAscent();
}
break;
}
}
//启动线程
public void start() {
if(draw == null) {
imageLoaded = false;
draw = new java.lang.Thread
(this,"paint image button");
draw.start();
}
}
//停止线程
public void stop() {
if(draw!=null) {
draw.stop(); draw=null;
}
}
public void run() {
draw.setPriority(Thread.MIN_PRIORITY);
while(!imageLoaded) {
repaint();
try {
Thread.sleep(100);
} catch(InterruptedException ie) {
ie.printStackTrace();
break;
}
}
stop();
}
public void setTooltipsText(String explain) {
this.explain = explain;
}
//获得工具提示实例和文本
public void setTooltips(Tooltips
tooltips,String explain) {
this.tooltips = tooltips;
this.explain = explain;
}
public int getAlignment() {
return Alignment;
}
//定义浮动按纽布局格式
public void setAlignment(int ID) {
if(ID!=HORIZON && ID!=VERTICAL)
throw new NumberFormatException
("Alignment format error, at setAlignment method");
Alignment = ID;
setTransform();
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
setTransform();
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
setTransform();
}
//图像与字体以左右、上下分布时彼此之间的空间
public void setBosomspace(int
h_bosomSpace,int v_bosomSpace) {
if(h_bosomSpace < =0 || v_bosomSpace < =0)
throw new NumberFormatException
("set value error,at setBosomspace method");
this.h_bosomSpace = h_bosomSpace;
this.v_bosomSpace = v_bosomSpace;
setTransform();
}
//设定左右和上下两侧的空白区域
public void setSidespace(int h_side
Space, int v_sideSpace) {
if(h_sideSpace < =0 || v_sideSpace < =0)
throw new NumberFormatException
("set value error,at setSidespace method");
this.h_sideSpace = h_sideSpace;
this.v_sideSpace = v_sideSpace;
setTransform();
}
private void setTransform() {
definePoint();
if(Status!=Hide) {
super.setBounds(x,y,width,height);
repaint();
}
}
public Point getLocation() {
return new Point(x,y);
}
public void setLocation(Point p) {
setLocation(p.x,p.y);
}
public void setLocation(int x,int y) {
setBounds(x,y,this.width,this.height);
}
public Dimension getSize() {
return new Dimension(this.width,this.height);
}
public void setSize(Dimension d) {
setSize(d.width,d.height);
}
public void setSize(int width,int height) {
setBounds(this.x,this.y,width,height);
}
public Rectangle getBounds() {
return new Rectangle(x,y,width,height);
}
public void setBounds(Rectangle r) {
setBounds(r.x,r.y,r.width,r.height);
}
public void setBounds(int x,int
y,int width,int height) {
this.x=x; this.y=y; this.width=
width; this.height=height;
definePoint();
if(Status!=Hide) {
super.setBounds(this.x,this.
y,this.width,this.height);
start();
}
}
public boolean isVisible(){
return Status!=Hide?true:false;
}
public boolean isEnabled() {
return Status!=Disable?true:false;
}
public void setVisible(boolean b) {
if(b) {
Status = Normal;
super.setVisible(true);
super.setBounds(x,y,width,height);
start();
}
else {
Status = Hide;
super.setVisible(false);
}
}
public void setEnabled(boolean b) {
setStatus(b?Normal:Disable);
}
public void setDisable() {
setEnabled(false);
}
public synchronized boolean setClicked() {
if(Status==Disable) return false;
setStatus(Enter);
return true;
}
public synchronized boolean setEntered
(Point mouse,Rectangle frame) {
if(tooltips!=null && explain!=null) {
int toolX=mouse.x-this.x-frame.x*2;
int toolY=mouse.y-this.y-frame.y*2;
tooltips.setVisible(explain,new
Point(toolX,toolY),frame);
}
if(Status==Disable) return false;
setStatus(Enter);
return true;
}
public synchronized boolean setExited() {
if(tooltips!=null && explain!=
null && tooltips.isVisible())
tooltips.setCleared();
if(Status==Disable) return false;
setStatus(Normal);
return true;
}
public synchronized boolean setPressed() {
if(tooltips!=null && explain!=
null && tooltips.isVisible())
tooltips.setCleared();
if(Status==Disable) return false;
setStatus(Press);
return true;
}
public synchronized boolean setReleased(Point mouse) {
if(Status==Disable) return false;
if(mouse.x >=0&&mouse.x < =
width-1&&mouse.y >=0&&mouse.y < =height-1)
setStatus(Enter);
else
setStatus(Normal);
return true;
}
private void setStatus(int ID) {
if(ID < Normal||ID >Hide)
throw new NumberFormatException
("Status format error,at the setStatus method");
if(Status == Hide) return;
//当Status的值为Hide,
不允许重画,必须通过setVisible方法显示
this.Status = ID;
repaint();
}
public void repaint() {
repaint(0,0,width,height);
}
public void update(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,width,height);
g.setColor(getForeground());
paint(g);
}
public void paint(Graphics g ) {
switch(Status) {
case Normal:
imageLoaded=g.drawImage
(NormalImage,imageX,imageY,basicColor,this);
if(text!=null) {
g.setColor(Color.black);
g.setFont(font);
g.drawString(text,fontX,fontY);
}
break;
case Enter:
g.setColor(Color.white); g.drawRect(0,0,width,height);
g.setColor(Color.gray);
g.drawLine(0,height-1,width-1,height-1);
g.drawLine(width-1,0,width-1,height-1);
g.drawImage(EnterImage,imageX,imageY,basicColor,this);
if(text!=null) {
g.setColor(Color.blue); g.setFont(font);
g.drawString(text,fontX,fontY);
}
break;
case Press:
g.setColor(Color.gray); g.drawRect(0,0,width,height);
g.setColor(Color.white);
g.drawLine(0,height-1,width-1,height-1);
g.drawLine(width-1,0,width-1,height-1);
g. drawImage(PressImage,imageX+1,
imageY+1,basicColor,this);
if(text!=null) {
g.setColor(Color.red); g.setFont(font);
g.drawString(text,fontX+1,fontY+1);
}
break;
case Disable:
g. drawImage(DisableImage,imageX,
imageY,basicColor,this);
if(text!=null) {
g.setColor(Color.gray); g.setFont(font);
g.drawString(text,fontX,fontY);
}
break;
}
}
}
工具提示类
import java.awt.*;
import java.lang.String;
import java.lang.Thread;
public class Tooltips extends Canvas
implements java.lang.Runnable {
/* keep是工具提示留屏时间变量
delay是线程空运行时间变量,
到期线程将被暂停
h_sideSpace和v_sideSpace
是左右两侧和上下两侧的空白区域
Status变量记录线程所处的五种状态,初始值为消失
*/
private Font font;
private int x,y,width,height,fontX,fontY;
private int h_sideSpace=3,
v_sideSpace=3,keep=3,delay=10;
private java.lang.Thread draw;
private String text;
private int Status = 4;
private final static int Draw = 1;
//显示工具提示
private final static int stopDraw = 2;
//停止工具提示并清除
private final static int Clear = 3;
//清除工具提示
private final static int Hide = 4;
//隐藏工具提示
private final static int Suspend = 5;
//暂停线程
public Tooltips() {
this(new Font("Dialog",Font.PLAIN,12));
}
public Tooltips(Font font) {
this.font = font;
}
//提示显示,如果窗体区域不能满足要求,就不会显示
public synchronized void setVisible
(String text,Point mouse,Rectangle frame) {
if(text==null) return;
this.text = text;
FontMetrics fm = getFontMetrics(font);
int fontWidth = fm.stringWidth(this.text);
int fontHeight = fm.getHeight();
width = fontWidth + h_sideSpace*2;
height = fontHeight + v_sideSpace*2;
fontX = (width-fontWidth)/2;
fontY = (height - fontHeight)/2+fm.getAscent();
if(mouse.y-height-20 < 0&&mouse.y
+height+20 >=frame.height) return;
this.x=(mouse.x+width >=
frame.width?(frame.width-width-5 >0?
frame.width-width-5:0):mouse.x);
this.y=(mouse.y+20+height < =
frame.height?mouse.y+20:mouse.y-20-height);
if(draw==null) start();
else if(Status == Suspend) draw.resume();
Status = Draw;
}
public synchronized boolean isVisible() {
return (Status==Draw||Status==stopDraw)?true:false;
}
//清除提示文本
public synchronized void setCleared() {
if(Status!=Draw&&Status!=stopDraw) return;
Status = Clear;
}
public void setDelay(int delay) {
if(delay < 3) //空载时间最少3秒钟
throw new NumberFormatException
("delay value error,at the setDelay method");
this.delay = delay;
}
public void setKeep(int keep) {
if(keep < 1) //留屏时间最少1秒钟
throw new NumberFormatException
("keep value error,at the setKeep method");
this.keep = keep;
}
public void start() {
if(draw == null) {
draw = new java.lang.Thread(this,"draw text");
draw.start();
}
}
public void stop() {
if(draw!=null) {
draw.stop(); draw = null;
}
}
public void run() {
draw.setPriority(Thread.NORM_PRIORITY);
long beginTime=new java.util.Date().getTime();
while(true) {
switch(Status) {
case Draw:
super.setBounds(x,y,width,height);
repaint();
beginTime = new java.util.Date().getTime();
break;
case Clear:
super.setBounds(x,y,0,0);
Status = Hide;
beginTime = new java.util.Date().getTime();
break;
}
try {
Thread.sleep(500);
} catch(InterruptedException ie) {
ie.printStackTrace();
stop();
break;
}
if(Status==stopDraw && (new java.util.Date()
.getTime()-beginTime)/1000 >=keep)
Status = Clear;
else if(Status==Hide&&(new java.util.Date()
.getTime()-beginTime)/1000 >=delay) {
Status = Suspend; draw.suspend();
}
}
}
public void repaint() {
repaint(0,0,width,height);
}
public void update(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,width,height);
g.setColor(getForeground());
paint(g);
}
public void paint(Graphics g) {
if(Status==Draw||Status==stopDraw) {
synchronized(this) {
g.setColor(Color.yellow);
g.fillRect(0,0,width,height);
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
g.setColor(Color.red); g.setFont(font);
g.drawString(text,fontX,fontY);
Status = stopDraw;
}
}
}
}
失效图象过滤类
public class DisableFilter extends
java.awt.image.RGBImageFilter {
private int red,green,blue,color;
public int filterRGB(int x,int y,int rgb) {
red = (rgb > >16)&0xff;
green = (rgb > >8)&0xff;
blue = (rgb > >0)&0xff;
if(isRange(red,192,5)&&isRange
(green,192,5)&&isRange(blue,192,5))
color = 192;
else if(isRange(red,255,5)&&isRange
(green,255,5)&&isRange(blue,255,5))
color = 192;
else if(isRange(red,0,5)&&isRange
(green,0,5)&&isRange(blue,0,5))
color = 255;
else
color = 128;
return (rgb&0xff000000|
(color < < 16)|(color < < 8)|color);
}
public boolean isRange(int
color,int base,int margin) {
return color< =base+margin&&color
>=base-margin?true:false;
}
}
---- 本 程 序 在Windows95 下, 用Borland JBuiler2+JDK1.1.6 调 试 通 过
---- 以 下 另 附 两 个 类, 其 中 一 个 是image 的byte 数 组 类, 另 一 类 是frame 的 派 生 类, 把 这 五 个 类 组 合 一 起, 浮 动 按 纽 即 可 执 行。
import java.awt.*;
import java.awt.event.*;
public class Multi extends Frame
implements ActionListener,MouseListener{
Button butExit = new Button();
ImageButton cmdExit = new ImageButton
(ImageCode.mbox,ImageCode.bomb,
ImageCode.sound1,ImageCode.mbox,
" 千 古 江 山",ImageButton.HORIZON);
ImageButton cmdStop =
new ImageButton(ImageCode.sound2);
ImageButton cmdInfor =
new ImageButton(ImageCode.cowSmall);
Tooltips tooltips =
new Tooltips(new Font("Dialog",Font.PLAIN,14));
java.lang.Thread refresh;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == butExit) {
dispose();
System.exit(0);
}
}
public void mouseClicked(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
if(me.getSource()==cmdExit && cmdExit.setClicked()) {
cmdStop.setVisible(!cmdStop.isVisible());
}
else if(me.getSource()==cmdStop && cmdStop.setClicked()) {
cmdExit.setEnabled(!cmdExit.isEnabled());
}
else if(me.getSource()==cmdInfor
&& cmdInfor.setClicked());
}
public void mouseExited(java.
awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
if(me.getSource()==cmdExit
&& cmdExit.setExited());
else if(me.getSource()==cmdStop
&& cmdStop.setExited());
else if(me.getSource()==cmdInfor
&& cmdInfor.setExited());
}
public void mouseEntered
(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
Point mouse = me.getPoint();
Rectangle frame = this.getBounds();
if(me.getSource() == cmdExit
&& cmdExit.setEntered(mouse,frame));
else if(me.getSource()==cmdStop
&& cmdStop.setEntered(mouse,frame));
else if(me.getSource()==cmdInfor
&& cmdInfor.setEntered(mouse,frame))
cmdStop.setAlignment(cmdStop.getAlignment()
==ImageButton.VERTICAL?
ImageButton.HORIZON : ImageButton.VERTICAL);
}
public void mousePressed(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
if(me.getSource()==cmdExit
&& cmdExit.setPressed());
else if(me.getSource()==cmdStop
&& cmdStop.setPressed());
else if(me.getSource()==cmdInfor
&& cmdInfor.setPressed());
}
public void mouseReleased(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
Point mouse = me.getPoint();
if(me.getSource()==cmdExit
&& cmdExit.setReleased(mouse));
else if(me.getSource()==cmdStop
&& cmdStop.setReleased(mouse));
else if(me.getSource()==cmdInfor
&& cmdInfor.setReleased(mouse));
}
public Multi() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Multi();
}
private void jbInit() throws NullPointerException {
Dimension d = getToolkit().getScreenSize();
this.setSize(new Dimension(536, 307));
this.setLocation((d.width-getSize().
width)/2,(d.height-getSize().height)/2);
this.setLayout(null);
this.setTitle(System.getProperty("java.version"));
this.setBackground(Color.lightGray);
this.validate(); this.setVisible(true);
this.add(tooltips,null);
butExit.setFont(new Font("Dialog", 0, 16));
butExit.setBounds(new Rectangle(352, 136, 170, 36));
butExit.setLabel("Exit Frame");
butExit.addActionListener(this);
this.add(butExit, null);
repaint(0,0,this.getSize().width,this.getSize().height);
cmdExit.setFont(new Font("Dialog",Font.PLAIN,14));
cmdExit.setLocation(new Point(10,35));
cmdExit.setTooltips(tooltips,
"君不见黄河之水天上来,奔流到海不复回");
cmdExit.setVisible(true);
cmdExit.addMouseListener(this);
this.add(cmdExit,null);
cmdStop.setText("大风起兮");
cmdStop.setFont(new Font("Dialog",Font.PLAIN,14));
cmdStop.setLocation(110,35);
cmdStop.setSize(cmdExit.getSize().
width,cmdExit.getSize().height);
cmdStop.setTooltips(tooltips,
"看茫茫大江来天畔,风烟几万年");
cmdStop.setVisible(true);
cmdStop.addMouseListener(this);
this.add(cmdStop,null);
cmdInfor.setText("对潇潇暮雨洒江天");
cmdInfor.setFont(new Font("Dialog",Font.PLAIN,14));
cmdInfor.setLocation(210,260);
cmdInfor.setSize(cmdInfor.getSize().
width+10,cmdExit.getSize().height);
cmdInfor.setTooltips(tooltips,
"渐霜风凄紧,关河冷落,残照当楼");
cmdInfor.setVisible(true);
cmdInfor.addMouseListener(this);
this.add(cmdInfor,null);
}
}
public class ImageCode {
public static byte mbox[]={
71,73,70,56,57,97,32,0,32,0,-94,0,0,-1,-1,-1,
-49,-49,-49,21,56,-2,0,51,-1,0,0,-1,0,0,-52,
0,0,-103,0,0,0,33,-7,4,1,0,0,1,0,44,0,0,0,0,
32,0,32,0,0,3,-59,24,-70,123,-2,44,-54,-87,
-100,24,120,8,72,123,-67,-104,-109,105,-114,
55,29,-96,99,20,98,-74,-103,12,26,22,-123,97,
15,45,121,-64,-63,49,-45,-76,71,-24,-63,-95,
-56,124,-124,36,43,-109,99,-18,-116,-95,12,-63,
-121,-93,-114,92,79,-119,85,68,-75,94,71,47,-19,
-107,-5,45,-109,-60,101,-81,-103,-119,30,-81,-53,
-31,70,-6,-99,-114,-88,-93,-12,-79,125,-18,36,10,
-39,49,124,56,0,-124,-123,-123,91,123,95,91,-122,
-116,-120,-127,-118,76,0,14,-110,7,-121,-128,114,
-112,33,-110,-108,-100,-114,-104,110,-102,-116,
-106,33,-119,-96,-125,68,-99,-105,
21,-126,-107,-83,-101,-124,-98,
-85,-103,-125,-94,-80,-86,61,-126,85,126,77,89,31,
22,76,121,122,118,59,50,56,-63,35,-67,13,31,-58,-57,
-92,30,62,119,116,-55,98,93,-69,-69,-50,29,40,93,
-49,-37,38,45,-45,-119,-32,90,
69,70,28,9,0,59
};
public static byte bomb[]={
71,73,70,56,57,97,20,0,22,0,-29,0,0,-1,-1,-1,-1,-52,
51,-52,-1,-1,-52,-52,-52,-69,-69,-69,-103,-103,-103,
-120,-120,-120,102,102,102,85,85,85,51,51,51,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,33,-2,78,84,104,105,115,32,
97,114,116,32,105,115,32,105,
110,32,116,104,101,32,112,117,98,108,105,99,32,
100,111,109,97,105,110,46,32,75,101,
118,105,110,32,72,117,103,104,101,115,44,32,107,
101,118,105,110,104,64,101,105,116,46,99,111,109,
44,32,83,101,112,116,101,109,98,101,114,32,49,57,
57,53,0,33,-7,4,1,0,0,2,
0,44,0,0,0,0,20,0,22,0,0,4,-113,48,-55,73,-89,-72,88,
36,-64,-69,-33,73,118,109,94,73,-122,-39,22,0,10,
-80,118,36,-128,-114,-35,-5,125,-40,
-90,4,73,-48,118,-118,68,112,
72,-29,4,10,37,-123,114,-87,-96,-83,20,5,91,-89,64,
45,52,53,31,-31,111,74,-67,-58,0,8,-88,86,49,-104,
122,61,98,4,-11,64,-26,88,-117,28,-91,
26,96,56,-124,-53,111,44,-70,64,
-96,-37,-123,0,121,95,44,85,127,-128,-126,73,7,
-118,8,18,74,-127,103,104,-117,-115,-114,-120,30,
3,65,-115,66,120,-112,-106,67,74,101,-113,112,-99,
-97,102,-94,37,-89,-126,9,85,-85,-84,84,66,
25,76,-79,-78,87,17,0,59
};
public static byte sound1[]={
71,73,70,56,57,97,20,0,22,0,-62,0,0,-1,-1,-1,-1,51,51,
-52,-1,-1,-52,-52,-52,-103,-103,-103,
102,0,0,51,51,51,0,0,0,33,-2,78,84,104,105,
115,32,97,114,116,32,105,
115,32,105,110,32,116,104,101,32,112,117,98,108,
105,99,32,100,111,109,97,105,110,46,32,75,101,118,
105,110,32,72,117,103,104,101,115,44,32,107,101,
118,105,110,104,64,101,105,116,46,99,
111,109,44,32,83,101,112,116,101,109,98,101,
114,32,49,57,57,53,0,33,-7,4,1,0,0,2,0,44,0,0,0,0,
20,0,22,0,0,3,107,40,-70,-36,-2,48,54,-61,66,-112,-22,28,
97,85,-113,7,-79,113,23,9,-123,-64,104,126,13,
-102,114,-98,119,105,52,1,-36,91,-41,21,65,72,
-4,-74,-37,107,119,-31,-71,-124,-62,13,79,-80,52,
6,-111,56,102,113,122,-124,42,-89,-46,42,
-14,42,-51,18,12,-33,111,-46,-44,-100,-47,14,3,
67,84,-121,21,24,-34,6,-76,58,23,51,77,-28,42,54,
36,62,24,125,74,123,26,118,44,123,20,
11,-124,24,-119,-118,11,9,0,59
};
public static byte sound2[]={
71,73,70,56,57,97,20,0,22,0,-62,0,0,-1,-1,-1,
-52,-1,-1,-52,-52,-52,-103,-103,-103,102,102,
102,51,51,51,0,0,0,0,0,0,33,-2,78,84,104,105,
115,32,97,114,116,32,105,
115,32,105,110,32,116,104,101,32,112,117,98,
108,105,99,32,100,111,109,97,105,110,46,32,75,
101,118,105,110,32,72,117,103,104,101,115,44,32,
107,101,118,105,110,104,64,101,105,116,46,99,
111,109,44,32,83,101,112,116,101,109,98,
101,114,32,49,57,57,53,0,33,-7,4,1,0,0,1,0,44,0,0,
0,0,20,0,22,0,0,3,80,24,-70,-36,-2,-50,72,-8,-92,45,116,89,
67,36,-90,27,33,122,80,40,10,87,-55,-99,-101,-15,
69,28,122,-111,85,91,-36,52,-20,-35,74,-34,-20,-81,
-128,-113,49,20,-70,84,47,27,-78,103,0,12,-118,
-52,79,-13,57,84,26,55,65,-119,
51,-121,-51,110,125,55,30,-47,-102,97,-18,
-54,-116,112,16,-51,94,36,0,0,59
};
public static byte cowSmall[]={
71,73,70,56,57,97,27,0,19,0,-12,0,0,-1,-1,-1,-1,-4,
-4,-1,-5,-5,-1,-8,-8,-1,-9,-9,-1,-14,-14,-1,-19,-19,-1,
-25,-25,-1,-28,-28,-1,-37,-37,-1,-42,-42,-1,-51,-51,-64,
-64,-64,-44,-44,-44,-65,-65,-65,-86,-86,-86,127,127,
127,85,85,85,63,63,63,42,42,42,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,33,-7,4,1,0,0,12,0,44,0,0,0,0,27,0,19,
0,0,5,-37,32,35,-114,100,105,-98,100,-12,-96,-24,58,
62,17,20,21,-113,12,65,78,3,-23,
81,36,57,-127,32,-94,39,122,72,38,18,-60,-124,-62,
100,20,100,-58,-55,114,-46,-88,-11,-112,35,8,-59,-79,
-12,53,2,19,-56,-108,73,-127,28,39,17,-20,-120,-62,96,
-90,113,18,-78,60,66,89,
82,-120,34,-57,-115,25,70,-58,-53,123,20,127,
126,48,47,18,18,116,20,1,119,118,18,5,1,13,14,12,
104,17,14,70,36,53,12,113,13,116,90,77,1,6,6,-113,
65,122,44,104,13,20,
15,117,59,5,34,-94,1,12,8,15,8,40,102,-100,86,5,5,57,
16,65,85,58,13,44,16,-94,97,-66,34,8,13,-78,
-64,59,44,15,95,-121,53,43,85,78,59,18,16,46,40,14,6,53,
14,-36,15,-36,12,6,55,-37,44,12,-110,15,-41,47,36,56,
-28,12,43,-24,69,-25,34,42,-28,-15,39,-25,-110,-19,
-20,-42,49,-17,-36,-8,-106,-28,-52,93,115,
-48,-61,-38,-117,7,33,0,0,59
};
public static byte dot[]={
71,73,70,56,57,97,13,0,13,0,-94,-1,0,-1,0,0,
-64,-64,-64,-52,0,0,-103,0,0,-128,0,0,102,0,0,0,0,0,
0,0,0,33,-7,4,1,0,0,1,0,44,0,0,0,0,
13,0,13,0,64,3,42,24,-70,-78,91,-112,12,33,-128,5,
10,22,73,47,118,12,40,6,-43,-105,69,83,-23,-119,-27,
-56,-104,-25,-106,94,15,-38,-83,26,-89,90,
65,62,-13,-82,10,40,1,0,59
};
}
---- 浮 动 按 纽 特 点 分 析:
---- 不 难 发 现, 浮 动 按 纽 其 实 就 是 一 种 欺 骗 视 觉 的 设 计。 当 按 纽 处 于 有 效 状 态 时, 鼠 标 进 入 按 纽 区 域, 按 纽 的 左 侧 和 上 侧 产 生 一 条 白 色 线, 右 侧 和 下 侧 产 生 一 条 灰 色 线( 窗 体 的 背 景 为 明 灰 色), 就 产 生 了 按 纽 浮 起 的 感 觉; 当 按 下 鼠 标 时, 按 纽 对 侧 的 颜 色 交 换, 同 时 按 纽 中 的 图 象 和 文 字 也 向 右 下 移 动 一 个 象 素 点, 则 产 生 按 纽 凹 入 的 感 觉; 移 出 鼠 标, 灰 白 两 色 消 失, 图 象 和 文 字 回 移 一 个 象 素 点, 恢 复 原 状。 根 据 这 些 特 点, 浮 动 按 纽 存 在 五 种 状 态: 平 面、 浮 起、 按 下、 失 效、 消 失。
---- 据 此, 设 计 一 个 完 整 的 浮 动 按 纽 主 要 围 绕 这 五 种 状 态 来 进 行, 由 另 一 个 类 提 供 提 示 信 息; 如 果 按 纽 没 有 提 供 失 效 状 态 下 的 图 象, 则 使 用 平 面 状 态 下 的 图 像, 过 滤 出 一 个 失 效 图 像, 因 此 又 需 要 一 个 类 完 成 图 像 过 滤。
---- 设 计 浮 动 按 纽 要 点 说 明:
---- 浮 动 按 纽 类 和 工 具 提 示 类 均 由java.awt.Canvas 类 派 生, 失 效 图 像 类 由java.awt.image.RGBImageFilter 派 生。
---- 浮 动 按 纽 最 多 需 要 四 个 图 像, 最 少 需 要 一 个 图 像。 图 像 可 以 是 一 个image 或 它 的byte 数 组 形 式。
---- 若 是byte 数 组 形 式, 由java.awt.toolkit 类 下 的createImage 方 法 转 换 为image 形 式。 浮 动 按 纽 消 失 调 用component 类 的setVisible 方 法 完 成, 其 余 四 种 状 态 由paint 方 法 完 成。 设 置 文 本 使 用setText 方 法, 设 置 工 具 提 示 类 实 例 使 用setTooltips 方 法( 不 设 置 这 两 项 亦 可)。 产 生 提 示 调 用 工 具 提 示 类 实 例 的setVisible 方 法。 为 感 应 鼠 标 事 件, 对 应mouseListener 接 口 中 的 方 法, 浮 动 按 纽 中 亦 有 五 个 方 法, 并 返 回 一 个 布 尔 值 确 定 是 否 已 执 行。
---- 提 示 信 息 由 工 具 提 示 类 中 的setVisible-run-repaint-update-paint 过 程 完 成。 调 用 类 实 例 时, 若 线 程 处 于 停 止 或 暂 停 状 态, 将 先 启 动 或 恢 复 再 执 行。 提 示 信 息 达 到 提 示 留 屏 时 间 后, 被 清 除( 在 此 期 间, 若 浮 动 按 纽 产 生 第 二 个 事 件, 提 示 亦 会 消 失)。 之 后 达 到 线 程 空 运 行 时 间, 线 程 将 被 暂 停, 直 到 再 有 浮 动 按 纽 使 用 工 具 提 示。 提 示 留 屏 时 间 和 线 程 空 运 行 时 间 的 参 数 通 过setKeep 和setDelay 方 法 修 改, 单 位 为 秒。 在 一 个 窗 体 中, 一 个 工 具 提 示 类 实 例 由 所 有 浮 动 按 纽 类 实 例 共 用。
---- 为 保 证 程 序 正 确 输 出, 还 应 注 意 以 下 几 点:
---- 1. 为 保 证 工 具 提 示 类 实 例 可 以 覆 盖 在 窗 体 的 所 有 控 件 上, 工 具 提 示 类 实 例 应 第 一 个 使 用add 方 法 添 加 到 窗 体 中。
---- 2. 浮 动 按 纽 必 须 调 用setVisible 方 法 才 能 显 示 或 消 失。
---- 3. 在 添 加 浮 动 按 纽 前, 应 调 用repaint 方 法 刷 新 屏 幕, 否 则 可 能NullPointerException 异 常。
---- 4. 此 按 纽 是 针 对frame 设 计, 若 用 到Applet 中, 请 修 改 浮 动 按 纽 中 的setEntered 方 法 中 的 鼠 标 位 置 计 算。
---- 以 下 是 程 序 代 码:
---- 浮 动 按 纽 类:
import java.awt.*;
import java.awt.image.*;
import java.lang.String;
public class ImageButton extends
Canvas implements java.lang.Runnable {
// h_bosomSpace,v_bosomSpace
变量是图像与字体之间
按左右或上下分布时它们之间的间隔
// h_sideSpace,v_sideSpace
变量是左右两侧和上下两侧的空白区域
// Status变量代表五个按纽
状态变量,初始值为消失
// Alignment变量代表按纽图像
和文本是水平分布还是上下分布
int x,y,width,height,imageX,imageY,fontX,fontY;
private Image NormalImage,
EnterImage,PressImage,DisableImage;
private String text;
private Font font = new Font("Dialog",Font.PLAIN,12);
private Color basicColor = Color.lightGray;
private java.lang.Thread draw;
private boolean imageLoaded;
private int h_bosomSpace=1,v_bosomSpace=1;
private int h_sideSpace=3,v_sideSpace=3;
private int Status = 5;
private final static int Normal=1;
private final static int Enter=2;
private final static int Press=3;
private final static int Disable=4;
private final static int Hide = 5;
private int Alignment;
public final static int HORIZON = 1;
//图象和字体左右分布
public final static int VERTICAL = 2;
//图象和字体上下分布
private Tooltips tooltips; //工具提示例实例
private String explain; //工具提示文本
public ImageButton(byte NormalIcon[]) {
this(NormalIcon,NormalIcon,NormalIcon,
NormalIcon,null,HORIZON);
}
public ImageButton(byte NormalIcon[],
byte EnterIcon[],byte PressIcon[],
byte DisableIcon[],String text,int Alignment) {
if(Alignment!=HORIZON && Alignment!=VERTICAL)
throw new NumberFormatException
("Alignment format error,at the
ImageButton construction");
this.text = text; this.Alignment = Alignment;
if(NormalIcon.length!=0)
this.NormalImage = getToolkit().createImage
(NormalIcon,0,NormalIcon.length);
if(!(new String(EnterIcon).equals
(new String(NormalIcon))))
this.EnterImage = getToolkit().createImage
(EnterIcon,0,EnterIcon.length);
if(!(new String(PressIcon).equals
(new String(NormalIcon))))
this.PressImage = getToolkit().createImage
(PressIcon,0,PressIcon.length);
if(!(new String(DisableIcon).equals
(new String(NormalIcon))))
this.DisableImage = getToolkit().createImage
(DisableIcon,0,DisableIcon.length);
LoadImage(); definePoint();
}
public ImageButton(Image NormalImage) {
this(NormalImage,null,null,null,null,HORIZON);
}
public ImageButton(Image NormalImage,
Image EnterImage,Image PressImage,
Image DisableImage,String text,int Alignment) {
if(Alignment!=HORIZON && Alignment!=VERTICAL)
throw new NumberFormatException
("Alignment format error,at the
ImageButton construction");
this.text = text; this.Alignment = Alignment;
this.NormalImage = NormalImage;
this.EnterImage = EnterImage;
this.PressImage = PressImage;
this.DisableImage = DisableImage;
LoadImage(); definePoint();
}
private void LoadImage() {
java.awt.MediaTracker tracker =
new java.awt.MediaTracker(this);
if(DisableImage==null) {
ImageFilter filter = new DisableFilter();
DisableImage = createImage( new FilteredImageSource(
NormalImage.getSource(),filter));
}
if(EnterImage==null) EnterImage = NormalImage;
if(PressImage==null) PressImage = EnterImage;
tracker.addImage(NormalImage,0);
tracker.addImage(EnterImage,1);
tracker.addImage(PressImage,2);
tracker.addImage(DisableImage,3);
try {
tracker.waitForAll();
if(tracker.isErrorAny()) throw
new InterruptedException
("image Load error,at the LoadImage method");
} catch(InterruptedException ie) {
ie.printStackTrace();
}
}
//下面这个方法确定按纽的区域,图象、文本的显示位置
private void definePoint() {
int fontWidth=0,fontHeight=0,imageWidth,imageHeight;
FontMetrics fm = getFontMetrics(font);
if(this.text!=null) {
fontWidth = fm.stringWidth(this.text);
fontHeight = fm.getHeight();
}
imageWidth = NormalImage.getWidth(this);
imageHeight = NormalImage.getHeight(this);
switch(Alignment) {
case HORIZON:
//当按纽中的图象和文字是左右分布时
if(h_sideSpace*2 + imageWidth +
h_bosomSpace + fontWidth >width)
width = h_sideSpace*2 + imageWidth +
h_bosomSpace + fontWidth;
if(imageHeight + v_sideSpace*2 >height)
height = imageHeight + v_sideSpace*2;
if(fontHeight + v_sideSpace*2 >height)
height = fontHeight + v_sideSpace*2;
if(this.text == null) {
imageX = (width - imageWidth)/2;
imageY = (height - imageHeight)/2;
fontX=fontY=0;
} else {
imageX = (width - imageWidth
- h_bosomSpace - fontWidth)/2;
imageY = (height - imageHeight)/2;
fontX = imageX + imageWidth + h_bosomSpace;
fontY = (height - fontHeight)/2+ fm.getAscent();
}
break;
case VERTICAL: //当按纽中的图象和文字是按上下分布时
if(imageWidth + h_sideSpace*2 >width)
width = imageWidth + h_sideSpace*2;
if(fontWidth + h_sideSpace*2 >width)
width = fontWidth + h_sideSpace*2;
if(v_sideSpace*2 + imageHeight +
v_bosomSpace + fontHeight >height)
height = v_sideSpace*2 + imageHeight +
v_bosomSpace + fontHeight;
if(this.text == null) {
imageX = (width - imageWidth)/2;
imageY = (height - imageHeight)/2;
fontX=fontY=0;
} else {
imageX = (width-imageWidth)/2;
imageY = (height-imageHeight-v_
bosomSpace-fontHeight)/2;
fontX = (width-fontWidth)/2;
fontY = imageY+imageHeight+v_
bosomSpace+fm.getAscent();
}
break;
}
}
//启动线程
public void start() {
if(draw == null) {
imageLoaded = false;
draw = new java.lang.Thread
(this,"paint image button");
draw.start();
}
}
//停止线程
public void stop() {
if(draw!=null) {
draw.stop(); draw=null;
}
}
public void run() {
draw.setPriority(Thread.MIN_PRIORITY);
while(!imageLoaded) {
repaint();
try {
Thread.sleep(100);
} catch(InterruptedException ie) {
ie.printStackTrace();
break;
}
}
stop();
}
public void setTooltipsText(String explain) {
this.explain = explain;
}
//获得工具提示实例和文本
public void setTooltips(Tooltips
tooltips,String explain) {
this.tooltips = tooltips;
this.explain = explain;
}
public int getAlignment() {
return Alignment;
}
//定义浮动按纽布局格式
public void setAlignment(int ID) {
if(ID!=HORIZON && ID!=VERTICAL)
throw new NumberFormatException
("Alignment format error, at setAlignment method");
Alignment = ID;
setTransform();
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
setTransform();
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
setTransform();
}
//图像与字体以左右、上下分布时彼此之间的空间
public void setBosomspace(int
h_bosomSpace,int v_bosomSpace) {
if(h_bosomSpace < =0 || v_bosomSpace < =0)
throw new NumberFormatException
("set value error,at setBosomspace method");
this.h_bosomSpace = h_bosomSpace;
this.v_bosomSpace = v_bosomSpace;
setTransform();
}
//设定左右和上下两侧的空白区域
public void setSidespace(int h_side
Space, int v_sideSpace) {
if(h_sideSpace < =0 || v_sideSpace < =0)
throw new NumberFormatException
("set value error,at setSidespace method");
this.h_sideSpace = h_sideSpace;
this.v_sideSpace = v_sideSpace;
setTransform();
}
private void setTransform() {
definePoint();
if(Status!=Hide) {
super.setBounds(x,y,width,height);
repaint();
}
}
public Point getLocation() {
return new Point(x,y);
}
public void setLocation(Point p) {
setLocation(p.x,p.y);
}
public void setLocation(int x,int y) {
setBounds(x,y,this.width,this.height);
}
public Dimension getSize() {
return new Dimension(this.width,this.height);
}
public void setSize(Dimension d) {
setSize(d.width,d.height);
}
public void setSize(int width,int height) {
setBounds(this.x,this.y,width,height);
}
public Rectangle getBounds() {
return new Rectangle(x,y,width,height);
}
public void setBounds(Rectangle r) {
setBounds(r.x,r.y,r.width,r.height);
}
public void setBounds(int x,int
y,int width,int height) {
this.x=x; this.y=y; this.width=
width; this.height=height;
definePoint();
if(Status!=Hide) {
super.setBounds(this.x,this.
y,this.width,this.height);
start();
}
}
public boolean isVisible(){
return Status!=Hide?true:false;
}
public boolean isEnabled() {
return Status!=Disable?true:false;
}
public void setVisible(boolean b) {
if(b) {
Status = Normal;
super.setVisible(true);
super.setBounds(x,y,width,height);
start();
}
else {
Status = Hide;
super.setVisible(false);
}
}
public void setEnabled(boolean b) {
setStatus(b?Normal:Disable);
}
public void setDisable() {
setEnabled(false);
}
public synchronized boolean setClicked() {
if(Status==Disable) return false;
setStatus(Enter);
return true;
}
public synchronized boolean setEntered
(Point mouse,Rectangle frame) {
if(tooltips!=null && explain!=null) {
int toolX=mouse.x-this.x-frame.x*2;
int toolY=mouse.y-this.y-frame.y*2;
tooltips.setVisible(explain,new
Point(toolX,toolY),frame);
}
if(Status==Disable) return false;
setStatus(Enter);
return true;
}
public synchronized boolean setExited() {
if(tooltips!=null && explain!=
null && tooltips.isVisible())
tooltips.setCleared();
if(Status==Disable) return false;
setStatus(Normal);
return true;
}
public synchronized boolean setPressed() {
if(tooltips!=null && explain!=
null && tooltips.isVisible())
tooltips.setCleared();
if(Status==Disable) return false;
setStatus(Press);
return true;
}
public synchronized boolean setReleased(Point mouse) {
if(Status==Disable) return false;
if(mouse.x >=0&&mouse.x < =
width-1&&mouse.y >=0&&mouse.y < =height-1)
setStatus(Enter);
else
setStatus(Normal);
return true;
}
private void setStatus(int ID) {
if(ID < Normal||ID >Hide)
throw new NumberFormatException
("Status format error,at the setStatus method");
if(Status == Hide) return;
//当Status的值为Hide,
不允许重画,必须通过setVisible方法显示
this.Status = ID;
repaint();
}
public void repaint() {
repaint(0,0,width,height);
}
public void update(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,width,height);
g.setColor(getForeground());
paint(g);
}
public void paint(Graphics g ) {
switch(Status) {
case Normal:
imageLoaded=g.drawImage
(NormalImage,imageX,imageY,basicColor,this);
if(text!=null) {
g.setColor(Color.black);
g.setFont(font);
g.drawString(text,fontX,fontY);
}
break;
case Enter:
g.setColor(Color.white); g.drawRect(0,0,width,height);
g.setColor(Color.gray);
g.drawLine(0,height-1,width-1,height-1);
g.drawLine(width-1,0,width-1,height-1);
g.drawImage(EnterImage,imageX,imageY,basicColor,this);
if(text!=null) {
g.setColor(Color.blue); g.setFont(font);
g.drawString(text,fontX,fontY);
}
break;
case Press:
g.setColor(Color.gray); g.drawRect(0,0,width,height);
g.setColor(Color.white);
g.drawLine(0,height-1,width-1,height-1);
g.drawLine(width-1,0,width-1,height-1);
g. drawImage(PressImage,imageX+1,
imageY+1,basicColor,this);
if(text!=null) {
g.setColor(Color.red); g.setFont(font);
g.drawString(text,fontX+1,fontY+1);
}
break;
case Disable:
g. drawImage(DisableImage,imageX,
imageY,basicColor,this);
if(text!=null) {
g.setColor(Color.gray); g.setFont(font);
g.drawString(text,fontX,fontY);
}
break;
}
}
}
工具提示类
import java.awt.*;
import java.lang.String;
import java.lang.Thread;
public class Tooltips extends Canvas
implements java.lang.Runnable {
/* keep是工具提示留屏时间变量
delay是线程空运行时间变量,
到期线程将被暂停
h_sideSpace和v_sideSpace
是左右两侧和上下两侧的空白区域
Status变量记录线程所处的五种状态,初始值为消失
*/
private Font font;
private int x,y,width,height,fontX,fontY;
private int h_sideSpace=3,
v_sideSpace=3,keep=3,delay=10;
private java.lang.Thread draw;
private String text;
private int Status = 4;
private final static int Draw = 1;
//显示工具提示
private final static int stopDraw = 2;
//停止工具提示并清除
private final static int Clear = 3;
//清除工具提示
private final static int Hide = 4;
//隐藏工具提示
private final static int Suspend = 5;
//暂停线程
public Tooltips() {
this(new Font("Dialog",Font.PLAIN,12));
}
public Tooltips(Font font) {
this.font = font;
}
//提示显示,如果窗体区域不能满足要求,就不会显示
public synchronized void setVisible
(String text,Point mouse,Rectangle frame) {
if(text==null) return;
this.text = text;
FontMetrics fm = getFontMetrics(font);
int fontWidth = fm.stringWidth(this.text);
int fontHeight = fm.getHeight();
width = fontWidth + h_sideSpace*2;
height = fontHeight + v_sideSpace*2;
fontX = (width-fontWidth)/2;
fontY = (height - fontHeight)/2+fm.getAscent();
if(mouse.y-height-20 < 0&&mouse.y
+height+20 >=frame.height) return;
this.x=(mouse.x+width >=
frame.width?(frame.width-width-5 >0?
frame.width-width-5:0):mouse.x);
this.y=(mouse.y+20+height < =
frame.height?mouse.y+20:mouse.y-20-height);
if(draw==null) start();
else if(Status == Suspend) draw.resume();
Status = Draw;
}
public synchronized boolean isVisible() {
return (Status==Draw||Status==stopDraw)?true:false;
}
//清除提示文本
public synchronized void setCleared() {
if(Status!=Draw&&Status!=stopDraw) return;
Status = Clear;
}
public void setDelay(int delay) {
if(delay < 3) //空载时间最少3秒钟
throw new NumberFormatException
("delay value error,at the setDelay method");
this.delay = delay;
}
public void setKeep(int keep) {
if(keep < 1) //留屏时间最少1秒钟
throw new NumberFormatException
("keep value error,at the setKeep method");
this.keep = keep;
}
public void start() {
if(draw == null) {
draw = new java.lang.Thread(this,"draw text");
draw.start();
}
}
public void stop() {
if(draw!=null) {
draw.stop(); draw = null;
}
}
public void run() {
draw.setPriority(Thread.NORM_PRIORITY);
long beginTime=new java.util.Date().getTime();
while(true) {
switch(Status) {
case Draw:
super.setBounds(x,y,width,height);
repaint();
beginTime = new java.util.Date().getTime();
break;
case Clear:
super.setBounds(x,y,0,0);
Status = Hide;
beginTime = new java.util.Date().getTime();
break;
}
try {
Thread.sleep(500);
} catch(InterruptedException ie) {
ie.printStackTrace();
stop();
break;
}
if(Status==stopDraw && (new java.util.Date()
.getTime()-beginTime)/1000 >=keep)
Status = Clear;
else if(Status==Hide&&(new java.util.Date()
.getTime()-beginTime)/1000 >=delay) {
Status = Suspend; draw.suspend();
}
}
}
public void repaint() {
repaint(0,0,width,height);
}
public void update(Graphics g) {
g.setColor(getBackground());
g.fillRect(0,0,width,height);
g.setColor(getForeground());
paint(g);
}
public void paint(Graphics g) {
if(Status==Draw||Status==stopDraw) {
synchronized(this) {
g.setColor(Color.yellow);
g.fillRect(0,0,width,height);
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
g.setColor(Color.red); g.setFont(font);
g.drawString(text,fontX,fontY);
Status = stopDraw;
}
}
}
}
失效图象过滤类
public class DisableFilter extends
java.awt.image.RGBImageFilter {
private int red,green,blue,color;
public int filterRGB(int x,int y,int rgb) {
red = (rgb > >16)&0xff;
green = (rgb > >8)&0xff;
blue = (rgb > >0)&0xff;
if(isRange(red,192,5)&&isRange
(green,192,5)&&isRange(blue,192,5))
color = 192;
else if(isRange(red,255,5)&&isRange
(green,255,5)&&isRange(blue,255,5))
color = 192;
else if(isRange(red,0,5)&&isRange
(green,0,5)&&isRange(blue,0,5))
color = 255;
else
color = 128;
return (rgb&0xff000000|
(color < < 16)|(color < < 8)|color);
}
public boolean isRange(int
color,int base,int margin) {
return color< =base+margin&&color
>=base-margin?true:false;
}
}
---- 本 程 序 在Windows95 下, 用Borland JBuiler2+JDK1.1.6 调 试 通 过
---- 以 下 另 附 两 个 类, 其 中 一 个 是image 的byte 数 组 类, 另 一 类 是frame 的 派 生 类, 把 这 五 个 类 组 合 一 起, 浮 动 按 纽 即 可 执 行。
import java.awt.*;
import java.awt.event.*;
public class Multi extends Frame
implements ActionListener,MouseListener{
Button butExit = new Button();
ImageButton cmdExit = new ImageButton
(ImageCode.mbox,ImageCode.bomb,
ImageCode.sound1,ImageCode.mbox,
" 千 古 江 山",ImageButton.HORIZON);
ImageButton cmdStop =
new ImageButton(ImageCode.sound2);
ImageButton cmdInfor =
new ImageButton(ImageCode.cowSmall);
Tooltips tooltips =
new Tooltips(new Font("Dialog",Font.PLAIN,14));
java.lang.Thread refresh;
public void actionPerformed(ActionEvent e) {
if(e.getSource() == butExit) {
dispose();
System.exit(0);
}
}
public void mouseClicked(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
if(me.getSource()==cmdExit && cmdExit.setClicked()) {
cmdStop.setVisible(!cmdStop.isVisible());
}
else if(me.getSource()==cmdStop && cmdStop.setClicked()) {
cmdExit.setEnabled(!cmdExit.isEnabled());
}
else if(me.getSource()==cmdInfor
&& cmdInfor.setClicked());
}
public void mouseExited(java.
awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
if(me.getSource()==cmdExit
&& cmdExit.setExited());
else if(me.getSource()==cmdStop
&& cmdStop.setExited());
else if(me.getSource()==cmdInfor
&& cmdInfor.setExited());
}
public void mouseEntered
(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
Point mouse = me.getPoint();
Rectangle frame = this.getBounds();
if(me.getSource() == cmdExit
&& cmdExit.setEntered(mouse,frame));
else if(me.getSource()==cmdStop
&& cmdStop.setEntered(mouse,frame));
else if(me.getSource()==cmdInfor
&& cmdInfor.setEntered(mouse,frame))
cmdStop.setAlignment(cmdStop.getAlignment()
==ImageButton.VERTICAL?
ImageButton.HORIZON : ImageButton.VERTICAL);
}
public void mousePressed(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
if(me.getSource()==cmdExit
&& cmdExit.setPressed());
else if(me.getSource()==cmdStop
&& cmdStop.setPressed());
else if(me.getSource()==cmdInfor
&& cmdInfor.setPressed());
}
public void mouseReleased(java.awt.event.MouseEvent me) {
if(me.getModifiers()==4) return;
Point mouse = me.getPoint();
if(me.getSource()==cmdExit
&& cmdExit.setReleased(mouse));
else if(me.getSource()==cmdStop
&& cmdStop.setReleased(mouse));
else if(me.getSource()==cmdInfor
&& cmdInfor.setReleased(mouse));
}
public Multi() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Multi();
}
private void jbInit() throws NullPointerException {
Dimension d = getToolkit().getScreenSize();
this.setSize(new Dimension(536, 307));
this.setLocation((d.width-getSize().
width)/2,(d.height-getSize().height)/2);
this.setLayout(null);
this.setTitle(System.getProperty("java.version"));
this.setBackground(Color.lightGray);
this.validate(); this.setVisible(true);
this.add(tooltips,null);
butExit.setFont(new Font("Dialog", 0, 16));
butExit.setBounds(new Rectangle(352, 136, 170, 36));
butExit.setLabel("Exit Frame");
butExit.addActionListener(this);
this.add(butExit, null);
repaint(0,0,this.getSize().width,this.getSize().height);
cmdExit.setFont(new Font("Dialog",Font.PLAIN,14));
cmdExit.setLocation(new Point(10,35));
cmdExit.setTooltips(tooltips,
"君不见黄河之水天上来,奔流到海不复回");
cmdExit.setVisible(true);
cmdExit.addMouseListener(this);
this.add(cmdExit,null);
cmdStop.setText("大风起兮");
cmdStop.setFont(new Font("Dialog",Font.PLAIN,14));
cmdStop.setLocation(110,35);
cmdStop.setSize(cmdExit.getSize().
width,cmdExit.getSize().height);
cmdStop.setTooltips(tooltips,
"看茫茫大江来天畔,风烟几万年");
cmdStop.setVisible(true);
cmdStop.addMouseListener(this);
this.add(cmdStop,null);
cmdInfor.setText("对潇潇暮雨洒江天");
cmdInfor.setFont(new Font("Dialog",Font.PLAIN,14));
cmdInfor.setLocation(210,260);
cmdInfor.setSize(cmdInfor.getSize().
width+10,cmdExit.getSize().height);
cmdInfor.setTooltips(tooltips,
"渐霜风凄紧,关河冷落,残照当楼");
cmdInfor.setVisible(true);
cmdInfor.addMouseListener(this);
this.add(cmdInfor,null);
}
}
public class ImageCode {
public static byte mbox[]={
71,73,70,56,57,97,32,0,32,0,-94,0,0,-1,-1,-1,
-49,-49,-49,21,56,-2,0,51,-1,0,0,-1,0,0,-52,
0,0,-103,0,0,0,33,-7,4,1,0,0,1,0,44,0,0,0,0,
32,0,32,0,0,3,-59,24,-70,123,-2,44,-54,-87,
-100,24,120,8,72,123,-67,-104,-109,105,-114,
55,29,-96,99,20,98,-74,-103,12,26,22,-123,97,
15,45,121,-64,-63,49,-45,-76,71,-24,-63,-95,
-56,124,-124,36,43,-109,99,-18,-116,-95,12,-63,
-121,-93,-114,92,79,-119,85,68,-75,94,71,47,-19,
-107,-5,45,-109,-60,101,-81,-103,-119,30,-81,-53,
-31,70,-6,-99,-114,-88,-93,-12,-79,125,-18,36,10,
-39,49,124,56,0,-124,-123,-123,91,123,95,91,-122,
-116,-120,-127,-118,76,0,14,-110,7,-121,-128,114,
-112,33,-110,-108,-100,-114,-104,110,-102,-116,
-106,33,-119,-96,-125,68,-99,-105,
21,-126,-107,-83,-101,-124,-98,
-85,-103,-125,-94,-80,-86,61,-126,85,126,77,89,31,
22,76,121,122,118,59,50,56,-63,35,-67,13,31,-58,-57,
-92,30,62,119,116,-55,98,93,-69,-69,-50,29,40,93,
-49,-37,38,45,-45,-119,-32,90,
69,70,28,9,0,59
};
public static byte bomb[]={
71,73,70,56,57,97,20,0,22,0,-29,0,0,-1,-1,-1,-1,-52,
51,-52,-1,-1,-52,-52,-52,-69,-69,-69,-103,-103,-103,
-120,-120,-120,102,102,102,85,85,85,51,51,51,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,33,-2,78,84,104,105,115,32,
97,114,116,32,105,115,32,105,
110,32,116,104,101,32,112,117,98,108,105,99,32,
100,111,109,97,105,110,46,32,75,101,
118,105,110,32,72,117,103,104,101,115,44,32,107,
101,118,105,110,104,64,101,105,116,46,99,111,109,
44,32,83,101,112,116,101,109,98,101,114,32,49,57,
57,53,0,33,-7,4,1,0,0,2,
0,44,0,0,0,0,20,0,22,0,0,4,-113,48,-55,73,-89,-72,88,
36,-64,-69,-33,73,118,109,94,73,-122,-39,22,0,10,
-80,118,36,-128,-114,-35,-5,125,-40,
-90,4,73,-48,118,-118,68,112,
72,-29,4,10,37,-123,114,-87,-96,-83,20,5,91,-89,64,
45,52,53,31,-31,111,74,-67,-58,0,8,-88,86,49,-104,
122,61,98,4,-11,64,-26,88,-117,28,-91,
26,96,56,-124,-53,111,44,-70,64,
-96,-37,-123,0,121,95,44,85,127,-128,-126,73,7,
-118,8,18,74,-127,103,104,-117,-115,-114,-120,30,
3,65,-115,66,120,-112,-106,67,74,101,-113,112,-99,
-97,102,-94,37,-89,-126,9,85,-85,-84,84,66,
25,76,-79,-78,87,17,0,59
};
public static byte sound1[]={
71,73,70,56,57,97,20,0,22,0,-62,0,0,-1,-1,-1,-1,51,51,
-52,-1,-1,-52,-52,-52,-103,-103,-103,
102,0,0,51,51,51,0,0,0,33,-2,78,84,104,105,
115,32,97,114,116,32,105,
115,32,105,110,32,116,104,101,32,112,117,98,108,
105,99,32,100,111,109,97,105,110,46,32,75,101,118,
105,110,32,72,117,103,104,101,115,44,32,107,101,
118,105,110,104,64,101,105,116,46,99,
111,109,44,32,83,101,112,116,101,109,98,101,
114,32,49,57,57,53,0,33,-7,4,1,0,0,2,0,44,0,0,0,0,
20,0,22,0,0,3,107,40,-70,-36,-2,48,54,-61,66,-112,-22,28,
97,85,-113,7,-79,113,23,9,-123,-64,104,126,13,
-102,114,-98,119,105,52,1,-36,91,-41,21,65,72,
-4,-74,-37,107,119,-31,-71,-124,-62,13,79,-80,52,
6,-111,56,102,113,122,-124,42,-89,-46,42,
-14,42,-51,18,12,-33,111,-46,-44,-100,-47,14,3,
67,84,-121,21,24,-34,6,-76,58,23,51,77,-28,42,54,
36,62,24,125,74,123,26,118,44,123,20,
11,-124,24,-119,-118,11,9,0,59
};
public static byte sound2[]={
71,73,70,56,57,97,20,0,22,0,-62,0,0,-1,-1,-1,
-52,-1,-1,-52,-52,-52,-103,-103,-103,102,102,
102,51,51,51,0,0,0,0,0,0,33,-2,78,84,104,105,
115,32,97,114,116,32,105,
115,32,105,110,32,116,104,101,32,112,117,98,
108,105,99,32,100,111,109,97,105,110,46,32,75,
101,118,105,110,32,72,117,103,104,101,115,44,32,
107,101,118,105,110,104,64,101,105,116,46,99,
111,109,44,32,83,101,112,116,101,109,98,
101,114,32,49,57,57,53,0,33,-7,4,1,0,0,1,0,44,0,0,
0,0,20,0,22,0,0,3,80,24,-70,-36,-2,-50,72,-8,-92,45,116,89,
67,36,-90,27,33,122,80,40,10,87,-55,-99,-101,-15,
69,28,122,-111,85,91,-36,52,-20,-35,74,-34,-20,-81,
-128,-113,49,20,-70,84,47,27,-78,103,0,12,-118,
-52,79,-13,57,84,26,55,65,-119,
51,-121,-51,110,125,55,30,-47,-102,97,-18,
-54,-116,112,16,-51,94,36,0,0,59
};
public static byte cowSmall[]={
71,73,70,56,57,97,27,0,19,0,-12,0,0,-1,-1,-1,-1,-4,
-4,-1,-5,-5,-1,-8,-8,-1,-9,-9,-1,-14,-14,-1,-19,-19,-1,
-25,-25,-1,-28,-28,-1,-37,-37,-1,-42,-42,-1,-51,-51,-64,
-64,-64,-44,-44,-44,-65,-65,-65,-86,-86,-86,127,127,
127,85,85,85,63,63,63,42,42,42,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,33,-7,4,1,0,0,12,0,44,0,0,0,0,27,0,19,
0,0,5,-37,32,35,-114,100,105,-98,100,-12,-96,-24,58,
62,17,20,21,-113,12,65,78,3,-23,
81,36,57,-127,32,-94,39,122,72,38,18,-60,-124,-62,
100,20,100,-58,-55,114,-46,-88,-11,-112,35,8,-59,-79,
-12,53,2,19,-56,-108,73,-127,28,39,17,-20,-120,-62,96,
-90,113,18,-78,60,66,89,
82,-120,34,-57,-115,25,70,-58,-53,123,20,127,
126,48,47,18,18,116,20,1,119,118,18,5,1,13,14,12,
104,17,14,70,36,53,12,113,13,116,90,77,1,6,6,-113,
65,122,44,104,13,20,
15,117,59,5,34,-94,1,12,8,15,8,40,102,-100,86,5,5,57,
16,65,85,58,13,44,16,-94,97,-66,34,8,13,-78,
-64,59,44,15,95,-121,53,43,85,78,59,18,16,46,40,14,6,53,
14,-36,15,-36,12,6,55,-37,44,12,-110,15,-41,47,36,56,
-28,12,43,-24,69,-25,34,42,-28,-15,39,-25,-110,-19,
-20,-42,49,-17,-36,-8,-106,-28,-52,93,115,
-48,-61,-38,-117,7,33,0,0,59
};
public static byte dot[]={
71,73,70,56,57,97,13,0,13,0,-94,-1,0,-1,0,0,
-64,-64,-64,-52,0,0,-103,0,0,-128,0,0,102,0,0,0,0,0,
0,0,0,33,-7,4,1,0,0,1,0,44,0,0,0,0,
13,0,13,0,64,3,42,24,-70,-78,91,-112,12,33,-128,5,
10,22,73,47,118,12,40,6,-43,-105,69,83,-23,-119,-27,
-56,-104,-25,-106,94,15,-38,-83,26,-89,90,
65,62,-13,-82,10,40,1,0,59
};
}
- 上一篇: JAVA语言中链表和双向链表的实现
- 下一篇: JAVA数据流的应用
-= 资 源 教 程 =-
文 章 搜 索