用java程序模拟的水族箱。
想象:
在这个喧嚣的世界中,总是有一些角落让人感到宁静、美丽和惬意。水族箱就是其中之一。闭上眼睛,想象一下,透过清澈的玻璃,你仿佛看到了一个小小的海洋世界,那里栖息着各色各样的鱼儿。
在这个水族箱里,水清澈见底,微光透过水面洒下,投射出斑斓的光影。在这柔和的光线下,鱼儿们如梦幻般地穿梭来回,它们的身影时隐时现,仿佛在跳着优美的舞蹈。
这个水族箱仿佛是一个小小的海洋世界,充满了生机与活力。每一条鱼儿都是如此美丽、灵动,它们的存在让整个空间充满了生命的力量,让人心情愉悦,仿佛身临其境般感受到了大自然的美妙。在这里,你可以忘却烦恼,感受到内心的平静和安宁。
打开你的心扉,让这个水族箱带给你一份宁静和美好,让那些灵动的鱼儿在你的心灵深处留下美好的印记。
截图:
代码:
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Random;
class Fish {
Component tank;
Image image1;
Image image2;
Point location;
Point velocity;
Rectangle edges;
Random random;
public Fish(Image image1, Image image2, Rectangle edges, Component tank) {
random = new Random(System.currentTimeMillis());
this.tank = tank;
this.image1 = image1;
this.image2 = image2;
this.edges = edges;
this.location = new Point(100 + (Math.abs(random.nextInt()) % 300),
100 + (Math.abs(100 + random.nextInt()) % 100));
this.velocity = new Point(random.nextInt() % 8, random.nextInt() % 8);
}
public void swim() {
if(random.nextInt() % 7 <= 1) {
velocity.x += random.nextInt() % 4;
velocity.x = Math.min(velocity.x, 8);
velocity.x = Math.max(velocity.x, -8);
velocity.y += random.nextInt() % 4;
velocity.y = Math.min(velocity.y, 8);
velocity.y = Math.max(velocity.y, -8);
}
location.x += velocity.x;
location.y += velocity.y;
if(location.x < edges.x) {
location.x = edges.x;
velocity.x = -velocity.x;
}
if((location.x + image1.getWidth(tank)) > (edges.x + edges.width)) {
location.x = edges.x + edges.width - image1.getWidth(tank);
velocity.x = -velocity.x;
}
if(location.y < edges.y) {
location.y = edges.y;
velocity.y = -velocity.y;
}
if((location.y + image1.getHeight(tank)) > (edges.y + edges.height)) {
location.y = edges.y + edges.height - image1.getHeight(tank);
velocity.y = -velocity.y;
}
}
public void drawFishImage(Graphics g) {
if(velocity.x < 0) {
g.drawImage(image1, location.x, location.y, tank);
} else {
g.drawImage(image2, location.x, location.y, tank);
}
}
}
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(fish1Address);
tracker.addImage(fishImages[0], 0);
fishImages[1] = Toolkit.getDefaultToolkit().getImage(fish2Address);
tracker.addImage(fishImages[1], 0);
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();
}
}
运行:
my aquarium