How to output java full screen properly

末鹿安然 提交于 2019-12-05 00:16:47
ethanfar

Usually, as far as games go, it's preferable to use full screen mode instead of using a maximized window. You can do this in Java by using:

GraphicsEnvironment gfxEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gfxDev = gfxEnv.getDefaultScreenDevice();

Window window = new GameWindow();
gfxDev.setFullScreenWindow(window);

If you still want to use a regular frame and center the content panel, you need to define some of the GridBagLayout constraints. It's impossible to tell which without out seeing the code for the rest of the components on that screen, but consider the following:

  1. GridBagConstraints.fill
  2. GridBagConstraints.anchor
  3. GridBagConstraints.weightx
  4. GridBagConstraints.weighty

And finally, regarding setting the screen to the largest size, it is already addressed here: Java JFrame Size according to screen resolution

I am also having same requirement as you have, below code works for me.

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,d.width,d.height); // i assume you have extended JFrame

try this, hope it works for you as well.

MyFrame mFrame= new MyFrame();
mFrame.setVisible(true);
mFrame.setExtendedState(mFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);

I know this is a terrible answer because I don't have time to write any code. Have you tried creating a listener so you can get the proper maximum size once the window is actually created, and then setting the GridBagConstraints weightx and weighty properties accordingly?

Did you try this code

JFrame frame = new JFrame();
frame.setSize(Toolkit.getDefaultToolkit().getScreenSize());
frame.setVisible(true);

You can get full screen size of any device by "Toolkit.getDefaultToolkit().getScreenSize()" in java. Above code I set frame size to fullscreen.

int height = Toolkit.getDefaultToolkit().getScreenSize().height;
int width = Toolkit.getDefaultToolkit().getScreenSize().width;

You can get hight and width of screen to your code by using above codes. I think this will be a help.

You can easily call

setExtendedState(MAXIMIZED_BOTH); on jframe or

use bellow code to set screen size to any PC.

//size of the screen

 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

//height of the task bar

Insets scnMax = Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration());
    int taskBarSize = scnMax.bottom;

//available size of the screen

 setSize(screenSize.width, screenSize.height - taskBarSize);
    setLocation(screenSize.width - getWidth(), screenSize.height - taskBarSize - getHeight());

if u want you can remove taskbar size to get full screen anyway this is the code and this will help you.

sampopes

Try setting image as a background to you JFrame. So it will adjust with frame size

How to set Jframe Background Image in GroupLayout Java

so even in full screen it will be adjusting..

if you use panel then you can resize according to panel, it shows in full panel size

yourinternalframe.setSize(mainPanel.getSize());
yourinternalframe.show();

this may be not seem as your real need, you may do something according to this

I took a look at the code that you attached for FormTTS.java, what I found out is that your screen was set as using the absolute layout hardcoded to some numbers of pixels. Look at the following code:

Menu.getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

Menu.getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 230, 530, 320));

Your JFrame is not using the GridBagLayout, instead it's using AbsoluteLayout from Netbeans library. So I guess you generated these UI codes with the tools from Netbeans.

And then regarding your picture that does not fill all the screen when maximized: jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/freetts/equations.png")));

Menu.getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 1530, 990));

Same problem here, it's hardcoded to some numbers of pixels.

If you want everything to be centered when you maximized your screen, I think the only way to do is to use the gridbag layout for your JFrame and this requires you to update almost everything in your code. And you will need to fully understand how GridBagLayout works. Here is the place to start.

However if you only want the background image to fill the screen you can follow the steps here to let the picture scaled to fill the size of JLabel: Resize a picture to fit a JLabel

If it still doesn't work, you should also get the size of the screen (from one of the answers here) and then set the prefferedSize of the JLabel with those values in addition of scaling the image.

To add on to @eitanfar's answer, the best way of enabling fullscreen in Java is using the FSEM (FullScreen Exclusive Mode) API.


As he stated, this is achieved by setting the windows as fullscreen on the GraphicsDevice you want the window to appear fullscreen on, usually the default one. Even if your device does not support FSEM (id est isFullscreenSupported() returns false), setting the window as fullscreen will still partially work as the API will emulate fullscreen. The only safety check is to verify whether the GraphicsEnvironment is headless (isHeadless()). If it is, then there are no devices to display to.


The advantage FSEM gives you is that all graphics processing is run on the GPU (the GraphicsDevice is the GPU, not the monitor), therefore making it faster on most systems. In your program's options, you can allow the user to choose to enable or not FSEM so that they can run at optimal performance.


However, the system's repaint events are undefined when in FSEM, you're better off using active rendering, therefore you're better off ignoring repaint (setIgnoreRepaint(true)) and then using a custom thread for drawing.

I am having a similar problem with my application. the nearest I have come is to set all components that reside on top to either component.setOpaque(false), or component.setBackground(new Color(255,255,255,0)). you could also try panel.setVisible(false) for the unused panels. its hard to offer up code with out the entire program but give this a whirl:

Menu.setBackground(new Color(255,255,255,0);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!