Center Java splash screen on multi-monitor - java

I'm creating a splash screen for a Java application using the SplashScreen class. The problem is that the position on multi-monitor systems is undefined. The documentation states "the position on multi-monitor systems is not specified - it is platform and implementation dependent". The application is going to be used solely on multi-monitor platforms, and I would like to ensure that it's always centered on one of the screens. Is there any way to achieve this? The application is going to be used on Linux only, so the platform is the same everywhere.

There is a sample Javafx equivalent of Splash screen. However this splash screen is basically a java swing applet that is called from javafx to be displayed to the user and simulates more or less eclipse and netbeans splash screen using progress bar and titles for the loaded contents.
Following is the link at
http://0divides0.wordpress.com/2011/03/17/splash-screen-with-javafx/
You must be able to get the code and seperate out the splash screen code written in java swings and use it for yourself.
This is a custom java swing splash screen.
and hence to center the splash screen it uses the traditional functions
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2 - (labelSize.height / 2));

Then don't use the builtin splash screen from Java 6. Create your own splash screen using your UI toolkit and position the window manually by calculating its size and the size of the available screens.

An other sample code :
// Get the size of the screen
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// Determine the new location of the window
int w = login.getSize().width;
int h = login.getSize().height;
int x = (dim.width-w)/2;
int y = (dim.height-h)/2;
// Move the window
setLocation(x, y);
setVisible(true);

It might not be defined in the context of Java, but it's for sure defined in the context of the display server.
For Linux / X11 the important aspect is whether XInerama is enabled or not. This X extension enables applications to get knowledge of the physical displays that comprise the desktop.
A quick way of verifying that XInerama is enabled is to try to maximize a window on your multi monitor target system. With XInerama enabled it should only maximize to the dimensions of the active monitor, not the entire screen surface.
Splash screens will when XInerama is enabled be centered on the active monitor as the application launches.
If you focus solely on getting your application's splash screen positioned correctly you will quickly get feedback on other multi monitor issues like for instance the task bar stretching across all screens or windows that maximize across all screens.

A different solution to center a frame on a screen:
GraphicsConfiguration c = frm.getGraphicsConfiguration();
Rectangle in = c.getBounds();
int center_x = in.x + in.width / 2;
int center_y = in.y + in.height / 2;
frm.setLocation(center_x - frm.getWidth()/2, center_y - frm.getHeight()/2);
With the accepted solution I had the problem that the frame was displayed at the border of my two screens. i.e. at the center of both screens.

Related

JFrame scaling for high resolution screens

I have a non resizable Jframe of size 1280x800. Of course this size appears bigger and smaller according to the resolution of the screen. (It has a background image). Now, if i try this on lets say a 4k monitor, it would be absolutely impractical because to small. Isn't there a way to scale the JFrame? Or a solution to this problem? What i thought i would do is write bigger jFrames and tell the main class which one to open according to the resolution. I am sure there is a much more elegant way to do that, since i guess it is a problem that many would have come across!
What a nightmare! Please help me!
Thank you
One way you could achieve that is by getting the screen size of the device, and then setting the size of your JFrame accordingly:
Dimension screenDimension = Toolkit.getDefaultToolkit().getScreenSize();
double jFrameWidth = screenDimension.width * 0.7;
double jFrameHeight = screenDimension.height * 0.5;
Or if you just want to maximise the JFrame you can use:
jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
Detect the resolution of the window you're launching on and scale the frame to a percentage of that window. Have a baseline dimension (1280 * 800) so it isn't too-squashed on smaller screens.
As an example I pull the local GraphicsEnvironment, I pull the data from each relevant GraphicsDevice into a data class that I wrote myself, and I use that throughout my project as it gives me all sorts of stuff like buffer strategies, window dimensions, and so on. I do this when the application is loading (using a SplashScreen) which affords me control over the whole process.
That's about as elegant as you can get, I think.
EDIT
Editing in some example pseudocode to give an idea of what I'm getting at. I write primarily in Java, but I'm not doing this in an IDE so it won't necessarily be compile-ready:
public void scaleWindowDimensions(JFrame frame, GraphicsDevice gd) {
Rectangle bounds = gd.getDefaultConfiguration().getBounds();
int screenX = (int) bounds.getWidth();
int screenY = (int) bounds.getHeight();
// substitute this for however you're setting the size of the JFrame; this is simply how I sometimes do it
frame.getContentPane().setPreferredSize(new Dimension(screenX, screenY));
}
This is a very quick example that will set the size of your JFrame to that of the monitor it's running on. You'll need to modify the background image you're using as well. There's a lot more you could do with this but I'm keeping it simple on purpose.

How to make client-side Java application take different width and height depending on the screen resolutions

