我一直在练习用java编写不同风格的游戏开发,在尝试绘制为主菜单创建的菜单按钮时遇到了这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Game.MainMenu.paint(MainMenu.java:90)
at Game.Game.paintBackground(Game.java:131)
at Game.Game.paint(Game.java:125)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)
这是我的 MainMenu.java
package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import aMain.Config;
public class MainMenu implements MouseListener, MouseMotionListener {
boolean mouseOverPlay = false;
boolean imagesLoaded = false;
String ImageURL = "C:/Users/Vince/Desktop/Game Images/MainMenu/";
BufferedImage playOn;
BufferedImage playOff;
int playX = 150,
playY = 80;
BufferedImage tutorial;
int tutorialX,
tutorialY;
public void loadMenu() {
loadButtons();
}
public void loadButtons() {
try {
playOn = ImageIO.read(new File(ImageURL+"PlayOn.png"));
playOn = ImageIO.read(new File(ImageURL+"PlayOff.png"));
imagesLoaded = true;
}catch(IOException e) { System.out.println("error loading images");}
}
Point2D pointClicked;
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if((x > playX && x < playOn.getWidth()) && (y > playY && y < playOn.getWidth())) {
//clicked play
}
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void paint(Graphics g) {
g.setColor(Color.MAGENTA);
g.fillRect(0, 0, Config.FRAME_WIDTH, Config.FRAME_HEIGHT);
if(imagesLoaded) {
g.drawImage(playOff, playX, playY, playOff.getWidth(), playOff.getHeight(), null);
}
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if((x > playX && x < playOff.getWidth()) && (y > playY && y < playOff.getWidth())) {
mouseOverPlay = true;
}
}
}
这是我的 Game.java
package Game;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import aMain.Config;
import Areas.Area;
import Areas.HomeTown;
import Areas.YourHouse;
public class Game extends JPanel {
static int progress = 0;
int mapX, mapY;
static int mapXMax, mapYMax;
Input input;
MainMenu menu;
StateHandler shandler;
static enum Gamestate {
MENU, GAME
}
static Gamestate GameState;
static enum Place {
HOMETOWN, YOURHOUSE
}
static Place location;
public static BufferedImage background;
public Game() {
setPreferredSize(new Dimension(Config.FRAME_WIDTH, Config.FRAME_HEIGHT));
menu = new MainMenu();
input = new Input();
addKeyListener(input);
switchGameState(Gamestate.MENU);
}
public static void switchGameState(Gamestate gamestate) {
switch(gamestate) {
case MENU:
GameState = gamestate;
break;
case GAME:
GameState = gamestate;
break;
}
grabGameState();
}
static boolean game = false;
public static void grabGameState() {
switch(GameState) {
case GAME:
game = true;
break;
}
}
public static void switchPlace(Place place) {
switch(place) {
case HOMETOWN:
location = Place.HOMETOWN;
break;
}
grabPlace();
}
public static void grabPlace() {
switch(location) {
case HOMETOWN:
area[1].load();
break;
case YOURHOUSE:
area[2].load();
}
mapXMax = background.getWidth();
mapYMax = background.getHeight();
}
public void loadGame() {
if(progress == 0) {
switchPlace(Place.HOMETOWN);
}
}
static Area area[] = new Area[3];
{
//area[0] is used for a temp variable in methods
area[1] = new HomeTown();
area[2] = new YourHouse();
}
public void updateGame() {
if(game) {
loadGame();
} else {
menu.loadMenu();
}
}
public void paint(Graphics g) {
super.paint(g);
paintBackground(g);
}
public void paintBackground(Graphics g) {
if(GameState == Gamestate.MENU) {
menu.paint(g);
} else {
g.drawImage(background, 0, 0, mapXMax, mapYMax, null);
}
}
}
我知道那里可能有很多未使用的垃圾,这主要是为了测试目的。我不确定为什么它给了我一个空错误,即使我声明 MainMenu 绘画要等到图像完成加载才能绘画。洋红色背景完美加载,我不确定为什么我的按钮出现错误