我正在用 Java 制作游戏,目前设置的背景不起作用,但设置的前景有效。这是我的主类代码:
import java.awt.*;
import javax.swing.*;
public class Sam extends JFrame{
public static void main(String[] args) {
DisplayMode dm = new DisplayMode(1920, 1080, 32, 60);
Sam s = new Sam();
s.run(dm);
}
public void run(DisplayMode dm){
this.setBackground(Color.PINK);
this.setForeground(Color.BLACK);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(Exception ex){ex.printStackTrace();}
}finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("THIS IS GUNNA BE AWESOME", 200, 200);
}
}
这是我的 Screen 类的代码:
import java.awt.*;
import javax.swing.*;
public class Screen {
private GraphicsDevice vc;
public Screen(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if(dm != null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
}catch(Exception ex){ex.printStackTrace();}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}