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
| package book;
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D;
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.GLProfile; import javax.media.opengl.awt.GLJPanel; import javax.media.opengl.glu.GLU; import javax.swing.JFrame; import javax.swing.JPanel;
import com.jogamp.opengl.util.FPSAnimator;
public class Simple2DText extends JFrame implements GLEventListener { private GLU glu; private static final double Z_DIST = 7.0; private static Font font; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { Simple2DText frame = new Simple2DText(); frame.add(makeRenderPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }); }
public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); glu.gluLookAt(0,0,Z_DIST, 0,0,0, 0,1,0); }
@Override public void dispose(GLAutoDrawable arg0) { }
public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); glu = new GLU(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glEnable(GL.GL_DEPTH_TEST); drawable.addGLEventListener(this); FPSAnimator animator = new FPSAnimator(60, true); animator.start(); }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); if (height == 0)height = 1; gl.glViewport(x, y, width, height); gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0, (float)width/(float)height, 1, 100); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); } private static JPanel makeRenderPanel() { font = new Font("SansSerif", Font.BOLD, 48); JPanel renderPane = new JPanel() { public void paintComponent(Graphics g){ Graphics2D g2d = (Graphics2D) g; int width = getWidth(); int height = getHeight(); g2d.setPaint( new GradientPaint(0, 0, Color.YELLOW, width, height, Color.BLUE)); g2d.fillRect(0, 0, width, height); g2d.setPaint(Color.BLACK); g2d.setFont(font); g2d.drawString("Hello World", width/4, height/4); } }; renderPane.setLayout(new BorderLayout()); renderPane.setOpaque(false); renderPane.setPreferredSize(new Dimension(400, 300)); GLCapabilities caps = new GLCapabilities(GLProfile.getDefault()); caps.setAlphaBits(8); GLJPanel canvas = new GLJPanel(caps); canvas.setOpaque(false); renderPane.add(canvas, BorderLayout.CENTER); return renderPane; } } |