JPanels、JFrames 和 Windows,天哪!
简而言之,我正在尝试制作一款全屏游戏。
我尝试使用以下代码:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if(!gs.isFullScreenSupported()) {
System.out.println("full-screen not supported");
}
Frame frame = new Frame(gs.getDefaultConfiguration());
Window win = new Window(frame);
try {
// Enter full-screen mode
gs.setFullScreenWindow(win);
win.validate();
}
问题是我正在一个扩展 JPanel 的类中工作,虽然我有一个 Frame 类型的变量,但该类中没有任何 Window 类型。
我对 JPanel 的理解是它是一种窗口,但我无法将“this”传递到 gs.setFullScreenWindow(Window win)...我应该如何去做呢?
有没有使用 JPanel 调用该方法或类似方法的简单方法?
有没有办法可以从 JPanel 中获取 Window 类型的内容?
-
编辑:以下方法更改 JFrame 的状态并每 10 毫秒调用一次:
public void paintScreen()
{
Graphics g;
try{
g = this.getGraphics(); //get Panel's graphic context
if(g == null)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.add(this);
frame.pack();
frame.setResizable(false);
frame.setTitle("Game Window");
frame.setVisible(true);
}
if((g != null) && (dbImage != null))
{
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync(); //sync the display on some systems
g.dispose();
}
catch (Exception e)
{
if(blockError)
{
blockError = false;
}
else
{
System.out.println("Graphics context error: " + e);
}
}
}
我预计在 if(g==null) 语句(所有的frame.somethingOrOther()s)之后可能会有一些冗余或不必要的调用,任何清理建议将不胜感激...
另外,块错误就是看起来的那样。我忽略了一个错误。该错误仅发生一次,并且当设置忽略错误的第一个实例时,效果很好...对于任何感兴趣的人,如果有人想查看是否可以删除该块,我可以在那里发布附加信息,但我不担心……我以后可能会研究一下。
Simply stated, I am trying to make a game I am working on full-screen.
I have the following code I am trying to use:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if(!gs.isFullScreenSupported()) {
System.out.println("full-screen not supported");
}
Frame frame = new Frame(gs.getDefaultConfiguration());
Window win = new Window(frame);
try {
// Enter full-screen mode
gs.setFullScreenWindow(win);
win.validate();
}
Problem with this is that I am working within a class that extends JPanel, and while I have a variable of type Frame, I have none of type Window within the class.
My understanding of JPanel is that it is a Window of sorts, but I cannot pass 'this' into gs.setFullScreenWindow(Window win)... How should I go about doing this?
Is there any easy way of calling that, or a similar method, using a JPanel?
Is there a way I can get something of type Window from my JPanel?
-
EDIT: The following method changes the state of JFrame and is called every 10ms:
public void paintScreen()
{
Graphics g;
try{
g = this.getGraphics(); //get Panel's graphic context
if(g == null)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.add(this);
frame.pack();
frame.setResizable(false);
frame.setTitle("Game Window");
frame.setVisible(true);
}
if((g != null) && (dbImage != null))
{
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync(); //sync the display on some systems
g.dispose();
}
catch (Exception e)
{
if(blockError)
{
blockError = false;
}
else
{
System.out.println("Graphics context error: " + e);
}
}
}
I anticipate that there may be a few redundancies or unnecessary calls after the if(g==null) statement (all the frame.somethingOrOther()s), any cleanup advice would be appreciated...
Also, the block error is what it seems. I am ignoring an error. The error only occurs once, and this works fine when setup to ignore the first instance of the error... For anyone interested I can post additional info there if anyone wants to see if that block can be removed, but i'm not concerned... I might look into it later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在这个问题上有进展吗?如果您可以用您的预期行为以及代码实际在做什么来更新您的问题,这可能会有所帮助?正如已经指出的,JFrame 是 Window 的子类,因此如果您有 JFrame,则不需要 Window。
无论如何,我有一个可以在全屏模式下运行的 Java 应用程序。尽管屏幕不像您的那样经常重新绘制,但它会定期重新绘制。我执行以下操作来进入全屏:
然后每当我需要更新屏幕时,我只需将新的 JPanel 插入
container
JPanel:不能声称它在技术上是完美的,但它对我来说效果很好。如果伪代码示例不能解决问题,我可以花一些时间整理一个可编译的示例。
Have you made any progress on this problem? It might be helpful if you could update your question with your expected behavior and what the code is actually doing? As was already pointed out, JFrame is a subclass of Window, so if you have a JFrame, you don't need a Window.
For what it's worth, I have a Java app which works in fullscreen mode. Although the screen is not repainted as often as yours, it is repainted regularly. I do the following to enter fullscreen:
Then whenever I need to update the screen, I just plug a new JPanel into the
container
JPanel:Can't claim that it's technically perfect, but it works well for me. If the pseudo-code examples don't cut it, I can spend some time putting together a compilable example.
JPanel 不是 窗口。 JFrame 是。
所以你可以尝试:
那应该可行。
JPanel is not a subclass of Window. JFrame is.
So you could try:
That should work.
我想我已经得到了你需要的东西。
将框架设置为未装饰,因此它
没有任何标题栏并且
东西。
将面板添加到框架中,这样就可以了
看起来只显示了您的面板。
最大化你的框架。所以现在它
看起来应该只有你的
面板无需全屏
和窗户的东西。
<块引用>
frame.setUndecorated(true);
框架.添加(面板); //现在最大化你的
框架。
注意:需要注意的是,只有当你的框架不可显示时才能调用未修饰的API,因此如果它已经显示,那么首先你需要执行setVisible(false)。
EDIT1:如果您想要的只是获取包含面板的窗口,那么您可以执行以下操作:
获取窗口实例后,您可以将其传递到任何您想要的地方。
EDIT2:
Frame
类还扩展了Window
,因此您可以直接执行gs.setFullScreen(frame)
。您不需要为该框架创建一个新窗口。I think I got what you need.
Set the frame undecorated, so it
comes without any title bar and
stuff.
Add your panel to the frame., so it
looks like only your panel is shown.
Maximize your frame. So now it
should look like there's only your
panel taking the full screen without
and window stuff.
Note: Its important to note that the undecorated API can only be called when your frame is undisplayable, so if its already show, then first you need to do setVisible(false).
EDIT1: If all you want is to get the window containing your panel, then you can do this:
Once you get the window instance you can pass it wherever you want.
EDIT2: Also the
Frame
class extendsWindow
so you can directly dogs.setFullScreen(frame)
. You dont need to create a new window for that frame.你为什么会这么想?你读过 API 了吗? JPanel 是从 Window 扩展的吗?
您可以尝试使用 SwingUtilities 类。它有一个返回给定组件的窗口的方法。
Why would you think that? Did you read the API? Does JPanel extend from Window?
You can try using the SwingUtilities class. It has a method that returns the Window for a given component.