public class PicButton extends Applet implements MouseListener
{
private int width,height;//长宽
private Image offI,img,img1,img2;
private Graphics offG;
private MediaTracker imageTracker;
private boolean isMouseEnter = false, isPress = false;
private Color ButtonColor=Color.yellow,lightC,darkC;
private URL url;
private AudioClip sound1, sound2;
private String param;
public void init(){//初始化
param = new String();
param = getParameter("sound1");\\接受外部参数(路径参数)
if(param == null)
param = "1.au";//默认为1.au
sound1 = getAudioClip(getDocumentBase(), param); //getDocumentBase(),是你运行时的当前目录
param = getParameter("sound2");//同上
if(param == null)
param = "2.au";
sound2 = getAudioClip(getDocumentBase(), param);
width = getSize().width;//设置长宽
height = getSize().height;
param = getParameter("URL");
try{
url = new URL(param);
}catch(MalformedURLException e){}
offI = createImage(width,height);\\创建图画(长宽如上)
offG = offI.getGraphics();
imageTracker = new MediaTracker(this);
// img = new Image();
param = getParameter("Images");\\和上面代码类似
img = getImage(getCodeBase(), param);
imageTracker.addImage(img, 0); param = getParameter("Images1");
img1 = getImage(getCodeBase(), param);
imageTracker.addImage(img1, 0); param = getParameter("Images2");
img2 = getImage(getCodeBase(), param);
imageTracker.addImage(img2, 0);
try{
imageTracker.waitForID(0);
}catch(InterruptedException e){}
addMouseListener(this);\\添加事件监听器(自己)
}
public void start(){\\小应用程序开始
offG.drawImage(img,0,0,width,height,this);\\画图
repaint();\\重画
}
public void mouseClicked(MouseEvent e){
} public void mousePressed(MouseEvent e){\\鼠标按下事件
offG.drawImage(img2,0,0,width,height,this);
repaint();
sound1.play();
System.out.println("sound2 play");
} public void mouseReleased(MouseEvent e){\\鼠标松开事件
offG.drawImage(img1,0,0,width,height,this);
repaint();
sound2.play();
getAppletContext().showDocument(url);
} public void mouseEntered(MouseEvent e){\\鼠标进入该区域事件
offG.drawImage(img1,0,0,width,height,this);
repaint();
} public void mouseExited(MouseEvent e){\\鼠标离开事件
offG.drawImage(img,0,0,width,height,this);
repaint();
}
public void paint(Graphics g)\\小应用程序开始时和repaint()都要调用的事件
{
g.drawImage(offI,0,0,width,height,this);
}
}
{
private int width,height;//长宽
private Image offI,img,img1,img2;
private Graphics offG;
private MediaTracker imageTracker;
private boolean isMouseEnter = false, isPress = false;
private Color ButtonColor=Color.yellow,lightC,darkC;
private URL url;
private AudioClip sound1, sound2;
private String param;
public void init(){//初始化
param = new String();
param = getParameter("sound1");\\接受外部参数(路径参数)
if(param == null)
param = "1.au";//默认为1.au
sound1 = getAudioClip(getDocumentBase(), param); //getDocumentBase(),是你运行时的当前目录
param = getParameter("sound2");//同上
if(param == null)
param = "2.au";
sound2 = getAudioClip(getDocumentBase(), param);
width = getSize().width;//设置长宽
height = getSize().height;
param = getParameter("URL");
try{
url = new URL(param);
}catch(MalformedURLException e){}
offI = createImage(width,height);\\创建图画(长宽如上)
offG = offI.getGraphics();
imageTracker = new MediaTracker(this);
// img = new Image();
param = getParameter("Images");\\和上面代码类似
img = getImage(getCodeBase(), param);
imageTracker.addImage(img, 0); param = getParameter("Images1");
img1 = getImage(getCodeBase(), param);
imageTracker.addImage(img1, 0); param = getParameter("Images2");
img2 = getImage(getCodeBase(), param);
imageTracker.addImage(img2, 0);
try{
imageTracker.waitForID(0);
}catch(InterruptedException e){}
addMouseListener(this);\\添加事件监听器(自己)
}
public void start(){\\小应用程序开始
offG.drawImage(img,0,0,width,height,this);\\画图
repaint();\\重画
}
public void mouseClicked(MouseEvent e){
} public void mousePressed(MouseEvent e){\\鼠标按下事件
offG.drawImage(img2,0,0,width,height,this);
repaint();
sound1.play();
System.out.println("sound2 play");
} public void mouseReleased(MouseEvent e){\\鼠标松开事件
offG.drawImage(img1,0,0,width,height,this);
repaint();
sound2.play();
getAppletContext().showDocument(url);
} public void mouseEntered(MouseEvent e){\\鼠标进入该区域事件
offG.drawImage(img1,0,0,width,height,this);
repaint();
} public void mouseExited(MouseEvent e){\\鼠标离开事件
offG.drawImage(img,0,0,width,height,this);
repaint();
}
public void paint(Graphics g)\\小应用程序开始时和repaint()都要调用的事件
{
g.drawImage(offI,0,0,width,height,this);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货