-
Notifications
You must be signed in to change notification settings - Fork 27
/
AWTThreadTest.java
105 lines (95 loc) · 3.54 KB
/
AWTThreadTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.lwjgl.opengl.awt;
import org.lwjgl.opengl.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import static org.lwjgl.opengl.GL.*;
import static org.lwjgl.opengl.GL11.*;
import javax.swing.JFrame;
public class AWTThreadTest {
abstract static class AWTGLCanvasExplicitDispose extends AWTGLCanvas {
protected AWTGLCanvasExplicitDispose(GLData data) {
super(data);
}
@Override
public void disposeCanvas() {
}
public void doDisposeCanvas() {
super.disposeCanvas();
}
}
public static void main(String[] args) {
Semaphore signalTerminate = new Semaphore(0);
Semaphore signalTerminated = new Semaphore(0);
JFrame frame = new JFrame("AWT test") {
@Override
public void dispose() {
// request the cleanup
signalTerminate.release();
try {
// wait until the thread is done with the cleanup
signalTerminated.acquire();
} catch (InterruptedException ignored) {
}
super.dispose();
}
};
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(600, 600));
GLData data = new GLData();
data.samples = 4;
data.swapInterval = 0;
AWTGLCanvasExplicitDispose canvas;
frame.add(canvas = new AWTGLCanvasExplicitDispose(data) {
private static final long serialVersionUID = 1L;
@Override
public void initGL() {
System.out.println("OpenGL version: " + effective.majorVersion + "." + effective.minorVersion + " (Profile: " + effective.profile + ")");
createCapabilities();
glClearColor(0.3f, 0.4f, 0.5f, 1);
}
@Override
public void paintGL() {
int w = getFramebufferWidth();
int h = getFramebufferHeight();
float aspect = (float) w / h;
double now = System.currentTimeMillis() * 0.001;
float width = (float) Math.abs(Math.sin(now * 0.3));
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, w, h);
glBegin(GL_QUADS);
glColor3f(0.4f, 0.6f, 0.8f);
glVertex2f(-0.75f * width / aspect, 0.0f);
glVertex2f(0, -0.75f);
glVertex2f(+0.75f * width/ aspect, 0);
glVertex2f(0, +0.75f);
glEnd();
swapBuffers();
}
}, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
Runnable renderLoop = new Runnable() {
@Override
public void run() {
while (true) {
canvas.render();
try {
if (signalTerminate.tryAcquire(10, TimeUnit.MILLISECONDS)) {
GL.setCapabilities(null);
canvas.doDisposeCanvas();
signalTerminated.release();
return;
}
} catch (InterruptedException ignored) {
}
}
}
};
Thread renderThread = new Thread(renderLoop);
renderThread.start();
}
}