在eclipse中加入图片
网上找的flappybird的一段代码 但是图片始终加不进去 并且提示是这样的
at javax.imageio.ImageIO.read(Unknown Source)
at flappybird.Bird.<init>(Bird.java:29)
at flappybird.World.start(World.java:45)
at flappybird.World.<init>(World.java:38)
at flappybird.FlappyBird.main(FlappyBird.java:26)
package flappybird;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Bird {
BufferedImage[] images;//保存鸟的所有活动状态
BufferedImage image;//保存当前鸟的状态
int x;
int y;
int width;
int height;
int size;
double g;//重力加速度
double h;//经过时间t秒后的垂直位移
double s;//经过时间t秒后的水平位移
double t;//时间
double speed;//速度
double alpha;//倾角
int index;//当前图片的下标
public Bird()throws IOException{
images = new BufferedImage[8];
for(int i = 0;i<8;i++)
{
images[i] = ImageIO.read(getClass().getResource(i+".png"));
//System.out.println(getClass().getResource(""));
}
image = images[0];
width = image.getWidth();
height = image.getHeight();
x = 132;
y = 280;
size = 40;
g = 4;
h = 0;
s = 0;
speed = 20;
t = 0.25;
alpha = 0;
index = 0;
}
public void step()
{
double v0 = speed;
h = v0*t - 0.5*g*t*t;
double vt = v0 - g*t;
s = v0*t;
y = y - (int)h;
speed = vt;
alpha = Math.atan(h/8);
}
//鸟翅膀挥动
public void fly(){
index++;
image = images[(index/8)%8];
//0 1 2 3 4 5 6 7 8 9 10 11 12 13 ......
//(/8) 0 0 0 0 0 0 0 0 1 1 1 1......
//(%8) 0 0 0 0 0 0 0 0 1 1 1 1......
}
public void flappy()
{
speed = 20;
}
//鸟撞击地面检查
public boolean hit(Ground ground)
{
boolean hit = (y + size/2 >= ground.y);
if(hit){
alpha = -Math.PI/2;
}
return hit;
}
//鸟撞击到柱子上的检查
public boolean hit(Column column)
{
int x1 = column.x - column.width/2 - size/2;
int x2 = column.x - column.width/2 + size/2;
int y1 = column.y - column.gap/2 + size/2;
int y2 = column.y - column.gap/2 - size/2;
if(x>x1&&y<y2)
{
if(y>y1&&y<y2){//除去柱子的间隙
return false;
}
return true;
}
return false;
}
}