I have a client-side Java application. When I open the application in different screen resolutions, I want the application window take different widths and heights.
In these resolutions:
1152x864
1280x800
1280x960
1280x1024
the application window should take 944px width & 698px height.
In these resolutions:
800x600
1024x768
the application window should take 720px width & 530px height.
In these resolutions:
1260x600
1280x720
1280x768
1360x768
1366x768
the application window should take 944px width & 530px height.
I want to know how this is calculated so I can use it in different applications I am developing.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();
With the this code you get the resolution of the screen. Then you can make an "if else" clause to apply the right dimension of your window.

Libgdx Window Resizing: Keeping Aspect Ratio

I'm currently making a game using the Libgdx library, and have currently hit a small hurdle.
I've disabled the ability to resize currently, in my Main.java class in the desktop project.
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.resizable = false;
I'm wondering whether there is a simple way to make the window resize whilst keeping the aspect ratio. (Like when you shift+resize)
What are my options? :)
that function does not give you the ability to keep the aspect ratio or even something of that what you think. It does just disables the possebilty to resize the screen at the desktop application.
Please take a look at the libGDX wiki especialy the Scene2D page.
Take a look at the Viewport stuff from the Stage. It's explained how you do keep the aspect ratio with the current libGDX. There are tutorials out there who do explain a different way with the help of a virtual resolution and the screen resize method. It's outdate!
from the wiki
This example also uses a fixed stage size with "black bars" on either
side, this time using glViewport. First the stage size of 800x480 is
scaled to fit the screen size using the Scaling class. The result is
used to configure glViewport, which changes which part of the screen
OpenGL will use. Lastly, setViewport is passed the viewport position
and size. The result is the same as the last example, the stage has
"black bars" as necessary to keep the aspect ratio, but no drawing can
occur outside the viewport.
public void resize (int width, int height) {
Vector2 size = Scaling.fit.apply(800, 480, width, height);
int viewportX = (int)(width - size.x) / 2;
int viewportY = (int)(height - size.y) / 2;
int viewportWidth = (int)size.x;
int viewportHeight = (int)size.y;
Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
stage.setViewport(800, 480, true, viewportX, viewportY, viewportWidth, viewportHeight);
}
regards

Display JFrame Centred on main monitor when using dual screens

I am trying to write some code to center my main application JFrame in the center of the computer screen using Java. To do it I am using the code below, which divides the process into two part, this is just because I use the ScreenHeight and ScreenWidth for scaling purposes elsewhere in the class and they are properties of the class.
This code works, on my laptop and other single screen machines perfectly, but on my main machine, which is dual monitor, it places the screen in the centre of the workspace which puts half the dialogue box (which can be small) on each screen. It's in a method, so that I can call it each time the dialogue boxes size, is changed by the program.
I use the boolean Width value to keep the screen in the same location on the vertical axis, but to center it on the horizontal.
// Finds the size of the screen
private void find_ScreenSize() {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dim = toolkit.getScreenSize();
ScreenHeight = dim.height;
ScreenWidth = dim.width;
}
// Centres the dialogue box within the screen
private void centre_Frame(JFrame Frame, boolean Width) {
find_ScreenSize();
if (!Width) { // if you are not justifying on the X axis
Frame.setLocation(Frame.getLocationOnScreen().x,
((ScreenWidth / 2) - (Frame.getWidth() / 2)));
} else {
Frame.setLocation(((ScreenWidth / 2) - (Frame.getWidth() / 2)),
((ScreenHeight / 2) - (Frame.getHeight() / 2)));
}
}
I would like to be able to center the dialogue box in the center of the main/first screen on any multi screen computers. The dialogue boxes in my application, that I don't control the location of, manage to do what I am trying to do for example my JOptionPane and file open and save dialogues all work perfectly.
I am developing on Linux, but the application is for use on Linux and MS platforms.
Searching for this problem gives me lots of examples of the above but nothing that shows me how to do what I want, any help would be appreciated.
Thanks in advance for any help.
frame.setLocationRelativeTo(null);
should work (doc). At least it does on my multi monitor setup. Note that the window is always displayed on the center of the main monitor, even if the app is launched from the secondary monitor.
You can use GraphicsEnvironment.getCenterPoint to find the center point rather than calculating youselves.
If you want to display in a specific monitor, see Show JFrame in a specific screen in dual monitor configuration

Get screen info in windows 7

In my app I'm using swing and awt. I am setting the size (and position) of the window I'm creating based on toolkit.getScreenSize() and this works fine. But I would like to deal with the size of the windows 7 task bar at the bottom (or wherever) of the screen. I can not seem to find out how to do this. Anyone know how I can get the size if the task bar?
You'll want to look at java.awt.GraphicsEnvironment#getMaximumWindowBounds(), which returns the bounds of the screen area excluding the area covered by "objects in the native windowing system such as task bars and menu bars".
Per Matthew's post I got going down the right path. I was able to determine the windows bounds of the task bar with:
Insets scnMax = getToolkit().getScreenInsets(getGraphicsConfiguration());
taskBarSize = scnMax.bottom;
and then subtracting that value from a getScreenSize() height value to help me position the app window correctly.
Don't have a multi monitor system so I'm not sure how this works there but I will have to test that out.

Categories

Resources