您好,我正在尝试创建全屏游戏的开头,但遇到了问题。我想要一个带有白色文本的蓝色背景,并将整个屏幕更改为 800*600。问题是我得到一个中间有一个 800*600 框的屏幕(不可见,但我可以通过鼠标边界判断)并且我的背景是黑色的。
我的代码:
import java.awt.*;
import javax.swing.JFrame;
/**
* Write a description of class Full here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Full extends JFrame
{
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800,600,16, DisplayMode.REFRESH_RATE_UNKNOWN);
Full full = new Full();
full.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.BLUE);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN,24));
Screen s = new Screen();
try{
s.setFullScreen(dm,this);
try{
Thread.sleep(15000);
}catch(Exception ex){}
}finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
if(g instanceof Graphics2D){
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
g.drawString("This is gonna be awesome!!",200,200);
}
}
public class Screen
{
private GraphicsDevice vc;
Window myWindow;
public Screen(){
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame myWindow){
myWindow.setUndecorated(true);
myWindow.setResizable(false);
vc.setFullScreenWindow(myWindow);
if(dm != null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
}catch(Exception ex){}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
}