//Fish定义了一条鱼游动的一些参量以及如何在Panel上显示鱼
package fish;
import java.util.Random;
public class Fish extends Thread{
private Random rand = new Random();
int fishID; //鱼的ID号
int x; //该鱼的x坐标
int y; //该鱼的y坐标
int width; //该鱼使用的图片的宽度
int height; //该鱼使用的图片的高度
//随机产生鱼初始游的方向
int down = rand.nextInt(10)%2;//down=1 : 向下游;down=0 : 向上游
int right = rand.nextInt(10)%2;//right=1: 向右游
//用来控制刷新周期,sleepTime越小,两次刷新之间的时间间隔就越小,从而速度就越快
private int sleepTime = rand.nextInt(50);
//随机产生斜游角度比x:y=angle,即在x方向上游动angle次之后才在y方向上移动一次
private int angle = rand.nextInt(5)+2;
//随机产生鱼上下左右游动的最大上限,即振幅
private int xSwing = rand.nextInt(1500);
private int ySwing = rand.nextInt(800);
private int countXSwing = 0;//x方向上振幅计数
private int countYSwing = 0;//y方向上振幅计数
//一个数组,包含了上述除了鱼ID的所有参量
private int[] fishStates = new int[12];
boolean isRunning;
private FishPanel panel;
public Fish(int fishID,int x,int y,int width,int height,FishPanel panel,boolean isRunning){
this.fishID = fishID;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.panel = panel;
this.isRunning = isRunning;
this.start();
}
public Fish(int fishID,int[] state,FishPanel panel,boolean isRunning){
this.fishID = fishID;
this.x = state[0];
this.y = state[1];
this.width = state[2];
this.height = state[3];
this.down = state[4];
this.right = state[5];
this.sleepTime = state[6];
this.angle = state[7];
this.xSwing = state[8];
this.ySwing = state[9];
this.countXSwing = state[10];
this.countYSwing = state[11];
this.panel = panel;
this.isRunning = isRunning;
this.start();
}
public void run()
{
while(isRunning)
{
computeX();
computeY();
try
{
sleep(sleepTime);//sleepTime
}
catch(Exception ex){
ex.printStackTrace();
}
panel.repaint();
}
}
public void reStart(){
this.isRunning = true;
}
public void halt()
{
this.isRunning = false;
}
public void computeX(){
if(right == 1){
if(countXSwing == xSwing)
{//判断是否到右端边框或达到最大振幅
right=0;
reset('x');
}
if(x >= panel.getWidth()){
this.x = -width;
StringBuffer buffer = new StringBuffer();
int[] states = this.getFishStates();
for(int i = 0 ;i < states.length;i ++){
if(i!=0)buffer.append(FishConstants.ARRAY_SEPARATOR);
buffer.append(states[i]);
}
buffer.append('\n');
panel.messageManager.sendMessage(Integer.toString(this.fishID),buffer.toString());
//reset('x');
this.halt();
}
x++;
}
else
{
if(countXSwing == xSwing)
{//判断是否到左端边框或达到最大振幅
right=1;
reset('x');
}
if(x+width <= 0){
this.x = panel.getWidth();
StringBuffer buffer = new StringBuffer();
int[] states = this.getFishStates();
for(int i = 0 ;i < states.length;i ++){
if(i!=0)buffer.append(FishConstants.ARRAY_SEPARATOR);
buffer.append(states[i]);
}
buffer.append('\n');
panel.messageManager.sendMessage(Integer.toString(this.fishID),buffer.toString());
this.halt();
}
x--;
}
countXSwing++;
}
//计算鱼的上下游动
public void computeY(){
//判断随机数l是否为0,是则水平游动,否则计算角度
if(countXSwing%angle == 0){
if(down == 1)//x former
{//down=1表示向下游且当x++或x--了l次后,y++
if( countYSwing == ySwing)
{//判断是否到底或上下振幅游满否
down=0;
reset('y');
}
if(y >= panel.getHeight()){
this.y = -height;
StringBuffer buffer = new StringBuffer();
int[] states = this.getFishStates();
for(int i = 0 ;i < states.length;i ++){
if(i!=0)buffer.append(FishConstants.ARRAY_SEPARATOR);
buffer.append(states[i]);
}
buffer.append('\n');
panel.messageManager.sendMessage(Integer.toString(this.fishID),buffer.toString());
this.halt();
//reset('y');
}
y++;
}
else if(countXSwing%angle == 0)//x former
{//当向上游且当x++或x--了l次之后,y--
if(countYSwing == ySwing)
{
down=1;
reset('y');
}
if(y+height <= 0)
{
this.y = panel.getHeight();
StringBuffer buffer = new StringBuffer();
int[] states = this.getFishStates();
for(int i = 0 ;i < states.length;i ++){
if(i!=0)buffer.append(FishConstants.ARRAY_SEPARATOR);
buffer.append(states[i]);
}
buffer.append('\n');
panel.messageManager.sendMessage(Integer.toString(this.fishID),buffer.toString());
this.halt();
//reset('y');
}
y--;
}
countYSwing++;
}
}
public void reset(char direction){
//鱼游动状态改变后重新生成各项数值
if(direction=='x')
{
xSwing = rand.nextInt(1500);//重新生成新的左右振幅(判断依据:x)
countXSwing = 0; //左右振幅计数清0
}
else
{
ySwing = rand.nextInt(800);//重新生成新的上下振幅(判断依据:y)
countYSwing = 0;//上下振幅计数清0
}
sleepTime = rand.nextInt(50);//重新生成新的速度
angle = rand.nextInt(5)+2;//重新生成新的角度比
}
public int[] getFishStates(){
fishStates[0] = this.x;
fishStates[1] = this.y;
fishStates[2] = this.width;
fishStates[3] = this.height;
fishStates[4] = this.down;
fishStates[5] = this.right;
fishStates[6] = this.sleepTime;
fishStates[7] = this.angle;
fishStates[8] = this.xSwing;
fishStates[9] = this.ySwing;
fishStates[10] = this.countXSwing;
fishStates[11] = this.countYSwing;
return fishStates;
}
}