Hi. Could someone help me out w/ this.
I am trying an extremely small test for drawing
GlyphVector of a char/string, but i getting just a line.
There was another thread from this forum on glyph
and tessellator and buffered image, but that’s a bit advanced,
and i just wanted drawing from points.
i just wanted a dumb down example here:
/**
* Draws some text in using GlyphVector.
*
* @author Kiet Le
*/
import java.awt.*;
import java.awt.event.*;
import java.awt.font.GlyphVector;
import java.awt.geom.PathIterator;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.nio.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
public class jfont
extends JFrame
implements GLEventListener, KeyListener
{
private GLU glu;
private GLUT glut;
private GLCapabilities caps;
private GLCanvas canvas;
private int base;
//
public jfont()
{
super(jfont.class.getSimpleName());
//
caps = new GLCapabilities();
canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
canvas.addKeyListener(this);
//
add(canvas);
}
public void run()
{
setSize(500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
canvas.requestFocusInWindow();
}
public static void main(String[] args)
{
new jfont().run();
}
public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
glu = new GLU();
glut = new GLUT();
//
gl.glShadeModel(GL.GL_FLAT);
gl.glEnable(gl.GL_POLYGON_SMOOTH);
}
public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
//
float white[] =
{ 1.0f, 1.0f, 1.0f };
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3fv(white, 0);
printString(gl, "O");
gl.glFlush();
}
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
{
GL gl = drawable.getGL();
//
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho((float)-w, (float) w, (float) -h, (float) h, -1.0, 1.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged)
{
}
private void printString(GL gl, String s)
{
//
Font font = getFont();
System.out.println(font.toString());
Graphics2D g2 = (Graphics2D) getGraphics();
FontMetrics fontInfo = g2.getFontMetrics(font);
GlyphVector gv = font.createGlyphVector(g2.getFontRenderContext(), s);
Shape shape = gv.getOutline();
System.out.println(gv.toString());
PathIterator itor = shape.getPathIterator(null, 1);
gl.glScaled(10, 10, 0);
gl.glPushMatrix();
gl.glBegin(GL.GL_LINE_STRIP);
float tmp[] = new float[6];
int it = 0;
while (!itor.isDone())
{
System.out.print(it + " ");
// tmp holds six values
switch (itor.currentSegment(tmp)) {
case PathIterator.SEG_MOVETO:// 1
gl.glVertex2f(tmp[0], tmp[1]);
System.out.println("-> " + tmp[0] + " " + tmp[1]);
break;
case PathIterator.SEG_CUBICTO:// 3
case PathIterator.SEG_QUADTO:// 2
//break;
case PathIterator.SEG_LINETO:
// gl.glColor3d(Math.random(), Math.random(), 0f);
gl.glVertex2f(tmp[0], tmp[1]);
System.out.println("-- " + tmp[0] + " " + tmp[1]);
gl.glEnd();
gl.glBegin(GL.GL_LINE_STRIP);
break;
case PathIterator.SEG_CLOSE:// 0
gl.glColor3f(1, 0, 0);
gl.glVertex2f(tmp[0], tmp[1]);
System.out.println(". " + tmp[0] + " " + tmp[1]);
gl.glEnd();
//gl.glTranslatef(3, 0, 0);
break;
default:
System.out.println("uh-oh" + itor.currentSegment(tmp));
break;
}// sw
itor.next();
it++;
}// wh
gl.glPopMatrix();
}
public void keyTyped(KeyEvent key)
{
}
public void keyPressed(KeyEvent key)
{
switch (key.getKeyCode()) {
case KeyEvent.VK_ESCAPE:
System.exit(0);
break;
default:
break;
}
}
public void keyReleased(KeyEvent key)
{
}
}