Full screen JFrame doesn't cover screen - java

I have searched for a solution to this but can't seem to find it.
If this has already been answered please link me to the answer. I am creating a JFrame setting it to full screen and changing the display mode.
This all works fine and the display mode is supported by my graphics card (I have already checked) so that is all ok. I am adding a canvas to the JFrame, creating a buffer strategy and using it to draw custom graphics to the screen. The issue is that when the display mode changes and the JFrame enters full screen mode it doesn't cover the full screen. This doesn't make sense as the display mode is 1024 x 768 and the canvas size is also 1024 x 768.
Instead the canvas sits in the top left corner of the screen in the same proportions as when the display mode wasn't changed. Furthermore, the graphics the don't draw properly. Everything works fine when I don't try to change the display mode and set full screen.
Here are the three lines of code that sets everything to full screen.
gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
gd.setFullScreenWindow(this);
gd.setDisplayMode(new DisplayMode(1024,768,32,48));
I am running windows 10 and my normal display mode is 3840x2160. Here is a screenshot of what happens when I run the code.
Although it doesnt look like it the display mode definitely changes as the screen goes black and then the screen becomes smaller and more blured.

Have you tried to maximize the window using setExtendedState? (Like in below example)
import javax.swing.JFrame;
public class MaximizedFrame
{
public static void main(String[] args)
{
JFrame frame = new JFrame("My Frame");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
}

Related

How to make full screen Java app.?

I want to make a Java app. with full screen which would run correctly on all screen resolution.
I tried Toolkit in my JFrame it made my frame full screen but some its components (e.g. JButton, JTextField) were not shown when I run it on 800 x 600 screen size, but properly shown in 1600 x 900 screen size.
How to make full screen Java app. with robust component layout?
may be you used 'null' for your JFrame
setLayout(null).
Please use your required layout for the JFrame
Try it :
JFrame jf = new JFrame();
jf.setExtendedState(JFrame.MAXIMIZED_BOTH);
jf.setUndecorated(true);
jf.setVisible(true);
Try this:
setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
you can try the above one it will resize the whole java app window to the available desktop resolution.

Positioning JFrame

I am trying to position my JFrame to be in the center of the screen when it opens. Is there code that I can put here to make my program start in the center of the screen.
import javax.swing.JFrame;
public class StarFuryTest
{
public static void main(String[] args)
{
StarFury menuFrame = new StarFury();
menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
menuFrame.setSize(600,800);//Set the size of the frame
menuFrame.setVisible(true);
}
}
I am making a paint program.
Also what are the statements that turn off decorations and restricts resizing. What is the statement that enters fullscreen.
First, consider whether you really want to put your window in the center of the screen. No other application does that.
The correct thing to do is use setLocationByPlatform(true), which will allow the desktop to place your window as it sees fit.
If you feel your application is so much more important than all others that it absolutely has to be placed in the center of the screen, you can use setLocationRelativeTo(null), which is guaranteed to center the window.
Regardless of whether you call setLocationByPlatform or setLocationRelativeTo, make sure you do it before you make your window visible.
setLocationRelativeTo(null) will place the window in the center of the screen (http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationRelativeTo%28java.awt.Component%29).

Can you put JFrames inside Full Screen Exclusive Mode?

To be honest, I didn't quite know how to express my question in the title. So if someone has a clearer idea of what i'm trying to ask, then please be so kind as to edit it, for the greater good of mankind.
I have the following code:
public class Main {
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
JFrame frame = new JFrame();
Window window = new Window(frame);
JButton btn = new JButton("Quit");
window.add(btn);
if(gd.isFullScreenSupported())
gd.setFullScreenWindow(window);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(null);
}
});
}
}
What I want to do is make a library system, illustrated in full screen mode. Inside the full screen mode, there should be simple frame windows with text fields in them and so on... No heavy 2D/3D graphics required here...
My problem is, the quit button is resized to fit the entire screen. I've tried to use setBounds, setSize, etc, on the frame, window and even button... But it doesn't seem to let me be able to center the button in the middle.
So my question: Is it possible to make JFrame application windows inside a JFrame application window which is set to full screen exclusive mode? Or is it only possible to use full screen exclusive mode together with 2D/3D methods like drawing?
Note: The only thing I intend to use full screen mode for is to set it to a black background, and have a Jframe window ontop of this black background in the center of the application. It is to give the illusion that the application is actually a system.
Thank you.
P.S. please note that the quit button was merely for my own testing purposes. It won't be part of the actual system, so my question does not revolve around how to resize this particular quit button.
Fullscreen Exclusive Mode is not meant to have Swing components inside. It is meant as a high performance way to draw graphics in Java (and can benefit as much as possible of underlaying hardware if done right).
What you want to do could be done by using an undecorated JDesktopPane and maximize it over the dimensions of the screen. Then proceed with using a JInternalFrame to have a JFrame like window inside that JDesktopPane.
I've included links to the respective API sections, they should get you started.
I think that what you are after is an MDI Application. If that is what you are after you could take a look here.
Your problem is that you do not use layout manager correctly.
Learn about layout managers - the modules that put and size/resize the visual components on screen. Then decide how do you want your button to look like and then decide which layout manager to use. It will do its job.
You know what i also had the exact same problem and the only thing i know from my experience is that you can use jinternal frame and set its property undecorated to true then add your custom title bar according to your requirement.

