java坦克大战游戏怎么加载放在在文件夹中的图片

 我来答
百度网友b8db7464d
推荐于2016-04-13 · TA获得超过904个赞
知道小有建树答主
回答量:186
采纳率:100%
帮助的人:242万
展开全部
我前两天刚好写完这个,你可以参考一下,下面是我的Tank类和爆炸(Explode)类的代码:
里面的Explode.class.getClassLoader().getResource("images/0.gif")是先得到类加载器,然后再用它来得到图片Explode.class你换成自己的类就行
---------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
public class Explode {
private int x,y;
private TankClient tc;
private boolean isLive=true;
private static boolean init=false;
private static Toolkit tool=Toolkit.getDefaultToolkit();
private static Image[] images={
tool.getImage(Explode.class.getClassLoader().getResource("images/0.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/1.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/2.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/3.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/4.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/5.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/6.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/7.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/8.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/9.gif")),
tool.getImage(Explode.class.getClassLoader().getResource("images/10.gif"))
};
int step=0;
public Explode(int x, int y, TankClient tc) {
this.x = x;
this.y = y;
this.tc = tc;
}
public void draw(Graphics g){
if(!init){
for(int i=0;i<images.length;i++)
g.drawImage(images[i], 0, 0,null);
init=true;
}
if(!isLive){
tc.explodes.remove(this);
return;
}
if(step==images.length){
this.isLive=false;
this.step=0;
return;
}
g.drawImage(images[step], x, y, null);
step++;
}
}

