Swing Paint Issues On Secondary Monitor? - java

I recently ran into an issue that I cannot explain. Hopefully someone knows what I can do to resolve my issue. I have a dual monitor machine running Windows 7 64-bit. Both monitors are identical models and settings. The primary monitor is on the right. I am using JRE 1.8.0_131
The issue I am seeing is that recently I discovered I am no longer able to run my Java Swing application on my second monitor. This was working perfectly fine before, as in I could move my application between both monitors, resizing and switching to/from maximized with no problems. Now whenever my application is on my second monitor, all I see is a frame that shows the part of my desktop background that is behind the frame
Below is a small sample program that creates a frame for each monitor. The frame on my primary monitor looks fine. The one on my secondary monitor is messed up like I mentioned. Simply moving the bad frame to my primary monitor does nothing to fix it but if I minimize then restore it looks fine (i.e. if I force the frame to repaint on the primary monitor it looks fine). Similarly, moving the good frame to the secondary monitor and forcing a repaint results in the bad frame
My JRE has not been updated in months so that does not seem like the issue. As an experiment I tried using JDK 1.7.0_17 but it has the same results. My machine got a Windows update yesterday which maybe coincidentally is when I noticed this issue. I am not saying that is the issue, just noting the information I have
Anyhow, any thoughts or suggestions would be greatly appreciated. This issue is annoying since dual monitor support is helpful for having my application and Eclipse visible at the same time, as well as the fact that my application has multiple frames which I placed one per monitor
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
for (final GraphicsDevice device : GraphicsEnvironment
.getLocalGraphicsEnvironment().getScreenDevices())
{
final DisplayMode displayMode = device.getDisplayMode();
LOGGER.debug(
"testSwing: device: {}, ID string: {}, isFullScreenSupported: {}, "
+ "displayModeWidth: {}, displayModeHeight: {}, "
+ "displayModeBitDepth: {}, displayModeRefreshRate: {}", device,
device.getIDstring(), device.isFullScreenSupported(),
displayMode.getWidth(), displayMode.getHeight(), displayMode.getBitDepth(),
displayMode.getRefreshRate());
if (GraphicsDevice.TYPE_RASTER_SCREEN == device.getType())
{
final GraphicsConfiguration configuration =
device.getDefaultConfiguration();
final JFrame frame = new JFrame("Test Frame", configuration);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(
new JLabel(device.getIDstring(), SwingConstants.CENTER));
final Rectangle screenBounds = configuration.getBounds();
frame.setBounds((screenBounds.x + (screenBounds.width / 2)) - 100,
(screenBounds.y + (screenBounds.height / 2)) - 100, 200, 200);
frame.setVisible(true);
}
}
}
});
NOTE: LOGGER is just a SLF4J logger which I added only to see if there was any useful information I could gather. This can be removed when running as it has no effect on the issue described above
Output from running this on my machine:
testSwing: device: Win32GraphicsDevice[screen=0], ID string: \Display0, isFullScreenSupported: true, displayModeWidth: 1920, displayModeHeight: 1200, displayModeBitDepth: 32, displayModeRefreshRate: 59
testSwing: device: Win32GraphicsDevice[screen=1], ID string: \Display1, isFullScreenSupported: true, displayModeWidth: 1920, displayModeHeight: 1200, displayModeBitDepth: 32, displayModeRefreshRate: 59

It is happening because moving your application has contents which is not updating when it is moving to another monitor and still trying to get data from initial monitor.
Try this link. Its about displaying JFrame on multiple monitors. Hope this will help.
how to display JFrame in full screen display mode at a multi-monitor environment?

Related

lwjgl 3 macOS issue with creating windows

