我正在做一个小型的爱好游戏,到目前为止我有几个课程。有两个类我遇到了一些问题,下面列出了所讨论的类。
Screen.Java
package geisst.flat;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Screen extends JFrame {
public int x = 100;
public int y = 100;
public Screen() {
this.setSize(400, 400);
this.setTitle("Flat Game");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
addKeyListener(new KeyListen());
}
public void paint(Graphics g) {
BufferedImage mainImage = null;
try {
mainImage = ImageIO.read(new File("res/test.gif"));
} catch(IOException e) {
}
g.drawImage(mainImage, x, y, null);
repaint();
}
}
和KeyListen.Java
package geisst.flat;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyListen extends KeyAdapter {
Screen screen;
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_LEFT) {
screen.x += 3;
}
}
}
KeyListen应该将mainImage的x位置向上移动3个像素,但我收到以下错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at geisst.flat.KeyListen.keyPressed(KeyListen.java:15)
at java.awt.Component.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
你这家伙和gal有什么想法会发生这种情况吗?这可能是非常明显的事情:P
提前致谢,
盖斯特
答案 0 :(得分:2)
您永远不会初始化screen
的价值。
你可以做的是这样的事情:
在KeyListener
班级
public KeyListen (Screen screen) {
this.screen = screen;
}
和Screen
班级:
addKeyListener(new KeyListen(this));
答案 1 :(得分:1)
你永远不会初始化屏幕,因此它是null,但你使用它的属性:
screen.x += 3;
您应该将Screen
对象的引用传递给侦听器,或者调用getSource
:
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_LEFT) {
if (e.getSource() instanceof Screen)
((Screen)e.getSource()).x += 3;
}
}
答案 2 :(得分:1)
1)不好的想法直接绘制到JFrame,放在那里JPanel,JComponent或JLabel
2)对于Swing JComponents是paintComponent而不是paint,paint是绘制到RootPane或GlassPane(从JFrame派生的Component)的正确方法
3)使用KeyBindings而不是KeyListener,因为KeyListener在窗口中没有Focus时不起作用,不好主意将setFocusable设置为ContentPane
4)使用Custom Painting的JComponent或JLabel的Icon
5)KeyBindings here
的示例