---------------------------
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Tank {
public static final int XSPEED=5;
public static final int YSPEED=5;
public static final int WIDTH=30;
public static final int HEIGHT=30;
private int x, y;
private TankClient tc;
private int oldX,oldY;
private boolean bl=false,bu=false,bd=false,br=false;
protected Direction dir=Direction.STOP;
protected Direction ptdir=Direction.D;
protected static int lifeCount=100;
private int superFireCount;
private BloodBar bb=new BloodBar();
private static Random random=new Random();
protected boolean good;
private boolean isLive=true;
private int step=random.nextInt(10)+8;
private static Toolkit tool=Toolkit.getDefaultToolkit();
private static Image[] images=null;
private static Map<String,Image> imgs=new HashMap<String,Image>();
static {
images = new Image[] {
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankL.gif")),
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankLU.gif")),
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankLD.gif")),
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankR.gif")),
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankRU.gif")),
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankRD.gif")),
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankU.gif")),
tool.getImage(Explode.class.getClassLoader().getResource(
"images/tankD.gif"))
};
imgs.put("L", images[0]);
imgs.put("LU", images[1]);
imgs.put("LD", images[2]);
imgs.put("R", images[3]);
imgs.put("RU", images[4]);
imgs.put("RD", images[5]);
imgs.put("U", images[6]);
imgs.put("D", images[7]);
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}
public boolean isLive() {
return isLive;
}
public Tank(int x, int y,boolean good) {
this.x = x;
this.y = y;
this.oldX=x+30;
this.oldY=y+30;
this.good=good;
}
public Tank(int x,int y,boolean good,Direction dir,TankClient tc){
this(x,y,good);
this.dir=dir;
this.tc=tc;
this.superFireCount=Integer.parseInt(PropertyMgr.getProperty("superFireCount"));
}
public Rectangle getRec(){
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public void move(){
this.oldX=x;
this.oldY=y;
switch(dir){
case L:
x-=XSPEED;
break;
case LU:
x-=XSPEED;
y-=YSPEED;
break;
case R:
x+=XSPEED;
break;
case RD:
x+=XSPEED;
y+=YSPEED;
break;
case D:
y+=YSPEED;
break;
case LD:
x-=XSPEED;
y+=YSPEED;
break;
case U:
y-=YSPEED;
break;
case RU:
x+=XSPEED;
y-=YSPEED;
break;
}
if(this.dir!=Direction.STOP)
this.ptdir=this.dir;
if(x<0)x=0;
if(y<20)y=20;
if(x+Tank.WIDTH>TankClient.width)x=TankClient.width-Tank.WIDTH;
if(y+Tank.HEIGHT>TankClient.height)y=TankClient.height-Tank.HEIGHT;
if(!good) {
this.step--;
if(random.nextInt(40)>=38)
fire();
}
if(!good&&step==0){
Direction[] dire=Direction.values();
int temp=random.nextInt(dire.length);
this.dir = dire[temp];
this.step=random.nextInt(10)+8;
}
}
public void draw(Graphics g) {
if(good&&isLive)
bb.draw(g);
if(!isLive){
if(!good)
tc.enamyTanks.remove(this);
return;
}
switch(ptdir){
case L:
g.drawImage(imgs.get("L"), x, y, null);
break;
case LU:
g.drawImage(imgs.get("LU"), x, y, null);
break;
case R:
g.drawImage(imgs.get("R"), x, y, null);
break;
case RD:
g.drawImage(imgs.get("RD"), x, y, null);
break;
case D:
g.drawImage(imgs.get("D"), x, y, null);
break;
case LD:
g.drawImage(imgs.get("LD"), x, y, null);
break;
case U:
g.drawImage(imgs.get("U"), x, y, null);
break;
case RU:
g.drawImage(imgs.get("RU"), x, y, null);
break;
}
move();
}
private void locateDirection(){
if(bl&&!bu&&!br&&!bd)
dir=Direction.L;
else if(!bl&&bu&&!br&&!bd)
dir=Direction.U;
else if(!bl&&!bu&&br&&!bd)
dir=Direction.R;
else if(!bl&&!bu&&!br&&bd)
dir=Direction.D;
else if(bl&&!bu&&!br&&bd)
dir=Direction.LD;
else if(bl&&bu&&!br&&!bd)
dir=Direction.LU;
else if(!bl&&!bu&&br&&bd)
dir=Direction.RD;
else if(!bl&&bu&&br&&!bd)
dir=Direction.RU;
else if(!bl&&!bu&&!br&&!bd)
dir=Direction.STOP;
}
public void keyReleased(KeyEvent e){
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_RIGHT:
br=false;
break;
case KeyEvent.VK_LEFT :
bl=false;
break;
case KeyEvent.VK_UP :
bu=false;
break;
case KeyEvent.VK_DOWN :
bd=false;
break;
}
locateDirection();
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_RIGHT:
br = true;
break;
case KeyEvent.VK_LEFT:
bl = true;
break;
case KeyEvent.VK_UP:
bu = true;
break;
case KeyEvent.VK_DOWN:
bd = true;
break;
case KeyEvent.VK_CONTROL:
fire();
break;
case KeyEvent.VK_A:
if(superFireCount>0){
superFire();
this.superFireCount--;
}
break;
}
locateDirection();
}
public void fire(){
if(!isLive)return;
int x=this.x+Tank.WIDTH/2-Missile.WIDTH/2;
int y=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2;
Missile m=new Missile(x, y, ptdir,this.good,this.tc);
tc.missiles.add(m);
}
public void superFire(){
if(!isLive)return;
int x=this.x+Tank.WIDTH/2-Missile.WIDTH/2;
int y=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2;
Direction[] temp=Direction.values();
for(int i=0;i<temp.length-1;i++){
Missile m=new Missile(x, y, temp[i],this.good,this.tc);
tc.missiles.add(m);
}
}
public boolean collodesWithWall(Wall w){
if(this.getRec().intersects(w.getRec())){
this.stay();
return true;
}
return false;
}
public boolean collodesWithTanks(List<Tank> tank) {
for (int i = 0; i < tank.size(); i++) {
Tank t = tank.get(i);
if (this.isLive&&t.isLive&&this.getRec().intersects(t.getRec())&&this!=t) {
if(this.good==t.good){
this.stay();
t.stay();
return true;
}
else{
if(this.good){
if(this.lifeCount>=0){
this.lifeCount-=20;
t.setLive(false);
tc.explodes.add(new Explode(x,y,tc));
return true;
}
else{
this.isLive=false;
t.setLive(false);
tc.explodes.add(new Explode(x,y,tc));
return true;
}
}
else{
if(t.lifeCount>0){
t.lifeCount-=20;
this.setLive(false);
tc.explodes.add(new Explode(x,y,tc));
return true;
}
else{
this.isLive=false;
t.setLive(false);
tc.explodes.add(new Explode(x,y,tc));
return true;
}
}
}
}
}
return false;
}
public void stay(){
this.x=oldX;
this.y=oldY;
}
public boolean eat(Blood b){
if(this.isLive&&b.isLive()&&this.good&&this.getRec().intersects(b.getRec())){
this.lifeCount=100;
b.setLive(false);
return true;
}
return false;
}
private class BloodBar{
public void draw(Graphics g){
Color c=g.getColor();
g.setColor(Color.black);
g.drawRect(x, y-10, WIDTH, 10);
int w=WIDTH*lifeCount/100;
g.setColor(Color.red);
g.fillRect(x, y-10, w, 10);
g.setColor(c);
}
}
public int getSuperFireCount() {
return superFireCount;
}
public void setSuperFireCount(int superFireCount) {
this.superFireCount = superFireCount;
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友845f74e61
2012-07-23 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1617万
展开全部
继承PANEL类
重写paint()
方法。

参照
http://zhidao.baidu.com/question/426770971.html?oldq=1
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式