📄 cloud.java
字号:
package airplane;
import java.awt.Image;
import java.applet.Applet;
import java.util.*;
public class Cloud extends SuperSprite implements Runnable{
int AppletWidth, AppletHeight;
int moveWay; //白云移动的方向:0 = 从左到右;1 = 从右到左
int cloudImgWidth,cloudImgHeight;
Thread cloudThread;
Random R;
int moveStep = 0;
public Cloud(Image cloudImg,
int startX,
int startY,
int moveWay,
Applet Game,
int AppletWidth,
int AppletHeight) {
super(cloudImg, startX, startY, Game); //调用父类的创建方法
this.moveWay = moveWay;
cloudImgWidth = cloudImg.getWidth(Game);
cloudImgHeight = cloudImg.getHeight(Game);
moveStep = cloudImgWidth/100;
this.AppletWidth = AppletWidth;
this.AppletHeight = AppletHeight;
setVisible(true); //不可见
setMove(true); //不可移动
R = new Random();
cloudThread = new Thread(this);
cloudThread.setPriority(Thread.MAX_PRIORITY);
cloudThread.start();
}
public void updateState() { //更新白云Sprite的状态
if (active == true) {
switch (moveWay) {
case 0: { //左 --> 右
if (X > AppletWidth) { //当白云穿过右边界时,重新从左开始
do{
Y = R.nextInt() % (AppletHeight - cloudImgHeight);
}while(Y<=0);
X = -cloudImgWidth;
} else
X = X + moveStep; //向右移动1像素
break;
}
case 1: { //右 --> 左
if (X < -cloudImgWidth) { //当白云穿过左边界时,重新从右开始
do{
Y = R.nextInt() % (AppletHeight - cloudImgHeight);
}while(Y<=0);
X = AppletWidth;
} else
X = X - moveStep; //向左移动1像素
}
}
}
}
public void stop() { //stop()方法
cloudThread = null;
}
public void run() {
while (cloudThread!=null) {
try {
Thread.sleep(100);
} catch (InterruptedException E) {}
this.updateState();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -