import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.font.*; import java.io.*; import java.text.*; import net.java.games.jogl.*; /** * Description of the Class * *@author Davide Raccagni *@created January 29, 2004 */ public class JOGLTTF extends Frame implements GLEventListener { public final static Dimension PREFERRED_FRAME_SIZE = new Dimension(600, 400); public JOGLTTF() { super("TTF"); GLCapabilities capabilities = new GLCapabilities(); GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(capabilities); canvas.addGLEventListener(this); add(canvas, BorderLayout.CENTER); } public Dimension getPreferredSize() { return PREFERRED_FRAME_SIZE; } public void display(GLDrawable drawable) { long inTime = System.currentTimeMillis(); GL gl = drawable.getGL(); GLU glu = drawable.getGLU(); gl.glClear(GL.GL_COLOR_BUFFER_BIT); drawString(glu, gl); } public void displayChanged(GLDrawable drawable, boolean modeChanged, boolean deviceChanged) { } public void init(GLDrawable drawable) { GL gl = drawable.getGL(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glColor3f(1.0f, 1.0f, 1.0f); } public void reshape(GLDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); GLU glu = drawable.getGLU(); gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluOrtho2D(-getWidth() / 2.0, getWidth() / 2, -getHeight() / 2.0, getHeight() / 2.0); } private class GLUtesselatorCallbackImpl extends GLUtesselatorCallbackAdapter { private GL gl; public GLUtesselatorCallbackImpl(GL gl) { this.gl = gl; } public void begin(int type) { gl.glBegin(type); } public void vertex(java.lang.Object vertexData) { double[] coords = (double[])vertexData; gl.glVertex3dv(coords); } public void end() { gl.glEnd(); } } protected void drawString(GLU glu, GL gl) { try { Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("monotxt_.ttf")).deriveFont(100.0f); GlyphVector gv = font.createGlyphVector( new FontRenderContext(new AffineTransform(), true, true), new StringCharacterIterator("DR")); GeneralPath gp = (GeneralPath)gv.getOutline(); PathIterator pi = gp.getPathIterator(AffineTransform.getScaleInstance(1.0, -1.0), 1.0f); tesselate(glu, gl, pi, pi.getWindingRule(), false); } catch (Exception x) { } } public void draw(GL gl, PathIterator pi) { boolean closed = false; for (; !pi.isDone(); pi.next()) { double[] coords = new double[6]; switch (pi.currentSegment(coords)) { case PathIterator.SEG_MOVETO: if (closed) { gl.glEnd(); closed = false; } gl.glBegin(GL.GL_LINE_STRIP); case PathIterator.SEG_LINETO: gl.glVertex2d(coords[0], coords[1]); break; case PathIterator.SEG_CLOSE: gl.glEnd(); closed = true; break; } } if (!closed) { gl.glEnd(); } } public void tesselate(GLU glu, GL gl, PathIterator pi, int windingRule, boolean justBoundary) { GLUtesselatorCallback aCallback = new GLUtesselatorCallbackImpl(gl); GLUtesselator tess = glu.gluNewTess(); glu.gluTessCallback(tess, GLU.GLU_TESS_BEGIN, aCallback); glu.gluTessCallback(tess, GLU.GLU_TESS_END, aCallback); glu.gluTessCallback(tess, GLU.GLU_TESS_ERROR, aCallback); glu.gluTessCallback(tess, GLU.GLU_TESS_VERTEX, aCallback); glu.gluTessCallback(tess, GLU.GLU_TESS_COMBINE, aCallback); glu.gluTessNormal(tess, 0.0, 0.0, -1.0); switch (windingRule) { case PathIterator.WIND_EVEN_ODD: glu.gluTessProperty(tess, GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_ODD); break; case PathIterator.WIND_NON_ZERO: glu.gluTessProperty(tess, GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_NONZERO); break; } if (justBoundary) { glu.gluTessProperty(tess, GLU.GLU_TESS_BOUNDARY_ONLY, GL.GL_TRUE); } else { glu.gluTessProperty(tess, GLU.GLU_TESS_BOUNDARY_ONLY, GL.GL_FALSE); } glu.gluTessBeginPolygon(tess, (double[])null); for (; !pi.isDone(); pi.next()) { double[] coords = new double[6]; switch (pi.currentSegment(coords)) { case PathIterator.SEG_MOVETO: glu.gluTessBeginContour(tess); break; case PathIterator.SEG_LINETO: glu.gluTessVertex(tess, coords, coords); break; case PathIterator.SEG_CLOSE: glu.gluTessEndContour(tess); break; } } glu.gluTessEndPolygon(tess); } public static void main(String[] args) { Frame f = new JOGLTTF(); f.pack(); f.setVisible(true); } }