码迷,mamicode.com
首页 > 其他好文 > 详细

大神求解答......

时间:2016-06-06 17:02:14      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

我在写推箱子游戏时,出现了有的箱子推的动,有的箱子推不动,代码如下:

package youxi;

import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;

import org.junit.Test;

public class Mainframe extends Frame implements KeyListener {
int wx;
int wy;
public Mainframe()
{
cage();
sheepinit();
wolfinit();
treeInit();
background();
setMainframe();
this.addKeyListener(this);

}
private void setMainframe()
{

this.setTitle("推箱子");
this.setLocation(200,100);
this.setSize(800,600);
//this.setResizable(false);
this.setVisible(true);
}
private void wolfinit()
{
wx=6;
wy=6;
Icon i=new ImageIcon("-10.png");
lab_wolf=new JLabel(i);
lab_wolf.setBounds(wx*50, wy*50, 50, 50);
this.add(lab_wolf);

}
JLabel lab_wolf;

private void sheepinit()
{
Icon i=new ImageIcon("sheep.png");

JLabel lab_sheep1=new JLabel(i);
lab_sheep1.setBounds(350, 150, 50, 50);
this.add(lab_sheep1);
datas[3][7]=4;
sheeps[3][7]=lab_sheep1;

JLabel lab_sheep2=new JLabel(i);
lab_sheep2.setBounds(250, 450, 50, 50);
this.add(lab_sheep2);
datas[9][5]=4;
sheeps[9][5]=lab_sheep2;

JLabel lab_sheep3=new JLabel(i);
lab_sheep3.setBounds(300, 450, 50, 50);
this.add(lab_sheep3);
datas[9][6]=4;
sheeps[9][6]=lab_sheep3;


}

JLabel[][] sheeps=new JLabel[12][16];
int[][] datas={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};

int num=0;
int total=3;
private void treeInit()
{
Icon ic=new ImageIcon("tree.png");
for(int i=0;i<datas.length;i++)
{
for(int j=0;j<datas[i].length;j++)
{
if(datas[i][j]==1)
{
JLabel lab_tree=new JLabel(ic);
lab_tree.setBounds(j*50, i*50, 50, 50);
this.add(lab_tree);


}
}
}
}
private void cage()
{
Icon i=new ImageIcon("cage.png");
JLabel lab_cage1=new JLabel(i),
lab_cage2=new JLabel(i),
lab_cage3=new JLabel(i);
lab_cage1.setBounds(550, 300, 50, 50);
lab_cage2.setBounds(400, 300, 50, 50);
lab_cage3.setBounds(550, 400, 50, 50);
this.add(lab_cage1);
this.add(lab_cage2);
this.add(lab_cage3);

}
private void victory()
{
if(num == total){
//移除窗体键盘事件,避免用户多余操作
this.removeKeyListener(this);
//以下代码为广告植入,需要与美工配合后最终定稿
JDialog victory = new JDialog(this,"恭喜你取得了胜利!传智播客——JAVA培训行业领军旗舰",true);
victory.setSize(400, 300);
victory.setLocationRelativeTo(null);
victory.setLayout(null);

JLabel info = new JLabel(new ImageIcon("gg.jpg"));
info.setBounds(2, 2, 380, 180);
victory.add(info);

JLabel click = new JLabel(new ImageIcon("gg2.jpg"));
click.setBounds(2, 182, 380, 88);
click.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e) {
try {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.itcast.cn");
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
victory.add(click);
victory.setVisible(true);
}
}

private void background()
{
Icon i=new ImageIcon("floor.png");
JLabel lab=new JLabel(i);
lab.setBounds(0, 0, 600, 800);
this.add(lab);

}

public void keyReleased(KeyEvent e)
{
int key =e.getKeyCode();
if(key==39)
{
if(datas[wy][wx+1]==0)
{
wx=wx+1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x+50, y);
Icon i=new ImageIcon("right.png");
lab_wolf.setIcon(i);
return;
}
if(datas[wy][wx+1]==1)
{
return;
}
if(datas[wy][wx+1]==4&&datas[wy][wx+2]==1)
{
return;
}
if(datas[wy][wx+1]==4&&datas[wy][wx+2]==4)
{
return;
}
if(datas[wy][wx+1]==4&&datas[wy][wx+2]==12)
{
return;
}
if(datas[wy][wx+1]==12&&datas[wy][wx+2]==1)
{
return;
}
if(datas[wy][wx+1]==12&&datas[wy][wx+2]==4)
{
return;
}
if(datas[wy][wx+1]==12&&datas[wy][wx+2]==12)
{
return;
}
if(datas[wy][wx+1] == 8){
wx = wx+1;
int x =(int)lab_wolf.getLocation().getX();
int y =(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x+50, y);
Icon i = new ImageIcon("right.png");
lab_wolf.setIcon(i);
return;
}

if(datas[wy][wx+1]==4&&datas[wy][wx+2]==0)
{
datas[wy][wx+1]=0;
datas[wy][wx+2]=4;
}
if(datas[wy][wx+1]==12&&datas[wy][wx+2]==0)
{
datas[wy][wx+1]=8;
datas[wy][wx+2]=4;
num--;
}
if(datas[wy][wx+1]==12&&datas[wy][wx+2]==8)
{
datas[wy][wx+1]=8;
datas[wy][wx+2]=12;
}

if(datas[wy][wx+1]==4&&datas[wy][wx+2]==8)
{
datas[wy][wx+1]=0;
datas[wy][wx+2]=12;
num++;
}
sheeps[wy][wx+1].setLocation(50*wx+100,50*wy);
sheeps[wy][wx+2]=sheeps[wy][wx+1];
sheeps[wy][wx+1]=null;
wx=wx+1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x+50, y);
Icon i=new ImageIcon("right.png");
lab_wolf.setIcon(i);
victory();
return;

}

if(key==37)
{
if(datas[wy][wx-1] == 0){
wx = wx-1;
int x =(int)lab_wolf.getLocation().getX();
int y =(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x-50, y);
Icon i = new ImageIcon("left.png");
lab_wolf.setIcon(i);
return;
}
if(datas[wy][wx-1]==1)
{
return;
}
if(datas[wy][wx-1]==4&&datas[wy][wx-2]==1)
{
return;
}
if(datas[wy][wx-1]==4&&datas[wy][wx-2]==4)
{
return;
}
if(datas[wy][wx-1]==4&&datas[wy][wx-2]==12)
{
return;
}
if(datas[wy][wx-1]==12&&datas[wy][wx-2]==1)
{
return;
}
if(datas[wy][wx-1]==12&&datas[wy][wx-2]==4)
{
return;
}
if(datas[wy][wx-1]==12&&datas[wy][wx-2]==12)
{
return;
}
if(datas[wy][wx-1]==8)
{
wx=wx-1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x-50, y);
Icon i=new ImageIcon("left.png");
lab_wolf.setIcon(i);
return;
}
if(datas[wy][wx-1]==4&&datas[wy][wx-2]==0)
{
datas[wy][wx-1]=0;
datas[wy][wx-2]=4;
}
if(datas[wy][wx-1]==12&&datas[wy][wx-2]==0)
{
datas[wy][wx-1]=8;
datas[wy][wx-2]=4;
num--;
}
if(datas[wy][wx-1]==12&&datas[wy][wx-2]==8)
{
datas[wy][wx-1]=8;
datas[wy][wx-2]=12;
}

if(datas[wy][wx-1]==4&&datas[wy][wx-2]==8)
{
datas[wy][wx-1]=0;
datas[wy][wx-2]=12;
num++;
}
sheeps[wy][wx-1].setLocation(50*wx-100,50*wy);
sheeps[wy][wx-2]=sheeps[wy][wx+1];
sheeps[wy][wx-1]=null;

wx=wx-1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x-50, y);
Icon i=new ImageIcon("right.png");
lab_wolf.setIcon(i);
victory();
return;

}
if(key==38)
{
if(datas[wy-1][wx] == 8){
wy = wy-1;
int x =(int)lab_wolf.getLocation().getX();
int y =(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y-50);
Icon i = new ImageIcon("up.png");
lab_wolf.setIcon(i);
return;
}
if(datas[wy-1][wx]==1)
{
return;
}
if(datas[wy-1][wx]==4&&datas[wy-2][wx]==1)
{
return;
}
if(datas[wy-1][wx]==4&&datas[wy-2][wx]==4)
{
return;
}
if(datas[wy-1][wx]==4&&datas[wy-2][wx]==12)
{
return;
}
if(datas[wy-1][wx]==12&&datas[wy-2][wx]==1)
{
return;
}
if(datas[wy-1][wx]==12&&datas[wy-2][wx]==4)
{
return;
}
if(datas[wy-1][wx]==12&&datas[wy-2][wx]==12)
{
return;
}
if(datas[wy-1][wx]==0)
{
wy=wy-1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y-50);
Icon i=new ImageIcon("up.png");
lab_wolf.setIcon(i);
return;
}
if(datas[wy-1][wx]==4&&datas[wy-2][wx]==0)
{
datas[wy-1][wx]=0;
datas[wy-2][wx]=4;
}
if(datas[wy-1][wx]==12&&datas[wy-2][wx]==0)
{
datas[wy-1][wx]=8;
datas[wy-2][wx]=4;
num--;
}
if(datas[wy-1][wx]==12&&datas[wy-2][wx]==8)
{
datas[wy-1][wx]=8;
datas[wy-2][wx]=12;
}

