我正在编写一个游戏,当他向左移动时,图像应向左旋转,但不是它给了我并且错误:
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)
是的,我知道java.lang.NullPointerException意味着某些东西是null但是 应该 无论如何都是null这里是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();
}
如果你需要全班:
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;
}
}
感谢您抽出宝贵时间帮助我!
答案 0 :(得分:3)
您可以使用Rotated Icon。这个类为你做所有的旋转逻辑。你只需要指定旋转度。
所以创建标签的基本代码是:
RotatedIcon icon = new RotatedIcon( new ImageIcon(...) );
JLabel player = new JLabel( icon );
然后,当您想要旋转图标时,您可以使用:
icon.setDegrees(...);
player.repaint();
答案 1 :(得分:1)
这不是你如何做Swing Graphics - 你不应该在一个组件上调用// Check that interrupts are disabled. If this assertion
// fails, DO NOT be tempted to fix it by inserting a "cli" in
// the interrupt path.
assert(!(read_eflags() & FL_IF));
,因为该对象很可能是空的或无效的。相反,如果这是我的程序,我将获得实际使用的图像,在所有4个方向上旋转(3加上原始图像),从这些图像中制作ImageIcons,将它们存储在集合中,或者更好地getGraphics()
,称为playerDirectionIconMap,然后毫不畏惧地使用它们。然后为了获得正确的图标,我只需要调用类似的东西,
HashMap<Direction, Icon>
请注意,// this method should probably not be static
public void turnPlayer(Direction d){
playerImage.setIcon(playerDirectionIconMap.get(p.getDirection()));
// .... other code?
}
方法不应该是静态的,因为这表明需要修复的设计损坏。
另请注意:
turnPlayer(...)
代替90.0。