为什么我的 JLabel (ImageIcon) 在我尝试旋转它时给我一个错误?

Why is my JLabel (ImageIcon) giving me an error when i try to rotate it?

我正在编写一个游戏,当他向左移动时,图像应该向左旋转,但不是这样,它给我和错误:

Exception in thread "Thread-2" java.lang.NullPointerException
at Schoo.NewGame.TheRealGame.turnPlayer(TheRealGame.java:365)
at Schoo.NewGame.TheRealGame.run(TheRealGame.java:154)
at java.lang.Thread.run(Unknown Source)

是的,我知道 java.lang.NullPointerException 意味着某些东西是空的,但没有任何东西 应该 无论如何这里是代码第 365 行:

g2.rotate(90.0+90.0+90.0);

第 154 行只是运行方法:

public static void turnPlayer(Direction d){
    Graphics g = playerImage.getGraphics();
    Graphics2D g2 = (Graphics2D) g;
    if (d == Direction.LEFT){
        if (p.getDirection() == Direction.LEFT){
            return;
        }
        if (p.getDirection() == Direction.UP){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.RIGHT){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.DOWN){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    if (d == Direction.UP){
        if (p.getDirection() == Direction.UP){
            return;
        }
        if (p.getDirection() == Direction.RIGHT){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.DOWN){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.LEFT){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    if (d == Direction.RIGHT){
        if (p.getDirection() == Direction.RIGHT){
            return;
        }
        if (p.getDirection() == Direction.DOWN){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.LEFT){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.UP){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    if (d == Direction.DOWN){
        if (p.getDirection() == Direction.DOWN){
            return;
        }
        if (p.getDirection() == Direction.LEFT){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.UP){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.RIGHT){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    RepaintPlayer();
    f.repaint();
    playerImage.repaint();
}

如果你需要整个 class:

public class TheRealGame{

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 boolean info = false;
private static JLabel iy, ix, im, in, iu;

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){
                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){
                p.attack();
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {

        }
    });
    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(), 100, 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 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() == -337){
                        p.move(p.getX(), 337);
                    }
                    if (p.getY() == 337){
                        p.move(p.getX(), -337);
                    }
                    if (p.getX() == -349){
                        p.move(349, p.getY());
                    }
                    if (p.getX() == 349){
                        p.move(-349, p.getY());
                    }
                    RepaintAllLabels();
                    Enemy.UpdateAll();
                    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(), 100, 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, File file){
    ImageIcon img = new ImageIcon(file.getPath());
    imgs.setBounds(x, y, img.getIconWidth(), img.getIconHeight());
    imgs.setLocation(x, y);
    imgs.setVisible(true);

    f.add(imgs);
    imgs.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 RepaintAllLabels(){
    for (int i = 0; i < ae.size(); i++){
        Repaint(ae.get(i));
    }
}

public static void MovePlayer(int x, int y){
    playerImage.setLocation(x, y);
    playerImage.repaint();
    f.repaint();
}

public static void RepaintPlayer(){
    if (p.isAttacking()){
        if (pt == Playere.Kaneki){
            ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_hit.png"));
            playerImage = new JLabel(img);
        }
        if (pt == Playere.Touka){
            ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_hit.png"));
            playerImage = new JLabel(img);
        }
    }else{
        if (pt == Playere.Kaneki){
            ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/Kaneki_walk.png"));
            playerImage = new JLabel(img);
        }
        if (pt == Playere.Touka){
            ImageIcon img = new ImageIcon(StartMenu.class.getResource("/Schoo/NewGame/touka_walk.png"));
            playerImage = new JLabel(img);
        }
    }
    playerImage.setVisible(false);
    playerImage.repaint();
    playerImage.setVisible(true);
    playerImage.repaint();
    playerImage.setVisible(true);
    MovePlayer(p.getX(), p.getY());
    f.repaint();
}

public static void paintplayer(){
    if (pt == Playere.Kaneki){
        playerImage = new JLabel(new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.png")));
        if (playerImage == null){
            System.out.println("[ERROR THIS WAS THE ERROR THE WHOLE TIME!!");
            return;
        }
        ImageIcon imgs = new ImageIcon(StartMenu.class.getResource("/schoo/NewGame/kaneki_walk.png"));
        playerImage.setBounds(p.getX(), p.getY(), imgs.getIconWidth(), imgs.getIconHeight());
        playerImage.setLocation(0, 0);
        playerImage.setVisible(true);
        f.add(playerImage);
        playerImage.setVisible(true);
    }
    if (pt == Playere.Touka){
        playerImage = new JLabel(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(p.getX(), p.getY());
        playerImage.setVisible(true);
        f.add(playerImage);
        playerImage.setVisible(true);
    }
}

public static void turnPlayer(Direction d){
    Graphics g = playerImage.getGraphics();
    Graphics2D g2 = (Graphics2D) g;
    if (d == Direction.LEFT){
        if (p.getDirection() == Direction.LEFT){
            return;
        }
        if (p.getDirection() == Direction.UP){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.RIGHT){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.DOWN){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    if (d == Direction.UP){
        if (p.getDirection() == Direction.UP){
            return;
        }
        if (p.getDirection() == Direction.RIGHT){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.DOWN){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.LEFT){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    if (d == Direction.RIGHT){
        if (p.getDirection() == Direction.RIGHT){
            return;
        }
        if (p.getDirection() == Direction.DOWN){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.LEFT){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.UP){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    if (d == Direction.DOWN){
        if (p.getDirection() == Direction.DOWN){
            return;
        }
        if (p.getDirection() == Direction.LEFT){
            g2.rotate(90.0);
        }
        if (p.getDirection() == Direction.UP){
            g2.rotate(90.0+90.0);
        }
        if (p.getDirection() == Direction.RIGHT){
            g2.rotate(90.0+90.0+90.0);
        }
        p.setDirection(d);
        playerImage.repaint();
    }
    RepaintPlayer();
    f.repaint();
    playerImage.repaint();
}

public static enum Direction{
    LEFT, UP, RIGHT, DOWN;
}
}

感谢您花时间帮助我!

这不是您处理 Swing Graphics 的方式 -- 您不应该在组件上调用 getGraphics(),因为存在很大的风险,即对象为 null 或无法运行。相反,如果这是我的程序,我会使用实际图像,在所有 4 个方向(3 个方向加上原始图像)旋转它,从这些图像中制作 ImageIcons,将它们存储在一个集合中,或者更好的 HashMap<Direction, Icon> ,说叫 playerDirectionIconMap,然后毫无畏惧地使用它们。然后为了获得正确的图标,我会调用类似

// this method should probably not be static
public void turnPlayer(Direction d){
    playerImage.setIcon(playerDirectionIconMap.get(p.getDirection()));
    // .... other code?
}

请注意,turnPlayer(...) 方法不应是静态的,因为这表明需要修复的损坏设计。

另请注意:

  • 旋转方法使用弧度而不是度数,因此使用 Math.PI / 2.
  • 而不是 90.0
  • 您需要使用以中心点为中心的旋转方法,这样您的旋转就不会围绕 0, 0。

可以使用Rotated Icon。 class 为您完成所有旋转逻辑。您只需要指定旋转度数即可。

所以创建标签的基本代码是:

RotatedIcon icon = new RotatedIcon( new ImageIcon(...) );
JLabel player = new JLabel( icon );

然后当你想旋转图标时你可以使用:

icon.setDegrees(...);
player.repaint();