捕鱼达人(三)

    在上篇文章里,我们建好了基本的所需要的界面,现在再来开始一条鱼的游动之旅吧。当一条鱼开始游动的时候,
程序建立后,在Eclipse的Package Explorer中,将看到如图所示的界面:
                           
在这里,使得程序运行的主程序为:Main.java
 
package my.main;

import java.util.Timer;

import my.ui.FishFrame;
import my.ui.FishPanel;
import my.ui.WelcomeWindow;

public class Main {
	public static void main(String[] args) throws Exception{
		WelcomeWindow window=new WelcomeWindow();
		
		FishFrame frame=new FishFrame();
		FishPanel panel=new FishPanel();
		frame.add(panel);
		window.setVisible(true);
		
		window.setVisible(false);
		frame.setVisible(true);
		while(true){
			panel.go();
		}
	}
}
    还有游动的鱼的类  Fish.java
package my.ui;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Fish {
	    FishPanel panel;
	    public void setPanel(FishPanel panel){
	    	this.panel=panel;
	    }
	    //当前帧的鱼图片
		BufferedImage fishImage;
		//鱼游动10帧的动画
		BufferedImage[] animation=new BufferedImage[10];
		//鱼翻转8帧的动画
		BufferedImage[] animation_catch=new BufferedImage[8];
		
		int location_x;
		int location_y;
		
		public Fish(){
			init();
		}
		
		public void init(){
			//初始化鱼游动的数组
			try {
				String fishImagePath="";//"images\\fish13_01.png"
				//初始化鱼游动的动画数组
				for(int i=1;i<=10;i++){
					String index= (i<10?"0":"")+i;
					fishImagePath="images\\fish13_"+index+".png";
					File fishFile=new File(fishImagePath);
					animation[i-1]=ImageIO.read(fishFile);
				}
				//初始化鱼翻转的动画数组
				for(int i=1;i<=2;i++){
					for(int j=1;j<=4;j++){
						fishImagePath="images\\fish13_catch_0"+j+".png";
						System.out.println(fishImagePath);
						File fishFile=new File(fishImagePath);
						System.out.println("****"+fishFile.getName());
						animation_catch[j+(i-1)*4-1]=ImageIO.read(fishFile);
					}
				}
				fishImage=animation[0];
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		public void swimming(){
			location_x=800;
			location_y=200;
			int step=1;
			while(true){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				step=step%10;
				System.out.println("鱼游动到了"+(step+1)+"帧...");
				fishImage=animation[step++];
				location_x -= 10;
				panel.repaint();//重新执行paint(),完成界面的重绘
				//判断鱼是否撞墙 并完成翻转动画
				if(location_x<=0){
					break;
				}
			}
		}
		//执行翻转的动画
		public void turnover(){
			for(int i=1;i<=8;i++){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				fishImage=animation_catch[i-1];
				panel.repaint();
			}
		}
		//鱼被清理出界面
		public void getout(){
			fishImage=null;
			panel.repaint();
		}
}
     在这个一条鱼的游动的程序里,鱼作为的是面板的一部分而存在着,被面板控制着,比较难以处理的就是关于获取图像,这是细节问题,根据图片的编号进行设计。
    最后的程序结果为:

   并且鱼还能完成撞到窗口最左边的时候会翻转,然后消失。都是通过for循环来实现的。
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值