if(datas[wy-1][wx]==4&&datas[wy-2][wx]==8)
{
datas[wy-1][wx]=0;
datas[wy-2][wx]=12;
num++;

}
sheeps[wy-1][wx].setLocation(50*wx,50*wy-100);
sheeps[wy-2][wx]=sheeps[wy-1][wx];
sheeps[wy-1][wx]=null;
wy=wy-1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y-50);
Icon i=new ImageIcon("up.png");
lab_wolf.setIcon(i);
victory();
return;

}


if(key==40)
{
if(datas[wy+1][wx] == 8){
wy = wy+1;
int x =(int)lab_wolf.getLocation().getX();
int y =(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y+50);
Icon i = new ImageIcon("-10.png");
lab_wolf.setIcon(i);
return;
}
if(datas[wy+1][wx]==1)
{
return;
}
if(datas[wy+1][wx]==4&&datas[wy+2][wx]==1)
{
return;
}
if(datas[wy+1][wx]==4&&datas[wy+2][wx]==4)
{
return;
}
if(datas[wy+1][wx]==4&&datas[wy+2][wx]==12)
{
return;
}
if(datas[wy+1][wx]==12&&datas[wy+2][wx]==1)
{
return;
}
if(datas[wy+1][wx]==12&&datas[wy+2][wx]==4)
{
return;
}
if(datas[wy+1][wx]==12&&datas[wy+2][wx]==12)
{
return;
}
if(datas[wy+1][wx]==0)
{
wy=wy+1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y+50);
Icon i=new ImageIcon("-10.png");
lab_wolf.setIcon(i);
return;
}
if(datas[wy+1][wx]==4&&datas[wy+2][wx]==0)
{
datas[wy+1][wx]=0;
datas[wy+2][wx]=4;
}
if(datas[wy+1][wx]==12&&datas[wy+2][wx]==0)
{
datas[wy+1][wx]=8;
datas[wy+2][wx]=4;
num--;
}
if(datas[wy+1][wx]==12&&datas[wy+2][wx]==8)
{
datas[wy+1][wx]=8;
datas[wy+2][wx]=12;
}

if(datas[wy+1][wx]==4&&datas[wy+2][wx]==8)
{
datas[wy+1][wx]=0;
datas[wy+2][wx]=12;
num++;
sheeps[wy+1][wx].setLocation(50*wx,50*wy+100);
sheeps[wy+2][wx]=sheeps[wy+1][wx];
sheeps[wy+1][wx]=null;
}
wy=wy+1;
int x=(int)lab_wolf.getLocation().getX();
int y=(int)lab_wolf.getLocation().getY();
lab_wolf.setLocation(x, y+50);
Icon i=new ImageIcon("right.png");
lab_wolf.setIcon(i);
victory();
return;

}
}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
return;
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
return;
}
}

错误提示如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at youxi.Mainframe.keyReleased(Mainframe.java:416)
at java.awt.Component.processKeyEvent(Unknown Source)

求大神答疑解惑。

大神求解答......

标签:

原文地址:http://www.cnblogs.com/wzl521/p/5563975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!