I have an undecorated window that needs to be centered, using this configuration:
Lwjgl3ApplicationConfiguration configuration = Lwjgl3ApplicationConfiguration()
configuration.setIdleFPS(60)
configuration.setBackBufferConfig(8,8,8,8,16,0,0)
configuration.setWindowedMode(1920,1080)
configuration.setTitle("Title")
configuration.setDecorated(false)
configuration.setResizable(false)
Later, in app, through options you can change the size of the window with presets defined from a specific aspect ratio. The resizing is made with this call:
Gdx.graphics.setWindowedMode(width, height)
This seems to keep the window in its original top left corner position (which can be at a random position on screen), but I want it to be centered on the monitor, or a way to move the window to any desired position at will.
The question:
How can I keep the window created by LibGDX with LWJGL3Application centered when changing window size with SetWindowedMode()
#Tenfour04 stated in response to the old answer below that you can get the LWJGL3Window instance with
Lwjgl3Window window = ((Lwjgl3Graphics)Gdx.graphics).getWindow();
You can then use that to set the position during a resize event for example
window.setWindowPos(x, y)
Old answer:
I solved this by reflection
public void setWindowSize(int width, int height) {
Lwjgl3Application app = (Lwjgl3Application) Gdx.app
Field windowfield = app.class.getDeclaredField("currentWindow")
if(windowfield.trySetAccessible()) {
Lwjgl3Window window = windowfield.get(app)
Gdx.graphics.setWindowedMode(width, height)
// Can use context size because of no decorations on window
window.setWindowPos(Gdx.graphics.width/2 - width/2, Gdx.graphics.height/2 - height/2)
}
}
Warning: Even though this works, this is not a good solution. The field of the class is kept private for a reason and not exposing it to the API means that it can change at any update, leaving you with a mess.
That being said, I'm posting this solution for people as desperate as me and because I'm not sure there's another proper solution yet. I will eagerly await a better solution though.
New to Codename One, moving over from Android "native" java development.
I'm trying to create a SideMenu design that loosely resembles the Material side menu, as seen in just about every Google app (e.g. Play Store). Starting with a header image, I can't seem to get rid of some padding on the top and bottom:
The white bars on the top and bottom are unwanted. Current code:
public void start() {
if (current != null) {
current.show();
return;
}
home = new Home();
// SideMenu header BG image
Image headerImage = theme.getImage("bg_navdrawer_header.png");
ScaleImageLabel sideMenuHeaderBg = new ScaleImageLabel(headerImage);
sideMenuHeaderBg.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
// SideMenu header app title
Label sideMenuHeaderLabel = new Label("Title");
sideMenuHeaderLabel.setUIID("SideMenuHeaderTitle");
Container sideMenuHeader = new Container();
sideMenuHeader.add(LayeredLayout.encloseIn(sideMenuHeaderBg, FlowLayout.encloseBottom(sideMenuHeaderLabel)));
home.getToolbar().addComponentToSideMenu(sideMenuHeader);
home.show();
}
The white bars disappear if I use BACKGROUND_IMAGE_SCALED_FILL, but then it doesn't keep aspect ratio - it stretches to fill those two white spaces. (EDIT: It does keep aspect ratio, but it clips the longer dimension). I've tried setting the UIID to "Container" to... everything, makes no difference.
Any ideas?
UPDATE:
For some reason, calling setWidth on the ScaledImageView immediately after setting the background type solved it. Doesn't matter what the width is, any number at all and the "letterboxing" is gone and the height is as it should be.
ScaleImageLabel sideMenuHeaderBg = new ScaleImageLabel(headerImage);
sideMenuHeaderBg.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FILL);
sideMenuHeaderBg.setWidth(0);
Going to leave this open in case somebody has a better solution or can at least explain why this is the case, or exactly what is happening since my solution is more of a hack than an actual answer.
BACKGROUND_IMAGE_SCALED_FILL should keep aspect ratio. It resizes the image to "fill" the space's shorter dimension. The longer dimension is clipped.
BACKGROUND_IMAGE_SCALED will stretch to fill the space, but won't keep aspect ratio.
BACKGROUND_IMAGE_SCALED_FIT will also keep aspect ratio. It resizes the image to "fill" the space's longer dimension. The shorter dimension is "letterboxed", as you see in your screenshot.
Now what I am doing in my program is that I am using setundecorated = true and MAXIMIZED_BOTH So it makes it go full screen and the display looks very nice, But the problem is that there are images (border) on the left and the right side of my screen and also a blue background. What happens is that in changing screens and resolutions these get disturbed and are not shown properly. Those grey patches come up again
History:
I have a java program which I wanted to always open in full screen; I was not able to find a way to do it properly so I had adjusted the minimum to (1370, 727) and maximum size. Thus, it started opening properly on my laptop, but when I changed my laptop's display to LCD, it started giving problems:
It opens in a smaller window:
If I then click on the maximize button, a grey area comes on the side and bottom (I wanted the items on screen to get stretched or center themselves):
And here for example, there is a grey patch at the bottom. Instead, I want the background to cover the whole screen.
Update 1
If I change to stretchable gridbaglayout, this is the code I used and what happens:
Menu.setExtendedState(MAXIMIZED_BOTH);
GridBagLayout gbl = new GridBagLayout();
Menu.setLayout(gbl);
JButton component = new JButton("1");
gbl.layoutContainer(Menu);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbl.setConstraints(component, gbc);
Menu.add(component);
Menu.pack();
Menu.setVisible(true);
Question
How do I set "this" frame to setExtendedState(MAXIMIZED_BOTH) as I have done to others? (if I do this in main function, I get an error; even if I make a function for this and call it in main I get an error)
How do I get everything to stretch/rearrange themselves according to the extra grey space?
Update 2
My files in this project:
Update 3
This is the current file I am working on "FormTTS.java"
Search for "MAXIMIZED_BOTH" in there and you will find the code I think you will want to check.
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:
GridBagConstraints.fill
GridBagConstraints.anchor
GridBagConstraints.weightx
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.
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);
I have made a program that uses null, border, card and some my custom layouts. The window of the program is set to resizable:false. Now I wan't to make full screen mode for my program but the problem is that GUI looks ugly if the size of the screen isn't the one I set. I could implement some kind of scale factor for all the components but the problem is that I have over 2000 components in over 50 classes.
Is there an option to resize whole swing UI for defined factor? That means that the image rendered as UI and all mouse events aswell would be resized.
You should probably try to rely on your layout managers. Try to figure out specifically which layouts aren't resizing the way you want, and focus on correcting their behavior instead of trying to manage everything from the top down.
Alternatively, you could keep the size ratio by adding black bars to the top and bottom or left and right of the window when it's in full screen mode. You'll probably still have to play with some of the layouts to get them perfect though.
Having 2000 different components sounds like a usability nightmare, but that's a different issue.
Is there any possibilities to scale the contents of the J Frame For Example : if im giving the initial setSize(800,1000) in which i placed the textfields,labels,buttons etc..as per the size 800,1000 the application looks good,suppose im maximizing the window of the Frame, automatically the textfields,labels,buttons are kept in the static way...how to scale it when the maximize is pressed the entire contents are cleanly arranged in good manner..kindly give me solution to solve this issue
if im giving the initial setSize(800,1000) in which i placed the
textfields,labels,buttons etc..as per the size 800,1000 the
application looks good,suppose im maximizing the window of the Frame,
automatically the textfields,labels,buttons are kept in the static
way...
for AbsoluteLayout to have to place the JComponent by using the Insets that came from first container
Is there any possibilities to scale the contents of the J Frame
have to use ComponentListener (notice delayed by Swing Timer, because this Listener firing a new event for every pixels on all directions) for scalling JComponents together with container
this is job only for LayoutManager, don't to supply that by using AbsoluteLayout & Insets & ComponentListener, be sure this code could be longer an more complicated than by using GroupLayout