我在从另一个班级获取图像时遇到了一些问题。我以前从未遇到过这个问题。有人可以指出我正确的方向。
package main;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class Main extends JFrame {
public static Character character;
static GraphicsEnvironment graphicsEnvironment;
static GraphicsDevice graphicsDevice;
static DisplayMode displayMode;
private Image i;
public static void main(String[] args) {
displayMode = new DisplayMode(1280, 720, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
Main m = new Main();
m.run();
}
public void run() {
setUndecorated(true);
setResizable(false);
graphicsDevice.setFullScreenWindow(this);
try {
graphicsDevice.setDisplayMode(displayMode);
} catch (Exception e) {
}
}
public void paint(Graphics g) {
g.setColor(Color.cyan);
g.fillRect(0, 0, displayMode.getWidth(), displayMode.getHeight());
i = character.getImage();
g.drawImage(i, 100, 100, this);
}
}
package main;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Character {
private Image i;
public Image getImage() {
i = new ImageIcon(this.getClass().getResource("/raw/images/player1.png")).getImage();
return i;
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.Main.paint(Main.java:52)
它说错误是i = character.getImage();
我在制作小程序时已经做过很多次了,如果我第一次尝试全屏游戏的话