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.
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'm trying to have an icon be added and displayed to the system tray using Java. However the icon is always either too small, or its cut off in areas.
Its the second one from left in case you couldn't tell.
What am I doing wrong here? How can I get this icon to be displayed fully? What's the standard icon size to be used for system tray?
Edit: I am using AWT SystemTray and TrayIcon
After you've retrieved the actual image resource from disk, you can resize it to the size you need by creating a "fake" one on-the-fly and taking its width.
I found that this was better than using the setImageAutoSize(true) method, as that method does not scale the image smoothly at all.
BufferedImage trayIconImage = ImageIO.read(getClass().getResource("/path/to/icon.png"));
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));
To display the icon at an optimal size, you will need to manually resize it to the correct size. This correct size can differ between operating systems and preferences, so Java provides a method to acquire the task bar icon dimensions, which are 16x16 in the case of your example image.
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Dimension trayIconSize = tray.getTrayIconSize();
// resize icon image to trayIconSize
// create your tray icon off of the resized image
}
According to TrayIcon.setImageAutoSize(boolean).
Sets the auto-size property. Auto-size determines whether the tray image is automatically sized to fit the space allocated for the image on the tray. By default, the auto-size property is set to false.
If auto-size is false, and the image size doesn't match the tray icon space, the image is painted as-is inside that space — if larger than the allocated space, it will be cropped.
I've ended up combining some of these answers to make the code I'm using.
This is producing a good looking icon in my system tray from a png that starts at 100x100.
It's worth noting that on a retina MacBook the icon looks worse scaled down. So I do a check elsewhere to see if it's running on a mac and don't apply this if it is.
public Image imageForTray(SystemTray theTray){
Image trayImage = Toolkit.getDefaultToolkit().getImage("my100x100icon.png");
Dimension trayIconSize = theTray.getTrayIconSize();
trayImage = trayImage.getScaledInstance(trayIconSize.width, trayIconSize.height, Image.SCALE_SMOOTH);
return trayImage;
}
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,
I'm trying to change the default java icon that appears in taskbar everytime I run my .jar program. I managed to change it with frame.setIconImage(img); but this makes icon way too small, I want it to be as big as other programs icons and have a high quality. Any way I can do that? Thanks.
As you only supplied a single icon, Windows will then scale that icon to whatever size it needs displaying it in the taskbar (could be 16x16, 32x32 or other sizes, depending on the desktop them and size of the taskbar.
If you want to have a "good looking" icon in the task bar you will need to provide a 32x32 pixel version of your icon.
Once you have that you can call setIconImages(List) instead of setIconImage() to define the icons that the operating system can use:
List<Image> icons = new ArrayList<Image>();
icons.add(getImage("someImage16x16.gif"));
icons.add(getImage("someImage32x32.gif"));
window.setIconImages(icons);
Where getImage() is some method returning the proper image icon. Essentially that would be the same steps you already used to define the current icon.
You can also supply a 64x64 and 24x24 icon using this method (just add more icons to the list).
Try looking at this example. It looks like you need to use frame.setIconImage(Toolkit.getDefaultToolkit().getImage("your_image.gif")); line