I created some code to work with lwjgl3, and ran into an issue with the macOS bindings since I am working on a mac.
The issue is that I cannot seem to get my program to create any windows. I know this for sure as the program just stops at that point. There are no errors.
The line the code gets stuck on is:
long window = glfwCreateWindow(300, 300, "Hello World!", MemoryUtil.NULL, MemoryUtil.NULL);
Since I am developing on IntelliJ, whenever I disconnect the process, it crashes with :
Exception Type: EXC_BAD_ACCESS (SIGABRT)
Exception Codes: EXC_I386_GPFLT
Exception Note: EXC_CORPSE_NOTIFY
I feel that that is not the issue, as I tried running another code dependant on lwjgl3, and it also got stuck at the part creating the window.
I know that the code is stuck at the part to create the window as I have put print statements every line.
I really hope that this is not a compatibility issue with either macOS Big Sur or the mac book pro 2015 model.
while glfwInit() return true, when I run System.out.println(glGetString(GL_VERSION) );, I get this error Fatal error: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
Here is the complete code that I am using for testing:
public static void main(String[] args) throws Exception{
GLFWErrorCallback.createPrint(System.err).set();
// Initialize GLFW. Most GLFW functions will not work before doing this.
if ( !glfwInit() )
throw new IllegalStateException("Unable to initialize GLFW");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
// Configure GLFW
glfwDefaultWindowHints(); // optional, the current window hints are already the default
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Should be true for macOS, according to GLFW docs, to get core profile.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// According to Apple docs, non-core profiles are limited to version 2.1.
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create the window
long window = glfwCreateWindow(300, 300, "Hello World!", MemoryUtil.NULL, MemoryUtil.NULL);
//glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
System.out.println('0');
glfwDestroyWindow(window);
}
I have googled this multiple times:
cannot seem to find a relevant answer. One person suggested that macOS has a hidden popup that had to be closed for the window to be assigned, but I did everything to disable it (as I could not find it) and still nothing happened. I think the GL_VERSION indicates an issue with how openGL is being bound to Java.
[Edit]
Ensured that the app was running on the main thread by passing in the JVM option -XstartOnFirstThread.
[EDIT2]
After leaving the project for a few hours, I came back and reran it.
I added one extra line on the top:
System.out.println("Hello LWJGL " + Version.getVersion() + "!");
The createWindow(...) function started working, which is weird as I do not know why. It also started working if I removed the line, so if anyone knows what was happening please let me know.
I am leaving the question unanswered as I still do not know the reason for the issue, I just happened to be lucky that it fixed itself.
[EDIT3]
I force rebuilt the entire project on intellij and it stopped working again. Am really confused as to why it was working in between
I had the -XstartOnFirstThread on the gradle.properties. Try set it in the jvmargs of the run configuration you currently have. That fixed it for me

java 1.8+ Full-Screen Exclusive Mode setFullScreenWindow(Frame) strange behavior

I'm working on a fullscreen application and discovered some really weird behavior using setFullScreenWindow(Frame) to actually get the application frame to fullscreen exclusive mode on some notebooks. While my code works on most WinOS systems on some it just doesn't. The resolution change works but the application stays in the top left corner of the screen in its former resolution like the setFullScreenWindow(Frame) call did not actually work. No exception thrown. If I connect a second display to the system and make it primary display it suddenly works. If I disconnect the second display and run the app on the previous used native display it also now suddenly works. If I reboot the system the native display again fails to bring the app to fullscreen. Both isFullScreenSupported() and isDisplayChangeSupported() are true in any cases.
Strange. Notebook is a MacBookPro with a Bootcamp WinOS10 installation but problem is not limited to this hardware or WinOsVersion.
client.windowScale = 2;
client.setVisible(false);
client.setResizable(false);
client.setUndecorated(true);
client.setSize(RES_WIDTH * client.windowScale, RES_HEIGHT * client.windowScale);
DisplayMode newDisplayMode;
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode_Old = gd.getDisplayMode();
if (gd.isFullScreenSupported() & gd.isDisplayChangeSupported()) {
gd.setFullScreenWindow(client);
gd.setDisplayMode(
new DisplayMode(
RES_WIDTH * client.windowScale,
RES_HEIGHT * client.windowScale,
DisplayMode_Old.getBitDepth(),
DisplayMode_Old.getRefreshRate()));
}
else {
System.out.println("Fullscreen failed.");
}
}
Actual Image:

Animated GIF in Java Application is throwing MalformedURLException

