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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import java.awt.Dimension;
import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.awt.GLJPanel; import javax.media.opengl.awt.GLCanvas; import javax.media.opengl.fixedfunc.GLMatrixFunc; import javax.swing.JFrame;
class Ex1_2 { public static GLJPanel getCanvas() { GLCapabilities capabilities = new GLCapabilities(null); capabilities.setDoubleBuffered(true); GLJPanel canvas = new GLJPanel(capabilities); canvas.addGLEventListener(new Ex1Listener()); return canvas; } private Ex1_2() { } }
class Ex1Listener implements GLEventListener { private GL2 gl; public void display(final GLAutoDrawable drawable) { if (gl == null) { gl = drawable.getGL().getGL2(); } gl.glClear(GL.GL_COLOR_BUFFER_BIT); gl.glColor3f(1.0f, 1.0f, 1.0f); gl.glBegin(GL2.GL_POLYGON); { gl.glVertex2i(5, 5); gl.glVertex2i(195, 5); gl.glVertex2i(195, 45); gl.glVertex2i(5, 45); } gl.glEnd(); gl.glColor3f(0.75f, 1.0f, 0.375f); gl.glBegin(GL2.GL_POLYGON); { gl.glVertex2i(195, 45); gl.glVertex2i(267, 45); gl.glVertex2i(267, 95); gl.glVertex2i(195, 95); } gl.glEnd(); gl.glColor3f(0.95f, 0.15f, 0.25f); gl.glBegin(GL2.GL_POLYGON); { gl.glVertex2i(267, 95); gl.glVertex2i(395, 95); gl.glVertex2i(395, 145); gl.glVertex2i(267, 145); } gl.glEnd(); gl.glFlush(); } public void displayChanged(final GLAutoDrawable drawable, final boolean modeChanged, final boolean deviceChanged) { } public void dispose(final GLAutoDrawable drawable) { } public void init(final GLAutoDrawable drawable) { if (gl == null) { gl = drawable.getGL().getGL2(); } gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION); gl.glLoadIdentity(); gl.glOrtho(0.0, 400.0, 0.0, 200.0, -1.0, 1.0); } public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { } }
public final class RedBookMain { public static void main(final String[] args) {
JFrame frame = new JFrame(); frame.addWindowListener(new WindowHandler(frame)); frame.setPreferredSize(new Dimension(400, 200)); frame.getContentPane().add(Ex1_2.getCanvas());
frame.pack(); frame.setVisible(true); } private RedBookMain() { } } |