我正在尝试使用 AWT 在 Java 中制作棋盘游戏。我想以全屏独占模式运行这个游戏。但是 paint()
方法不起作用。
问题 是我想在全屏框架上加载和绘制图像,但传统的 paint()
方法不允许我这样做。
最佳答案
下面的示例将使您在默认屏幕设备 上使用java.awt.Frame
全屏显示。
- Lesson: Full-Screen Exclusive Mode API (链接到 Oracle)。
public static void main(String[] args) throws IOException {
Frame frame = new Frame("Test");
frame.setUndecorated(true);
frame.add(new Component() {
BufferedImage img = ImageIO.read(new URL("http://upload.wikimedia.org/"+
"wikipedia/en/2/24/Lenna.png"));
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
});
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gs.setFullScreenWindow(frame);
frame.validate();
}
您也可以将上面的示例与 swing 一起使用(只需确保实现 paintComponent(Graphics g)
方法而不是 paint
)。
关于java - 全屏帧图像加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7803468/