Java Swing repaint & threads - java

Context: in my Java Swing app I have a chart (using JFreeChart) and when the user clicks on a datapoint on it, it opens a specific flash animation in a JDialog (flash played using DJNativeSwing).
Problem: when the flash animation starts playing, for some reason the background chart in the main window decides to refresh (calls its paintComponent()) and, as the chart is fairly heavy, this takes ~4 seconds during which the flash animation freezes.
I am thus looking for the most elegant / simplest solution to avoid the flash freeze. So far, I could imagine:
Find out why paintComponent() is called and avoid this
Open the JDialog (or child window without modal behavior) on a different thread
What would be the best approach and, most importantly, how to do it?

Related

Simulate mouse click/scroll/etc on swing without using Robot

I want to simulate basic input for a swing application. The whole thing runs headless and doesn't even have a JFrame. I just render all components from the JPanel using the draw() method on a Image (If you want to know why: The Image gets rendered on minecraft maps where users then should be able to interact). I want to execute click or scroll actions on the panel like it would come from regular user.
I know that I can simulate a Button click using doClick() but this doesn't work for components like checkboxes. I also tried using dispatchEvent() but that didn't fire the animations or on some components even threw a HeadlessException.

How to display LWJGL window and swing window at same time?

I want to do the following:
I've got a LWJGL display, and a running game loop. I did not explicitly create a thread for this. It just runs on the thread it takes. (the thread it is created in?)
Now I want to create a Swing JFrame, but of course this is not that simple.
Because when I tried this, the swing window blocks. It just doesn't update and it won't close, and this behaviour is expected.
I tried to create a new thread and put the JFrame in that new thread, but also this doesn't change the behavior.
I really know swing should be executed in the Event Dispatch Thread, but I don't know how to solve this problem. I also tried a lot with SwingUtils.invokeLater, but actually this has very little to do with the problem.
What can I do to have a LWJGL display and a working JFrame at the same time?
I want that JFrame to display generated BufferedImages to debug. The BufferedImages are used for creating textures. I don't want to output that debugging data to disk, and just want to display the images in a JFrame.

Change undecorated state of swing JFrame with LwjglCanvas inside

I have JFrame, that is setUndecorated(true) and inside it's content pane there is a LwjglCanvas a libgdx application rendering it's things.
I need at some point, do setUndecorated(false). But is seems to be impossible.
the decorated state cannot be changed once JFrame has been displayed, the only way to do it is to either create new JFrame, or dispose the existing one, and pack it back. Both solutions are terrible for me, because both will eventually notify LwjglCanvas to stop, and once stopped it cannot be started again.
I tried overwriting it's stop method, but it still get's messed up somehow.
Why do I need this?
I am trying to make a loading splash screen for libGDX desktop app, with a progress bar in it, that should display progress of my desktop app loading it's resources in opengl context. Splash screen has to be undecorated window, and when all loaded app itself should show, maximized and decorated.
Approaches that do not work:
Use one JFrame, and switch between decorated modes.
Try to move Lwjgl canvas from one JFrame to another
Use 2 separate Lwjgl canvases (cannot be done, because only one Al
context can be inited at a time)
How do I do this?

How to create multiple screens in Java?

I have been playing around with trying to get a menu screen for my game. I have only been able to figure out how to paint a new Screen on top of an existing one; meaning that the actual game is still running behind the title screen. I don't need actual code but just a description of how I would go about this.
One way to display a menu would by by using a JDialog outside of your main application window. Take a look at the How to Make Dialogs tutorial for more information.
Another possibility would be to use JInternalFrame for your game and menu so they can be wrapped in a larger application frame. These are explained nicely in How to Use Internal Frames

Help needed in diagnosing a painting issue on alt-tab in Swing

I have been looking into this for a few hours now and haven't found anything to guide my way. I have a Windows Swing GUI program that is performing some background processing in a SwingWorker. This also uses a progress dialog to let the user know how long the background processing will take.
The original designer of this system decided to disable mouse and keyboard input to the user interface, with the exception of the "Cancel" button on the progress dialog. They did this by using a glasspane that ignores all mouse and keyboard events.
The actual issue is that if the user alt-tabs or a screensaver happens, the user interface behind the glasspane never repaints. The progress dialog repaints, but this is due to the SwingWorker calling repaints periodically to update the progress.
I would like any advice for where to look to next. I haven't been able to find anything on alt-tab repainting in Java. Perhaps the progress dialog is modal by definition, preventing the EDT from repainting? Or perhaps the glasspane prevents repaints of "hidden" components?
Thanks,
Ryan
The article How to Write Window Listeners covers this topic. Using this example, you should see pairs of window events on each alt-tab event, similar to these:
java.awt.event.WindowEvent[WINDOW_LOST_FOCUS,opposite=null,oldState=0,newState=0] on One
java.awt.event.WindowEvent[WINDOW_DEACTIVATED,opposite=null,oldState=0,newState=0] on One
java.awt.event.WindowEvent[WINDOW_ACTIVATED,opposite=null,oldState=0,newState=0] on One
java.awt.event.WindowEvent[WINDOW_GAINED_FOCUS,opposite=null,oldState=0,newState=0] on One
If the glass pane's opaque is true, the repaint manager will not repaint the panel below. It's a performance optimization.

Categories