我正在尝试制作一个相当简单的游戏,有一个玩家类在尝试绘制时实例化异常。以下代码是播放器类。它被称为在主游戏类中创建玩家对象。
package space.invaders;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
*
* @author Your Name
* <Tristan, Teacher: Anthony , Last Edited: , Date Created, Program Description>
*/
public class player extends JPanel{
public static int px,py;
public static int rx = 1, ry = 1, width =75, height = 50;
public Image player;
static Rectangle shot = new Rectangle(rx,ry,width,height);
static Graphics g;
String imagePath;
URL playerPath;
// draws the palyer
public void paintPlayer(){
playerPath = getClass().getResource("/graphics/player.png");
try{
player = ImageIO.read(playerPath);
}catch(IOException io){
System.out.print("The player picture didn't load properly");
}
g.drawImage(player, px, py, player.this);
}
// draws the shot
static void paintShot(){
g.drawRect((int) shot.getX(), (int) shot.getY(),(int)shot.getWidth(),(int)shot.getHeight());
}
// moves the shot upwards, though might be unnececcary
static int moveShot (){
for(ry = (int) shot.getY(); ry < 300; ry++){
return ry;
}
return ry;
}
int setX(int x){
return px;
}
int setY(int y){
return py;
}
// I think this can be void since your * reminder to test that *
String setImage(String imagePath){
return imagePath;
}
// player movement control
public static class movement implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
rx += 5;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT){
rx -= 5;
}
else if(e.getKeyCode() == KeyEvent.VK_UP){
ry -= 5;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN){
ry += 5;
}
else if(e.getKeyCode() == KeyEvent.VK_SHIFT){
paintShot();
moveShot();
}
}
@Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}`
这是主要的游戏类,它正在调用和创建入侵者和玩家类(与玩家存在的入侵者问题相同)
package space.invaders;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import space.invaders.player.movement;
/**
*
* @author Your Name
* <Tristan , Teacher: Anthony , Last Edited: , Date Created, Program Description>
*/
public class mainGame extends JFrame{
public player p1 = new player();
public mainGame(){
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
createGUI();
startGame();
drawInvaders(1);
}
});
}
void createGUI(){
JFrame gameWindow = new JFrame("Space Invaders");
JPanel observableWindow = new JPanel();
gameWindow.add(observableWindow);
observableWindow.setBackground(Color.black);
gameWindow.setVisible(true);
gameWindow.setSize(425,300);
gameWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
gameWindow.addKeyListener(new movement());
}
void startGame(){
p1.setImage("/graphics/player.png");
p1.setX(50);
p1.setY(50);
p1.paintPlayer();
}
void drawInvaders(int numInvaders){
for(int i = 0; i < numInvaders; i++){
int randX = (int) (Math.random() * this.getWidth());
int randY = (int) (Math.random() * this.getHeight());
invader a = new invader();
a.setX(randX);
a.setY(randY);
a.newInvader();
}
}
}
这是错误代码
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at space.invaders.player.paintPlayer(player.java:31)
at space.invaders.mainGame.startGame(mainGame.java:48)
at space.invaders.mainGame$1.run(mainGame.java:26)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 12 seconds)