I have a Java application, and in order to find the size of the screen to use, I do the following, for example:
Toolkit.getDefaultToolkit().getScreenSize().width;
Toolkit.getDefaultToolkit().getScreenSize().height;
In my application, I am painting some items on the screen. And to determine the position, I use these values for width and height (because I want a particular item at the very bottom).
But, I'm finding that it is going off the screen, even though it's position doesn't exceed what the value for the height of my screen is.
Then, I realized I'm on a Mac, and the window that pops up for the Java application starts below the little toolbar menu thing at the top of my Mac (you know, the thing that has the time, the battery, etc). Do I need to take the height of that bar into consideration in my application? If so, is there an easy way to say "if I'm on a mac, then subtract something from the height." Also, what is that value of the height of that Menu Bar?
To get the maximum usable area, minus taskbars and menu bars, use the following:
Rectangle rect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
From the docs:
Returns the maximum bounds for centered Windows. These bounds account
for objects in the native windowing system such as task bars and menu
bars.
Related
I am maintaining some existing Java code so I am not looking to make major changes to the way it is currently done. I removed some items from a dialog, (which is fundamentally an org.eclipse.swt.widgets.Shell) and now the dialog displays much smaller, so small that the title and the menu bar do not display in their entirety. I can set the minimum width to fix this problem, but since the users may have different screen sizes and resolutions, I would prefer not to attempt a one-size-fits-all width.
I set it to the preferred size, using
getShell().computeSize
and
.setSize
and it improved the situation (the menu displays completely), but the dialog title is still being elided.
Is there a way of determining the minimum width of a dialog to display the title completely?
Alternatively, is there a way of getting the title as it's displayed (including ellipsis, if any) or of determining if it's being elided?
IFAIK, there is no way to compute the exact size of a shell so that its title can always be shown unshortened.
While the width of a string can be computed with a graphics context like this:
GC gc = new GC(shell);
Point titleSize = gc.textExtent("title");
gc.dispose();
int titleWidth = titleSize.x;
there is no way to determine a shell's title trim. That is, the space occupied by the close, minimize, and maximize buttons and the system menu (if visible at all).
To get a hint though, it should be possible to use shell.computeTrim(0, 0, titleSize.x, titleSize.y). The returned rectangle's width field should give an approximation of the width needed for the shell.
I watched a video by a guy I think is very good with game development, ForeignGuyMike and in his tutorials he uses a virtual width and height and scales it by 2. Here is a screen shot of the code.
I am starting a brand new game and want to know if there is some type of purpose to this. I want to know the pros and cons of this because I can't seem to find where he explains why and where on the internet someone does this. He then multiplies the virtual width and height by the scale for the size of his window. Any help is extremely appreciated.
First go through this article by Xoppa, it should clear your basics. But just to understand by an example.
Say you have a screen of width height = 1980x1020px. Now in your game, you hardcode your character size be 100px which looks fine. But if same game is started on a screen of 1280x720px that character of 100px height will be too big.
One way is you use relative values always. For example, take height of screen every time game runs and set your character height be SCREEN_HEIGHT/10. Now you have to follow this everywhere, set character tail height be SCREEN_HEIGHT/11.5 and so on. This could be hard to handle once your game starts to grow.
But if you create a screen camera of say 20x11(virtual screen size) and set character height be 1.2f then no matter what the screen size be, your character size will always be relative to the ratio of height. Because you are working independently from pixels. Everything is explained in the above article by the way.
I am using a Java application to display an image on the screen. I also am using an eye-tracker device which records the absolute pixel X,Y locations where the person is looking on the screen.
However, what I need to do is convert these X,Y coordinates from the screen positions into the X,Y locations of the image. In other words, somehow I need to figure out that (just an example) 482, 458 translates to pixel 1,1 (the upper left pixel) of the image.
How can I determine the image's placement on the screen (not relative to anything)?
I saw a few posts about "getComponentLocation" and some other APIs, but in my experimentation with these, they seem to be giving coordinates relative to the window. I have also had problems with that because the 1,1 coordinate that they give is within the window, and there is actually a bar at the top of the window (that has the title and the close and minimize buttons) whose width I do not know, so I cannot easily translate.
Surely there must be a way to get the absolute pixel location on the screen of a component?
If we are talking about Swing/AWT application than class java.awt.Component has method getLocationOnScreen which seemed to do what you want
And yes as #RealSkeptic mentioned in comments to question:
SwingUtilities.html#convertPointFromScreen
will do all this work for you considering components hierarchy
I have been developing a game for Android and I had an idea for catering for the different screen sizes, and I would like feedback on whether the idea is a good one, and how it can be improved, or if it shall be scrapped.
What I have is a 2d size scrolling game, where it is locked it landscape mode. Because screen sizes vary, the width of the screen (usually height, but it is in landscape so I shall refer to it as width) changes. This causes all sorts of problems for me with moving the background because it is basically two identical pictures scrolling, and as one leaves the screen the other begins to replace it, so I must catch the image when it has just left the screen, so it can be reset and ready to scroll again. This means it must reach an int value every time, else it will not be caught and both images would scroll off screen.
My idea, which I have briefly tested is as follows. Truncate the screen size to the nearest 100px. Add 100 to this value and have that as the width for the background images. Then divide this value by 100 and use this as the rate of px per frame it moves. This would mean that the background would move at a similar rate dependent upon screen size (set frame rate of 25FPS). It would move faster on larger screens and slower of smaller screens, but would move at the same speed relative (1% per second or whatever).
Is this idea flawed? I have tested it vaguely on a few screen sizes but the emulator is verry laggy, and I only have a couple of devices to test it on (I am only 18 so I cannot get my hands on many!).
Any feedback appreciated.
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.