import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.Vector;
public class Test extends Frame implements Runnable {
private static final long serialVersionUID = 1L;
Image aquariumImage, memoryImage;
Graphics memoryGraphics;
int numberFish = 8;
int sleepTime = 110;
Vector<Fish> fishes = new Vector<Fish>();
boolean runOK = true;
Image[] fishImages = new Image[2];
MediaTracker tracker;
Thread thread;
Test() {
setTitle("The Aquarium");
tracker = new MediaTracker(this);
URL fish1Address = getClass().getResource("resource/fish1.gif");
URL fish2Address = getClass().getResource("resource/fish2.gif");
URL bubblesAddress = getClass().getResource("resource/tank.jpg");
// fishImages[0] = Toolkit.getDefaultToolkit().getImage("fish1.gif");
fishImages[0] = Toolkit.getDefaultToolkit().getImage(fish1Address);
tracker.addImage(fishImages[0], 0);
// fishImages[1] = Toolkit.getDefaultToolkit().getImage("fish2.gif");
fishImages[1] = Toolkit.getDefaultToolkit().getImage(fish2Address);
tracker.addImage(fishImages[1], 0);
// aquariumImage = Toolkit.getDefaultToolkit().getImage("bubbles.gif");
aquariumImage = Toolkit.getDefaultToolkit().getImage(bubblesAddress);
tracker.addImage(aquariumImage, 0);
try {
tracker.waitForID(0);
} catch(Exception ex) {
System.out.println(ex.getMessage());
}
setSize(aquariumImage.getWidth(this), aquariumImage.getHeight(this));
setResizable(false);
setVisible(true);
memoryImage = createImage(getSize().width, getSize().height);
memoryGraphics = memoryImage.getGraphics();
thread = new Thread(this);
thread.start();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
runOK = false;
System.exit(0);
}
});
}
public void run() {
Rectangle edges = new Rectangle(0 + getInsets().left, 0 + getInsets().top, getSize().width
- (getInsets().left + getInsets().right), getSize().height - (getInsets().top + getInsets().bottom));
for(int loopIndex = 0; loopIndex < numberFish; loopIndex++) {
fishes.add(new Fish(fishImages[0], fishImages[1], edges, this));
try {
Thread.sleep(20);
} catch(Exception exp) {
System.out.println(exp.getMessage());
}
}
Fish fish;
while(runOK) {
for(int loopIndex = 0; loopIndex < numberFish; loopIndex++) {
fish = (Fish) fishes.elementAt(loopIndex);
fish.swim();
}
try {
Thread.sleep(sleepTime);
} catch(Exception exp) {
System.out.println(exp.getMessage());
}
repaint();
}
}
public void update(Graphics g) {
memoryGraphics.drawImage(aquariumImage, 0, 0, this);
for(int loopIndex = 0; loopIndex < numberFish; loopIndex++) {
((Fish) fishes.elementAt(loopIndex)).drawFishImage(memoryGraphics);
}
g.drawImage(memoryImage, 0, 0, this);
}
public static void main(String[] args) {
new Test();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
软件价值13-水族箱 My Aquarium
共17个文件
gif:4个
xml:3个
class:3个
0 下载量 127 浏览量
2024-02-13
19:58:39
上传
评论
收藏 1.16MB ZIP 举报
温馨提示
myAquarium项目文件
资源推荐
资源详情
资源评论
收起资源包目录
MyAquarium.zip (17个子文件)
MyAquarium
MyAquarium.iml 433B
src
Fish.java 2KB
resource
fish1.gif 2KB
tank.jpg 590KB
fish2.gif 2KB
Test.java 3KB
out
production
MyAquarium
Test.class 4KB
Test$1.class 684B
Fish.class 2KB
resource
fish1.gif 2KB
tank.jpg 590KB
fish2.gif 2KB
.idea
workspace.xml 2KB
misc.xml 276B
modules.xml 267B
.gitignore 50B
.gitignore 344B
共 17 条
- 1
资源评论
dracularking
- 粉丝: 1597
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功