Java Applet: Create a Frame to choose a screen area

I want to create a applet which alows the user to select an area on the users screen.
The overlay I want to create should be the same as screenr.com uses it.
http://www.screenr.com/record
// Edit
What I want to setup is a applet which allows me to create screenshots from a area.
At the moment I create a new frame with the applet. This frame is transparent and contains a panel which can be dragged and resized. This panel is also transparent but it has borders.
So more or less everything works right.
Taking screenshots works well, uploading them too.
My problem now is, that the user cant click anything on his screen because my frame overlays the whole screen.

Changing The Underlying Background Color Of A Swing Window

As discussed here, when resizing a Swing application in Vista (and Windows 7, which is what I'm using) you get a black background in the right/bottom corner while Swing's repaint catches up to the changes.
Playing with other applications (Windows Explorer (Native), Firefox (C++?), and Eclipse (Java)) I notice that they all have this same problem - contrary to what the folks in the link above say - but they minimize the problem by having a grey fill color, which is far less visually jarring than the black that appears in Swing.
I'm wondering if there's some way to change this so that Swing behaves like these other applications? I tried setting the background color of the JFrame, but to no avail.
Additional Info
Jonas discovered (see their informative answer below) that this is an issue with JFrames, but not AWT Frames - maybe that will help someone figure this out.
I have noticed the same problem. This color is gray at IE, in Opera it's black, and in Eclipse it's gray. It seam to be more visible in Swing, because it seam to be little slower at repainting and the color is as you said, black. This problem is more visible if you use the upper left corner to resize.
I coded an example and tried to understand where this black color is defined. A JFrame has many layers, so I set a different background on every layer.
import java.awt.Color;
import javax.swing.JFrame;
public class BFrame {
public static void main(String[] args) {
new JFrame() {{
super.setBackground(Color.CYAN);
this.getRootPane().setBackground(Color.BLUE);
this.getLayeredPane().setBackground(Color.RED);
this.getContentPane().setBackground(Color.YELLOW);
this.setSize(400,340);
this.setVisible(true);
}};
}
}
But this example didn't help. And maybe the color is set by a superclass to Frame.
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
My teory is that, since Swing paints itself, but uses a native Window, then is the native background painted before the resize, and the background of Swing is painted after the resize. But for native applications, the background is painted before the resize.
UPDATE: I tried with a Frame now, and it's not having the same problem. The background seam to be painted before the resize.
import java.awt.Color;
import java.awt.Frame;
public class B2Frame extends Frame {
public static void main(String[] args) {
new Frame() {{
setBackground(Color.YELLOW);
setSize(400,340);
setVisible(true);
}};
}
}
The frame is responsible for painting its background so you need to make sure you let it do its job.
You demonstrate this by setting:
System.setProperty("sun.awt.noerasebackground", "true");
This will cause the background to always be black on resize expansions. (So don't do it.)
The following worked for me:
(AWT only) Set up double buffering using createBufferStrategy(2) - wrapped in addNotify() otherwise you'll run into exceptions during Frame creation
(Step 1 is only necessary in AWT as Swing is double buffered by default.)
Always (important) call super() in your Frame.paint() implementation
Set the background colour with setBackground() then the background should always be the same colour when expanding the frame
Sample code:
class InnerFrame extends Frame {
public void addNotify() {
super.addNotify();
// Buffer
createBufferStrategy(2);
strategy = getBufferStrategy();
}
public void paint(Graphics g) {
super(g);
//...
}
//...
}
I also noticed this. For me this issue was solved with changing layout manager (I've used Free Form Layout before) and it worked pretty well (system color painting).
But eventually I switched back to FFL. Also some well known apps face this problem (f.e. SKYPE), but I actually don't mind it ...

Categories

Resources