在 JAVA 中,使用 setDisplayMode 更改全屏应用程序的分辨率
我是 JAVA 语言的初学者,正在尝试学习如何创建全屏应用程序.
I am a beginner int the JAVA language and is trying to learn how to create full screen applications.
我正在尝试创建一个简单的应用程序,在该应用程序运行时,会显示一个带有蓝色背景的全屏,并在屏幕中央显示一行简单的文本(它从位置 400x300 开始).应用程序的分辨率设置为 800x600.
I am trying to create a simple application where when run, displays a full screen with blue back ground and a simple line of text in the center of the screen( it starts at position 400x300). The resolution of the application is set to 800x600.
我在 MacbookAir 上运行代码,运行 OSX Lion,屏幕分辨率为 1440x900.问题是,尽管得到了我预期的蓝色背景,但文本只出现在屏幕的左上角.当我增加它的位置时,文本的位置将继续向下和向右移动,直到它消失时超过 1440x900.我猜屏幕分辨率仍然设置为 1440x900,而不是 800x600.
I am running the code on a MacbookAir, running OSX Lion, with a screen resolution of 1440x900. The problem is, despite getting a blue background as I had expected, the text only appears on around upper left of the screen. The position of the text would continue to move down and right as I increase it's position until it reaches beyond 1440x900 when it disappears. I am guessing that the screen resolution is still set at 1440x900 as oppose to 800x600.
这是我的主要课程:
import java.awt.*;
import javax.swing.JFrame;
public class bucky extends JFrame {
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
bucky b = new bucky();
b.run(dm);
}
public void run(DisplayMode dm){
setBackground(Color.BLUE); // Setting background color
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
//The Screen variable is going to be a screen object
Screen s = new Screen();
try{
s.setFullScreen(dm, this); //this refers to whatever object we are working on, ie 's'
try{
//Once it is set to full screen, and wait for 5 second
Thread.sleep(5000);
}catch(Exception ex){};
}
finally{
s.restoreScreen();
}
}
public void paint(Graphics g){
g.drawString("Test", 400, 300);
}
}
这里是 Screen 类的构造函数和 Screen 类的方法,它将应用程序设置为全屏
And here is the the constructor for the Screen class and method from Screen class which sets the application to full screen
private GraphicsDevice vc; // Gives an interface to graphics card/video card.
public Screen(){
//env is environment variable, containing all graphics manipulation objects
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
//When we get environment and getDegaultScreen Device, we get access to the entire monitor, not just a window
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){}
}
}
如果有人能指出为什么分辨率设置不正确,将不胜感激.
If anyone could point me out to why the resolution is not being set properly, it would be much appreciated.
推荐答案
来自 setDisplayMode
显示模式必须是返回的显示模式之一getDisplayModes(),有一个例外:传递一个显示模式DisplayMode.REFRESH_RATE_UNKNOWN 刷新率将导致选择可用显示模式列表中匹配的显示模式宽度、高度和位深.
The display mode must be one of the display modes returned by getDisplayModes(), with one exception: passing a display mode with DisplayMode.REFRESH_RATE_UNKNOWN refresh rate will result in selecting a display mode from the list of available display modes with matching width, height and bit depth.
因此,您需要确保将显示模式设置为其中一种.
So you'll need to make sure that you set the display mode to one of those.
相关文章