大神求解答......

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

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)

求大神答疑解惑。

时间: 2024-08-29 12:20:12

大神求解答......的相关文章

求大神帮解答javaEE这个问题,谢谢了

List<Privilege> privilegeList = privilegeService.findAll(); ActionContext.getContext().put("privilegeList", privilegeList); <s:checkboxlist name="privilegeIds" list="#privilegeList" listKey="id" listValue=&

关于添加微信公众号报错问题~求大神解答~

  弄微信请求添加永久图文素材时报错 madia id 是永久图片素材没错的 但还是包这个错 求大神解答!!如图:

[LeetCode]LRU Cache有个问题,求大神解答

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

条件运算符与逗号的嵌套的疑问,求大神解答

最近在学习条件运算符时,遇到条件运算符与逗号的嵌套的问题.有如下题目: 请大神分析输出结果?感激不尽!

求问各位大神关于fragment切换,软键盘隐藏的问题

============问题描述============ 问题背景,一个顶层linearlayout下面有个两个子linearlayout,第一个子linearlayout里面给一排button,可以水平滑动,第二个子linearlayout里面用来放fragment,xml如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="

python 抓取搜狗微信出现的问题,求大神解决

爬取到的data不是想要获取文章页面的源码,而是跳转到验证码的页面的源码.我网上查了一下是由于访问过于频繁导致的,我也加了time.sleep和改了请求头但还是显示不了,求大神支招,除了识别验证码的方式还能怎么做?? import re import urllib.request import time import urllib.error headers = {'User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/2

开发的一个android程序,总是显示无法运行,就退出了。但是没有报错。求大神解救啊!

============问题描述============ 代码如下: 在网上查有人说是intent的问题 原来没有加入监听器的时候没有问题 我是个新手,问题有点没水平啦~ 求大神啊.谢谢谢谢 ============解决方案1============ 把log贴上来看看,是不是menu这个activity没有在manifest 声明 ============解决方案2============ setonclicklistener那句话,把new helplistener前面的那个类型转换删除 =

用java制作简单登陆窗口,求大神教导

import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import java

hdu 1008为何不对?求大神指导!

/*#include<iostream> using namespace std; int main(){ int n; while (cin >> n){ if (n == 0) break; else if (n > 0 && n < 100){ int sum=0, max,min; int *f = new int[n]; for (int i = 0; i < n; i++) cin >> f[i]; max = min =