我正在编写一个游戏,我现在正在努力的是玩家旋转但没有成功。下面是错误信息:
Exception in thread "Thread-2" java.lang.NullPointerException
at Schoo.NewGame.SafeIcon.getIconWidth(SafeIcon.java:29)
at Schoo.NewGame.TheRealGame.turn(TheRealGame.java:412)
at Schoo.NewGame.TheRealGame.turnPlayer(TheRealGame.java:427)
at Schoo.NewGame.TheRealGame.RepaintPlayer(TheRealGame.java:377)
at Schoo.NewGame.TheRealGame$2.run(TheRealGame.java:225)
at java.lang.Thread.run(Unknown Source)
SafeIcon中的第29行:
return wrappee.getIconHeight();
更多内容:
private Icon wrappee;
private Icon standIn;
public SafeIcon(Icon icon) {
wrappee = icon;
}
@Override
public int getIconHeight() {
return wrappee.getIconHeight();
}
@Override
public int getIconWidth() {
return wrappee.getIconWidth();
}
TheRealGame中的第412行:
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
更多内容:
Icon icon;
public void turn(Direction d){
if (pt == Playere.Kaneki){
icon = new SafeIcon(playerImage.getIcon());
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
if (d == Direction.LEFT && p.getDirection() == Direction.UP){
g.rotate(Math.PI / 2, playerImage.getWidth(), playerImage.getHeight());
playerImage.getIcon().paintIcon(null, g, 0, 0);
g.dispose();
}
}
if (pt == Playere.Touka){
}
}
这是我找到safeIcon的地方。 如果你需要我的整个主要课程:
public class TheRealGame{
private static Bullets b = new Bullets();
private static boolean running = false;
private static boolean paused = false;
private static boolean right = false, left = false, up = false, down = false;
private static JFrame f;
private static ArrayList<JLabel> ae = new ArrayList<JLabel>();
private static Player p;
private static Playere pt;
private static JLabel playerImage;
private static Icon playerIcon;
private static ImageIcon imageIcon;
private static boolean attacking = false;
private static boolean info = false;
private static JLabel iy, ix, im, in, iu;
private static Enemys enemys = new Enemys();
private static ArrayList<JLabel> bullets = new ArrayList<JLabel>();
public static void main(Playere playertype){
pt = playertype;
p = new Player(pt);
f = new JFrame();
f.setVisible(true);
f.setSize(700, 700);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
down = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
right = true;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
if (attacking == false){
p.attack();
}
}
if (e.getKeyCode() == KeyEvent.VK_F3){
if (info == true){
info = false;
iy.setVisible(false);
ix.setVisible(false);
im.setVisible(false);
in.setVisible(false);
iu.setVisible(false);
}else if (info == false){
info = true;
iy.setVisible(true);
ix.setVisible(true);
im.setVisible(true);
in.setVisible(true);
iu.setVisible(true);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP || e.getKeyChar() == KeyEvent.VK_W){
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyChar() == KeyEvent.VK_A){
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyChar() == KeyEvent.VK_S){
down = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyChar() == KeyEvent.VK_D){
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
if (attacking == true)
p.attack();
}
}
@Override
public void keyTyped(KeyEvent e) {
}
});
playerImage = new JLabel();
iy = new JLabel();
ix = new JLabel();
im = new JLabel();
in = new JLabel();
iu = new JLabel();
iy.setLocation(0, 10);
ix.setLocation(0, 20);
im.setLocation(0, 30);
in.setLocation(0, 40);
iu.setLocation(0, 50);
iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 300, 15);
f.add(ix);
f.add(iy);
f.add(im);
f.add(in);
f.add(iu);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("free play - tokyo ghoul");
Start();
p.Paint();
}
public static void resume(){
if (paused == false){
return;
}
}
public static void pause(){
if (paused == true){
return;
}
}
public static void Stop(){
if (running == false){
return;
}
running = false;
}
public static void move(JLabel l, int newX, int newY){
l.setLocation(newX, newY);
l.repaint();
f.repaint();
}
public static void remove(JLabel l){
f.remove(l);
l.setVisible(false);
}
public static JLabel getPlayerImage(){
return playerImage;
}
public static Player getPlayer(){
return p;
}
public static void Start(){
running = true;
new Thread(new Runnable(){
@Override
public void run() {
int last = 0, u = 0;
while (running == true){
if (paused != true){
if (up == true){
p.move(p.getX(), p.getY()-1);
if (p.getDirection() != Direction.UP)
turnPlayer(Direction.UP);
}
if (down == true){
p.move(p.getX(), p.getY()+1);
if (p.getDirection() != Direction.DOWN)
turnPlayer(Direction.DOWN);
}
if (left == true){
p.move(p.getX()-1, p.getY());
if (p.getDirection() != Direction.LEFT)
turnPlayer(Direction.LEFT);
}
if (right == true){
p.move(p.getX()+1, p.getY());
if (p.getDirection() != Direction.RIGHT)
turnPlayer(Direction.RIGHT);
}
if (info == true){
int l = 10-last;
iy.setText("y: "+p.getY());
ix.setText("x: "+p.getX());
im.setText("enemys: "+ae.size());
in.setText("next enemy: "+l);
iu.setText("Updated "+u+" times.");
RefreshInfo();
}
if (p.getY() >= +334){
p.move(p.getX(), +334);
}
if (p.getY() <= -334){
p.move(p.getX(), -334);
}
if (p.getX() >= 675){
p.move(675, p.getY());
}
if (p.getX() <= -12){
p.move(-12, p.getY());
}
RepaintAllEnemys();
Enemy.UpdateAll();
playerImage.repaint();
RepaintPlayer();
enemys.Update();
b.Update();
f.repaint();
if (info != true){
f.repaint();
}
if (last == 10){
Random r = new Random();
int x = 1+r.nextInt(2), y = 1+r.nextInt(2), distance = 1+r.nextInt(570), nx = 0, ny = 0;
if (x == 1){
}
last = 0;
}
last++;
u++;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void RefreshInfo(){
f.remove(iy);
f.remove(ix);
f.remove(im);
f.remove(in);
f.remove(iu);
f.add(ix);
f.add(iy);
f.add(im);
f.add(in);
f.add(iu);
iy.setLocation(0, 10);
ix.setLocation(0, 20);
im.setLocation(0, 30);
in.setLocation(0, 40);
iu.setLocation(0, 50);
iu.setBounds((int) iu.getLocation().getX(), (int) iu.getLocation().getY(), 200, 15);
iy.setBounds((int) iy.getLocation().getX(), (int) iy.getLocation().getY(), 100, 15);
ix.setBounds((int) ix.getLocation().getX(), (int) ix.getLocation().getY(), 100, 15);
im.setBounds((int) im.getLocation().getX(), (int) im.getLocation().getY(), 100, 15);
in.setBounds((int) in.getLocation().getX(), (int) in.getLocation().getY(), 100, 15);
f.repaint();
}
public static void UpdateAll(){
}
public static void Paint(JLabel imgs, int x, int y, Icon file){
Icon img = file;
imgs.setIcon(img);
imgs.setBounds(x, y, img.getIconWidth(), img.getIconHeight());
imgs.setLocation(x, y);
imgs.setVisible(true);
f.add(imgs);
imgs.setVisible(true);
}
public static void addBullet(JLabel l, int x, int y, Icon icon){
Paint(l, x, y, icon);
bullets.add(l);
}
public static void moveBullet(int newX, int newY, JLabel label){
if (bullets.contains(label)){
label.setLocation(newX, newY);
label.repaint();
f.repaint();
label.setVisible(true);
}
}
public static void Repaint(JLabel l){
f.remove(l);
l.setBounds((int)l.getLocation().getX(), (int)l.getLocation().getY(), l.getWidth(), l.getHeight());
f.add(l);
}
public static void addAE(JLabel l){
ae.add(l);
}
public static void RepaintAllEnemys(){
for (int i = 0; i < ae.size(); i++){
ae.get(i).repaint();
}
}
public static void MovePlayer(int x, int y){
playerImage.setLocation(x, y);
playerImage.repaint();
f.repaint();
}
public static void paint(JLabel l, int Y, int X){
l.setLocation(X, Y);
l.setBounds(X, Y, l.getIcon().getIconWidth(), l.getIcon().getIconHeight());
l.setVisible(true);
f.add(l);
l.setVisible(true);
l.repaint();
f.repaint();
}
public static void RepaintPlayer(){
if (p.isAttacking() == true){
if (pt == Playere.Kaneki){
if (attacking == false){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_hit.png"));
playerImage.setIcon(img);
playerIcon = img;
attacking = true;
imageIcon = img;
}
}
if (pt == Playere.Touka){
if (attacking == false){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_shoot.png"));
playerImage.setIcon(img);
playerIcon = img;
attacking = true;
imageIcon = img;
}
}
}else{
if (pt == Playere.Kaneki){
if (attacking == true){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png"));
playerImage.setIcon(img);
playerIcon = img;
attacking = false;
imageIcon = img;
}
}
if (pt == Playere.Touka){
if (attacking == true){
ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png"));
playerImage.setIcon(img);
playerIcon = img;
attacking = false;
imageIcon = img;
}
}
}
turnPlayer(p.getDirection());
MovePlayer(p.getX(), p.getY());
playerImage.repaint();
f.repaint();
}
public static void paintplayer(){
if (pt == Playere.Kaneki){
ImageIcon imgs = new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.png"));
playerImage.setIcon(imgs);
playerImage.setBounds(p.getX(), p.getY(), imgs.getIconWidth(), imgs.getIconHeight());
playerImage.setLocation(274, 277);
playerImage.setVisible(true);
f.add(playerImage);
playerImage.setVisible(true);
imageIcon = imgs;
}
if (pt == Playere.Touka){
playerImage.setIcon(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")));
playerImage.setBounds(p.getX(), p.getY(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconWidth(), new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/touka_walk.png")).getIconHeight());
playerImage.setLocation(274, 277);
playerImage.setVisible(true);
f.add(playerImage);
playerImage.setVisible(true);
playerIcon = playerImage.getIcon();
}
}
public static Bullets getBullets(){
return b;
}
Icon icon;
public void turn(Direction d){
if (pt == Playere.Kaneki){
icon = new SafeIcon(playerImage.getIcon());
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
if (d == Direction.LEFT && p.getDirection() == Direction.UP){
g.rotate(Math.PI / 2, playerImage.getWidth(), playerImage.getHeight());
playerImage.getIcon().paintIcon(null, g, 0, 0);
g.dispose();
}
}
if (pt == Playere.Touka){
}
}
public static void turnPlayer(Direction d){
try {
TheRealGame.class.newInstance().turn(d);
} catch (InstantiationException e) {
System.out.println("Something went wrong!");
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println("Something went wrong!");
e.printStackTrace();
}
}
public enum Direction{
LEFT, UP, RIGHT, DOWN;
}
}
答案 0 :(得分:0)
那是因为当您在SafeIcon
中创建icon = new SafeIcon(playerImage.getIcon());
实例时,您正在为wrapee
变量定义null。
如果你需要wrapee为非null值,请调整构造函数以使其更安全,这样可以更容易地检测NPE的根本原因:
public SafeIcon(Icon icon) {
if (icon == null) {
throw new NullPointerException("icon"); // or IllegalArgumentException
}
this.wrapee = icon;
}
然后检查playerImage JLabel
实例是否应始终定义图标。如果是这样,请为其定义一个图标,您的问题就会得到解决。