From 88312df8323d3b1525d25d525bfdb7f2885d4318 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 28 Jul 2017 22:08:18 +0200 Subject: [PATCH] ActiveRenderLoop (for Swing) now accepts Window (instead of just JFrame) --- src/dorkbox/util/swing/ActiveRenderLoop.java | 8 ++-- src/dorkbox/util/swing/SwingActiveRender.java | 38 +++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/dorkbox/util/swing/ActiveRenderLoop.java b/src/dorkbox/util/swing/ActiveRenderLoop.java index e14d265..e0e3277 100644 --- a/src/dorkbox/util/swing/ActiveRenderLoop.java +++ b/src/dorkbox/util/swing/ActiveRenderLoop.java @@ -17,10 +17,10 @@ package dorkbox.util.swing; import java.awt.Graphics; import java.awt.Toolkit; +import java.awt.Window; import java.awt.image.BufferStrategy; import javax.swing.JComponent; -import javax.swing.JFrame; import dorkbox.util.ActionHandlerLong; import dorkbox.util.Property; @@ -68,15 +68,15 @@ class ActiveRenderLoop implements Runnable { // this needs to be synchronized because we don't want to our frame removed WHILE we are rendering it. synchronized (SwingActiveRender.activeRenders) { for (int i = 0; i < SwingActiveRender.activeRenders.size(); i++) { - JFrame jFrame = SwingActiveRender.activeRenders.get(i); + Window window = SwingActiveRender.activeRenders.get(i); - final BufferStrategy buffer = jFrame.getBufferStrategy(); + final BufferStrategy buffer = window.getBufferStrategy(); // maybe the frame was closed if (buffer != null) { try { graphics = buffer.getDrawGraphics(); - jFrame.paint(graphics); + window.paint(graphics); } catch (Exception e) { e.printStackTrace(); } finally { diff --git a/src/dorkbox/util/swing/SwingActiveRender.java b/src/dorkbox/util/swing/SwingActiveRender.java index 136e78f..43d879c 100644 --- a/src/dorkbox/util/swing/SwingActiveRender.java +++ b/src/dorkbox/util/swing/SwingActiveRender.java @@ -17,6 +17,7 @@ package dorkbox.util.swing; import java.awt.Component; import java.awt.EventQueue; +import java.awt.Window; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; @@ -25,24 +26,23 @@ import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import javax.swing.JComponent; -import javax.swing.JFrame; import dorkbox.util.ActionHandlerLong; /** * Contains all of the appropriate logic to setup and render via "Active" rendering (instead of "Passive" rendering). This permits us to - * render JFrames (and their contents), OFF of the EDT - even though there are other frames/components that are ON the EDT.
Because we + * render Windows (and their contents), OFF of the EDT - even though there are other frames/components that are ON the EDT.
Because we * still want to react to mouse events, etc on the EDT, we do not completely remove the EDT -- we merely allow us to "synchronize" the EDT * object to our thread. It's a little bit hacky, but it works beautifully, and permits MUCH nicer animations.
*

- * It is also important to REMEMBER -- if you add a component to an actively managed JFrame, YOU MUST make sure to call {@link + * It is also important to REMEMBER -- if you add a component to an actively managed Window, YOU MUST make sure to call {@link * JComponent#setIgnoreRepaint(boolean)} otherwise this component will "fight" on the EDT for updates. */ public final class SwingActiveRender { private static Thread activeRenderThread = null; - static final List activeRenders = new ArrayList(); + static final List activeRenders = new ArrayList(); static final List activeRenderEvents = new CopyOnWriteArrayList(); // volatile, so that access triggers thread synchrony, since 1.6. See the Java Language Spec, Chapter 17 @@ -56,25 +56,25 @@ class SwingActiveRender { /** - * Enables the jFrame to to added to an "Active Render" thread, at a target "Frames-per-second". This is to support smooth, swing-based - * animations.
This works by removing this object from EDT updates, and instead manually calls paint(g) on the jFrame, updating it + * Enables the window to to added to an "Active Render" thread, at a target "Frames-per-second". This is to support smooth, swing-based + * animations.
This works by removing this object from EDT updates, and instead manually calls paint(g) on the window, updating it * on our own thread. * - * @param jFrame the jFrame to add to the ActiveRender thread. + * @param window the window to add to the ActiveRender thread. */ public static - void addActiveRender(final JFrame jFrame) { + void addActiveRender(final Window window) { // this should be on the EDT if (!EventQueue.isDispatchThread()) { - throw new RuntimeException("adding a swing JFrame to be actively rendered must be done on the EDT."); + throw new RuntimeException("adding a swing Window to be actively rendered must be done on the EDT."); } // setup double-buffering, so we can properly use Active-Rendering, so the animations will be smooth - jFrame.createBufferStrategy(2); + window.createBufferStrategy(2); - // have to specify ALL children in jFrame to ignore EDT paint requests + // have to specify ALL children in Window to ignore EDT paint requests Deque components = new ArrayDeque(8); - components.add(jFrame); + components.add(window); Component[] c; Component pop; @@ -93,7 +93,7 @@ class SwingActiveRender { } hasActiveRenders = true; - activeRenders.add(jFrame); + activeRenders.add(window); } } @@ -137,14 +137,14 @@ class SwingActiveRender { /** - * Removes a jFrame from the ActiveRender queue. This should happen when the jFrame is closed. + * Removes a window from the ActiveRender queue. This should happen when the window is closed. * - * @param jFrame the jFrame to remove + * @param window the window to remove */ public static - void removeActiveRender(final JFrame jFrame) { + void removeActiveRender(final Window window) { synchronized (activeRenders) { - activeRenders.remove(jFrame); + activeRenders.remove(window); final boolean hadActiveRenders = !activeRenders.isEmpty(); hasActiveRenders = hadActiveRenders; @@ -154,9 +154,9 @@ class SwingActiveRender { } } - // have to specify ALL children in jFrame to obey EDT paint requests + // have to specify ALL children in window to obey EDT paint requests Deque components = new ArrayDeque(8); - components.add(jFrame); + components.add(window); Component[] c; Component pop;