The touch-screen application I am developing will be used on different screen resolutions. I never had to worry about this before, but now I do. I am wondering how can I design the GUIs so that EVERY object on the GUI resizes proportionally to fit the screen resolution? Can you refer me to a good tutorial page? I am designing the application using the NetBeans and the Swing framework. Thank you
I am wondering how can I design the GUIs so that EVERY object on the GUI resizes proportionally to fit the screen resolution?
not easy job you have to test all possible pixels ratio that exist (by default is possible to change and set various pixels ratio on todays fullHD screen) with lot of fails
this is reason why LayoutManagers exist,
I'd suggest to use NestedLayout
sure there are custom LayoutManagers, for example by using MigLayout (something betweens GridBagLayout and SpringLayout) is possible to put all JComponents to the container from one place, but again I'd be suggest use NestedLayout instead
you have to set (internally) minimum screenSize for displaying contents in the JScrollPane, for example screen with resolutions less than 1024 x 600 will have the content scrollable, otherwise let's this job for LayoutManagers,
this idea required model JFrame ---> JScrollPane ---> JPanel (father panel) in all cases, JScrollPane will protect your GUI against small (and dual too) screen resolutions
you have to look at FontMetrics for Font, because you need in/decrease Font size for various pixel ratios continiously with JComponents size on the screen
same with Icons if exist, prepare that before in some of Graphics SW's, not resize on runtime
my view ---> any Framework based on AWT/Swing isn't advantage for job this nature,
Related
I'm doing this project using Swing.
To add a more welcoming feel to the UI, I'm also adding a few Photoshopped images as the background.Here's where the problem begins...
I want the images to automatically resize themselves once the size of the window is increased or decreased, how can I make this happen ?
One way is to override the paintComponent(...) method of a JPanel to paint the image using the drawImage(....) method.
Another option is use a JLabel with an Icon as the background for the frame. Then you can use the Stretch Icon which will automatically scale based on the space available to the label. This is the most flexible solution since the StretchIcon can be used on any component that supports icons.
You can also check out the Background Panel which allows you to display an image. You can display the image at its actual size, scaled or tiled.
Currently I have JFrame that is always set to fullscreen, JPanel with a resolution that can be changed and a BufferedImage which is drawn onto the JPanel and is a fixed resolution of my game. I feel like this is very inefficient since there can be like three different resolutions, the fullscreen used for the JFrame being a screen resolution, a custom resolution for JPanel set by the user and the game's resolution could be different to both where I would be resizing twice before drawing.
How do modern games like Battlefield allow you to change resolution, they're always fullscreen and they're not just resizing in the window when they resize since I have a dual screen with one a TV and it actually shows me changed resolution due to changing the resolution of the game eventhough my TV can take the max of 1920 x 1080 but it's still fullscreen? All I need is really drawing to pixels, I don't even need adding buttons from swing or anything like that, I'll do that myself. My game is should always be fullscreen. I've been making my game and I'm really comfortable with using JFrame and JPanel but I really want to switch to something else if it's more efficient. What do you think I could do?
you could try this:
Toolkit.getDefaultToolkit().getScreenResolution();
or
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(app);
Also take a look at this post:
In JAVA, changing resolution with setDisplayMode for fullscreen application
Rendering to a BufferedImage of fixed size is somewhat defeating the purpose, unless you explicitly want scaling (e.g. because you can't render the full res fast enough).
All you need to do is resize your back buffer when the frame size changes (and render into it according to its current size, of course).
You can detect the changes easily by using either a WindowListener attached to the JFrame, or a ComponentListener attached to the contained panel. For flexibility, I personally would prefer the ComponentListener on the panel, because that enables you to use the same panel and listener for either windowed or full screen mode. Be cautious though that the listener will be called on the Swing event dispatch thread, so depending on the threading architecture of your game you may need some thread synchronization to communicate the change safely.
The way games like battlefield do it is with 3D world coordinates that are independent of the screen resolution using OpenGL or DirectX. Developers feed world coordinates into a rendering engine that then converts to screen coordinates. This is done with whatever the current screen resolution is set to. You will notice though that the HUD size will change depending on your selection of resolution. This is because the HUD is usually a 2D construct that is tied directly to screen coordinates. If you want to do some 3D stuff I would recommend LWJGL. Your only option with Swing is to look into using AffineTransform to scale your display. This however is inefficient compared to 3D and could be slower than you would desire.
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.
Give that I have written a JPanel with many different components on it, is there a way to apply an overall "dilate" ability on the panel so that everything in it stretches proportionally when I resize my window?
That is, if I manually resize my window to be 1/4 the size, everything in the panel should also shrink by 1/4 so the new panel is just a dilation of the first. Given that I have not designed the individual components inside to do this (there are many) is there any easy way to make the panel behave this way?
UPDATE: In order to be more clear on the solution I need, I will describe the panel contents:
The panel is a "game" of sorts, with a single null-layout and dozens of ImageIcons flying around the screen at any time. The ImageIcons are preloaded PNG files, which already have a permanent size. Of course, I could manually resize each ImageIcon and reposition them relative to window size, but that would involve recoding many components.
There are no buttons or text to worry about, so what I'm really looking for is some kind of "postprocessed" resize where the panel simply shrinks whatever's rendered by some porportion (think of resizing an image in Photoshop).
One option is of course to give up swing all together and use some 3rd party widget component library which draws itself using any Graphics. Then you can either draw the widgets on the image and resize the image, or, better yet, apply a transform to the graphics object you pass to the library.
If you do want to stick with swing there is the SwingUtilities.paintComponent method, which you could use to paint the Panel onto a BufferedImage which you could then resize. (I've used this myself to do some nice transitions between "views" in a game.)
The problem is of course that you somehow need to translate the user input accordingly. I have no solution for this right now, but the above perhaps helps you in some way.
You can try to override paintChildren() method of the panel and scale graphics to achieve desired visible size.
You could try J(X)Layer, see http://www.pbjar.org/blogs/jxlayer/jxlayer40/
Using layout managers instead of absolute positioning of the widgets will give you this behaviour. See the oracle tutorials: Using Layout Managers.
Do you really want fonts to resize on resize events? I don't know a layout manager which will do that for you.
I have a java game that I have made, is it a Canvas in a JFrame to run as an application and an applet.
When running as an applet, I want it so that when the user resizes the window, the game scales to fit the new size window.
Is there anyway of resizing the canvas and all the images or shapes that I have drawn on it to fit the new size window, or do I have to go though and change the code on all my objects and the positions they draw?
Any help would be very useful :)
You would have to add scaling to your painting. A transformation applied to your graphics might be the easiest way. You would determine the current size of the Canvas and scale based on the standard size.