I want to show an animated gif in my application. I followed the code found here: Show animated GIF
When I run my code I get the MalformedURLException error and my application will not run. Here is what I have that's not working.
The method that calls createVisuals():
private void defaultGUI() {
frame.setTitle("Class Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
frame.setBounds(0, 0, frameWidth, frameWidth/2);
frame.getContentPane().setBackground(Color.WHITE);
frame.setUndecorated(true);
try {
Visuals.createVisuals();
} catch (MalformedURLException e) {
e.printStackTrace();
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
The method that shows the gif:
public class Visuals {
public static void createVisuals() throws MalformedURLException{
URL cwURL = new URL("src\\images\\classmap_colorwheel-gif.gif");
Icon cwGif = new ImageIcon(cwURL);
JLabel cwIcon = new JLabel(cwGif);
GUI.frame.getContentPane().add(cwIcon);
}
What am I not doing correctly?
EDIT:
laksys pointed out that my URL constuction was wrong and gave a reference to fix it. The problem was that I was not giving the full file location alongside adding File: to the begining of the URL.
URL cwURL = new URL("src\\images\\classmap_colorwheel-gif.gif");
URL cwURL = new URL("File:C:/Users/01526460/Desktop/ClassMap/src/images/classmap_colorwheel-gif.gif");
This caused the exception to disappear, however the gif is not running properly. Only one frame of the gif sequence loads while the others only partially load. The gif is also looping faster than it should.
EDIT 2:
I figured out that the gif was not looping properly because of the way I made it, not because of Java. I used Photoshop CS6 to create a frame animation. When I ran the frame animation at 0 second delay between frames it looked fine in Photoshop. However, when the 0 second delay is interpreted through Java, the gif is actually trying to go 0 seconds between frames. If anyone else encounters this problem make sure the delay between your frames is not set to 0. Java does not automatically control the frame rate of gifs (like many browsers do).
I think your url construction is wrong. It may have protocol, host, port etc., please ref this

Minimizing the application in windows problem

I've wrote a simple application to store some text in a derby DB. I have 2 button each one creating a new inputDialog. My problem is that when I run the program on my Ubuntu PC all is well. When I run it on a windows 7 PC when the input dialog is displayed the whole thing is minimized and hidden from the user. So each time I want some input from the user he has to restore the application. And the other problem is that the program doesn't appear in the alt-tab menu too. Here is the code that I use to display the dialog:
String s = (String) JOptionPane.showInputDialog(this, "Моля въведете име:");
All help will be greatly appreciated.
I tried the following code - directly from main() via eclipse running on Windows 7 64-bit. The JFrame remains on display, even if I try otherwise.
JFrame f = new JFrame();
f.setSize(750, 500);
f.show();
JOptionPane.showInputDialog(f, "hello", "there");
System.out.println("hi");
Try this, and if you get the same result then at least we know it's a windows issue that we're dealing with rather than a Java issue.
EDIT:
After looking through your code, I found the offending line. Also as a side note, you should generally call setVisible() after you have done configuring your window. This is especially true with my code, as it would throw an exception if you try to call setUndecorated() after you have displayed the window.
Your Code:
this.setVisible(true); //This should be called after you finish configuration
device.setFullScreenWindow(this); //This is the problem!!!
Instead you should use:
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);
If you want to have your window fullscreen then use:
this.setUndecorated(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setVisible(true);

How to make full screen java applets?

I am designing a psychology experiment with java applets. I have to make my java applets full screen. What is the best way of doing this and how can I do this.
Since I haven't been using java applets for 3 years(The last time I've used it was for a course homework :) ) I have forgotten most of the concepts. I googled and found that link:
Dani web
But in the method described in above link you have to put a JFrame inside the applet which I have no idea how to do it.
Whatever I need a quick and dirty method b'cause I don't have much time and this is the reason why I asked it here.
Thanx in advance
The obvious answer is don't use applets. Write an application that uses a JFrame or JWindow as its top-level container. It's not a huge amount of work to convert an applet into an application. Applets are designed to be embedded in something else, usually a web page.
If you already have an applet and want to make it full screen, there's two quick and dirty hacks:
1). If you know the screen resolution, just set the applet parameters to be that size in the HTML and then run the browser in full screen mode.
2). Run the applet in appletviewer, rather than a web page, and maximise the appletviewer window.
Why not just open a new Frame from the applet (either from the "start()" method or, preferably, after the user presses an "open" button) and set it to be maximized?
JFrame frame = new JFrame();
//more initialization code here
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(dim.width, dim.height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Don't forget: The JFrame should be created and opened from the EDT. Applet start() is not guaranteed to be called on that thread, so use SwingUtilities.invokeLater(). Of course, if you opt for the button route, button listener is called on the EDT, so you should be safe.
I think you want to use WebStart. You can deploy from a browser but it is otherwise a full blown application. There are a few browserish security restrictions, but, as you're using an Applet currently, I think I can assume they're not a problem.
I've found a solution for this problem that works fine. Tested in Linux 64 bits (Chrome and Firefox) and in Windows 7 64 bits (Chrome and Explorer)
The only problem has been that my applet uses all the space in the browser and when the user switch off the full screen mode, the applet is not scaled to the browser size. The solution has been to keep the previous size of the applet before to enter in a fullscreen mode and then, set this size when the applet returns to the normal mode:
public void setFullScreen() {
if (!this.fullscreen) {
size = this.getSize();
if (this.parent == null) {
this.parent = getParent();
}
this.frame = new Frame();
this.frame.setUndecorated(true);
this.frame.add(this);
this.frame.setVisible(true);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] devices = ge.getScreenDevices();
devices[0].setFullScreenWindow(this.frame);
this.fullscreen = true;
} else {
if (this.parent != null) {
this.parent.add(this);
}
if (this.frame != null) {
this.frame.dispose();
}
this.fullscreen = false;
this.setSize(size);
this.revalidate();
}
this.requestFocus();
}

Categories

Resources