-
Notifications
You must be signed in to change notification settings - Fork 27
/
AWTTest.java
65 lines (60 loc) · 2.19 KB
/
AWTTest.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
package org.lwjgl.opengl.awt;
import org.lwjgl.opengl.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import static org.lwjgl.opengl.GL.*;
import static org.lwjgl.opengl.GL11.*;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class AWTTest {
public static void main(String[] args) {
JFrame frame = new JFrame("AWT test");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(600, 600));
GLData data = new GLData();
AWTGLCanvas canvas;
frame.add(canvas = new AWTGLCanvas(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() {
if (!canvas.isValid()) {
GL.setCapabilities(null);
return;
}
canvas.render();
SwingUtilities.invokeLater(this);
}
};
SwingUtilities.invokeLater(renderLoop);
}
}