Cleaned up sync issues with Swing Active Rendering

This commit is contained in:
nathan 2017-02-16 00:00:55 +01:00
parent d041655c6d
commit 897c4a83cb
2 changed files with 21 additions and 18 deletions

View File

@ -65,26 +65,28 @@ class ActiveRenderLoop implements Runnable {
actionHandlerLong.handle(updateDeltaNanos);
}
for (int i = 0; i < SwingActiveRender.activeRenders.size(); i++) {
JFrame jFrame = SwingActiveRender.activeRenders.get(i);
// 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);
final BufferStrategy buffer = jFrame.getBufferStrategy();
final BufferStrategy buffer = jFrame.getBufferStrategy();
// maybe the frame was closed
if (buffer != null) {
try {
graphics = buffer.getDrawGraphics();
jFrame.paint(graphics);
} catch (IllegalStateException ignored) {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (graphics != null) {
graphics.dispose();
// maybe the frame was closed
if (buffer != null) {
try {
graphics = buffer.getDrawGraphics();
jFrame.paint(graphics);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (graphics != null) {
graphics.dispose();
// blit the back buffer to the screen
if (!buffer.contentsLost()) {
buffer.show();
// blit the back buffer to the screen
if (!buffer.contentsLost()) {
buffer.show();
}
}
}
}

View File

@ -18,6 +18,7 @@ package dorkbox.util.swing;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
@ -41,7 +42,7 @@ public final
class SwingActiveRender {
private static Thread activeRenderThread = null;
static final List<JFrame> activeRenders = new CopyOnWriteArrayList<JFrame>();
static final List<JFrame> activeRenders = new ArrayList<JFrame>();
static final List<ActionHandlerLong> activeRenderEvents = new CopyOnWriteArrayList<ActionHandlerLong>();
// volatile, so that access triggers thread synchrony, since 1.6. See the Java Language Spec, Chapter 17