源码:public Graphics2D getGraphics() {
Window window = device.getFullScreenWindow();
if (window != null) {
BufferStrategy strategy = window.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}
else {
return null;
}
}
错误信息:Exception in thread "main" java.lang.NullPointerException
at com.snow.meng.graphics.ScreenManager.getGraphics(ScreenManager.java:36)
at com.snow.meng.test.GameCore.gameLoop(GameCore.java:24)
at com.snow.meng.test.GameCore.run(GameCore.java:19)
at com.snow.meng.tilegame.GameManager.main(GameManager.java:9)谢谢各位大虾。
Window window = device.getFullScreenWindow();
if (window != null) {
BufferStrategy strategy = window.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}
else {
return null;
}
}
错误信息:Exception in thread "main" java.lang.NullPointerException
at com.snow.meng.graphics.ScreenManager.getGraphics(ScreenManager.java:36)
at com.snow.meng.test.GameCore.gameLoop(GameCore.java:24)
at com.snow.meng.test.GameCore.run(GameCore.java:19)
at com.snow.meng.tilegame.GameManager.main(GameManager.java:9)谢谢各位大虾。
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.image.BufferStrategy;import javax.swing.JFrame;public class ScreenManager {
private GraphicsDevice device;
public ScreenManager() {
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
} public void setFullScreen(DisplayMode displayMode) {
// TODO Auto-generated method stub
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
frame.setResizable(false); device.setFullScreenWindow(frame);
} public Graphics2D getGraphics() {
Window window = device.getFullScreenWindow();
if (window != null) {
BufferStrategy strategy = window.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}
else {
return null;
}
} public int getWidth() {
Window window = device.getFullScreenWindow();
if (window != null) {
return window.getWidth();
}
else {
return 0;
}
} public int getHeight() {
Window window = device.getFullScreenWindow();
if (window != null) {
return window.getHeight();
}
else {
return 0;
}
}
}
public static void main(String[] args){
ScreenManager sm = new ScreenManager();
sm.setFullScreen(null);
System.out.println(sm.getHeight()+"==="+sm.getWidth());
sm.getGraphics();
}
控制台输出:
800===1280
at com.snow.meng.graphics.ScreenManager.getGraphics(ScreenManager.java:36)
at Test.main(Test.java:9)
768===1024
我的有问题啊。真不明白怎么回事