📄 boombutton.java
字号:
// BoomButton
// by Patrick Killelea (patrick@neog.com, http://challenger.neog.com/~patrick/)
// Thanks to Elijah Dean Meeker (http://www.realtime.net/~elijah/) and
// thanks to Sean E. Russell, University of Oregon for pieces of their
// applet code and gifs. Thanks to Steve Tomljenovic (stomljen@neog.com)
// for MediaTracker tip.
// Also thanks to http://sunsite.sut.ac.jp/multimed/sounds/sound_effects/
// for the explosion sound.
// Please report bugs and improvement suggestions to patrick@neog.com.
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.InterruptedException;
import java.applet.Applet;
import java.applet.AudioClip;
public class BoomButton extends java.applet.Applet implements Runnable {
private Image img[] = new Image[6];
public int state = 0;
private AudioClip clip1;
Thread anim;
Button goButton=null;
String button_text;
/****************************STATE CHANGES*************************************/
public void init(){
MediaTracker tracker = new MediaTracker(this);
button_text = getParameter("button_text");
goButton = new Button(button_text);
add(goButton);
clip1 = getAudioClip(getCodeBase(), "sounds/Explosion-3.au");
img[0] = getImage(getDocumentBase(), "images/home2.gif");
tracker.addImage(img[0], 0);
img[1] = getImage(getDocumentBase(), "images/bang1.gif");
tracker.addImage(img[1], 0);
img[2] = getImage(getDocumentBase(), "images/puff1.gif");
tracker.addImage(img[2], 0);
img[3] = getImage(getDocumentBase(), "images/puff2.gif");
tracker.addImage(img[3], 0);
img[4] = getImage(getDocumentBase(), "images/puff3.gif");
tracker.addImage(img[4], 0);
img[5] = getImage(getDocumentBase(), "images/puff4.gif");
tracker.addImage(img[5], 0);
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
try { tracker.waitForAll(); }
catch (InterruptedException e) {
System.out.println("Interrupted while loading images.");
System.exit(0);
}
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
} // end init
public void start(){
anim=null;
repaint();
}//end start
public void stop(){
if (anim!=null) {
anim.stop();
anim=null;
}
}//end stop
public void destroy(){
}//end destroy
/****************************END STATE CHANGES********************************/
/*******************************EVENTS****************************************/
public boolean action(Event evt, Object obj) {
if (obj.equals(button_text)) {
anim = new Thread(this);
anim.start();
//goButton=null;
removeAll();
return(true);
}
return false;
}
public boolean mouseDown(Event f, int x, int y){
//anim = new Thread(this);
//anim.start();
add(goButton);
return(true);
}//end mouseDown
public boolean mouseUp(Event e, int x, int y){
return(true);
}//end mouseUp
public boolean mouseMove(Event e, int x, int y){
showStatus("");
return(true);
}//end mouseEnter
public boolean mouseEnter(Event e, int x, int y){
showStatus(""); // Try to hide that this is an applet.
return(true);
}//end mouseEnter
public boolean mouseExit(Event e, int x, int y){
return(true);
}//end mouseExit
/*******************************END EVENTS*************************************/
/*******************************METHODS****************************************/
public void paint(Graphics g){
switch (state)
{
case 0:
//g.drawImage(img[0], 0, 0, this);
break;
case 1: // Explode 1 (Flash)
g.drawImage(img[1], 0, 0, this);
break;
case 2: // Explode 2 (Smoke)
g.drawImage(img[2], 0, 0, this);
break;
case 3: // Explode 3
g.drawImage(img[3], 0, 0, this);
break;
case 4: // Explode 4
g.drawImage(img[4], 0, 0, this);
break;
case 5: // Explode 5
g.drawImage(img[5], 0, 0, this);
break;
} // switch
} // end paint
public void run() {
// repaint();
if (clip1 != null) clip1.play();
for (state=1; state<=5; state++) {
repaint();
try {anim.sleep(75);}
catch (InterruptedException e){}
} // for
state=0;
repaint();
}
/*****************************END METHODS**************************************/
}//end class BoomButton
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -