2015-12-05 2 views
0

Je suis la programmation d'un jeu et quand il aller à gauche l'image doit tourner à gauche, mais il est pas donne moi et erreur:Pourquoi mon JLabel (ImageIcon) me donne-t-il une erreur lorsque j'essaie de le faire pivoter?

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

Oui, je sais que cela signifie quelque chose java.lang.NullPointerException est nulle, mais rien devrait nul est de toute façon ici le code sur la ligne 365:

g2.rotate(90.0+90.0+90.0); 

ligne 154 passe juste la méthode:

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(); 
} 

et si vous avez besoin toute la classe:

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; 
} 
} 

Nous vous remercions d'avoir pris le temps de me aider!

Répondre

3

Vous pouvez utiliser le Rotated Icon. Cette classe fait toute la logique de rotation pour vous. Vous avez juste besoin de spécifier les degrés de rotation.

Ainsi, le code de base pour créer l'étiquette serait:

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

Ensuite, lorsque vous voulez faire pivoter l'icône que vous pouvez utiliser:

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

Ne pivote toujours pas. – Rof

1

Ce n'est pas comme ça que vous faites des graphiques Swing - vous ne devriez pas appeler getGraphics() sur un composant car il y a un grand risque que l'objet soit nul ou ne fonctionne pas. Au lieu de cela, si c'était mon programme, j'utiliserais l'image réelle, je la tournerais dans les 4 directions (3 plus l'original), je ferais ImageIcons hors de ces images, les stockerais dans une collection, ou mieux HashMap<Direction, Icon>, disons appelé playerDirectionIconMap , et ensuite les utiliser sans crainte. Ensuite, pour obtenir l'icône appropriée, je voudrais juste appeler quelque chose comme,

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

Notez que la méthode turnPlayer(...) ne doit pas être statique, car cela suggère une conception cassée qui doit être corrigé.

Notez également que:

  • La méthode rotate utilise radians et non degrés, donc au lieu de 90,0, utilisez Math.PI/2.
  • Vous voulez utiliser la méthode rotate qui prend un point central afin que votre rotation est pas proche de 0, 0.
+0

Alors vous me dites au lieu d'ajouter une icône à le cadre ? – Rof

+0

@Rof: Non - vous ajoutez une icône à votre JLabel, ici votre playerImage JLabel, comme vous le faites déjà. –

+0

alors 'setIcon (TheIcon)'? – Rof