JOGL – OpenGL Example

Bitmap Fonts

 

Two Steps:

1.     Set Position

gl.glRasterPos2i(x,y);

 

2.     Draw Text – using GLUT

glut.glutBitmapString(gl, Font Type, “text string”);

 

Defined Font Styles

Defined Constant

Value

BITMAP_8_BY_13

3

BITMAP_9_BY_15

2

BITMAP_HELVETICA_10

6

BITMAP_HELVETICA_12

7

BITMAP_HELVETICA_18

8

BITMAP_TIMES_ROMAN_10

4

BITMAP_TIMES_ROMAN_24

5

 

Can specify font by either defined constant or numerical value:

 

glut.glutBitmapString(gl, BITMAP_HELVETICA_12, “Hello World”);

glut.glutBitmapString(gl, 7, “Hello World”);

 

 

Example:

 

 

 

import java.awt.*;

import java.awt.event.*;

 

import net.java.games.jogl.*;

import net.java.games.jogl.util.*;

 

public class BitMapFont

{

  public static void main(String[] args)

  {

    Frame frame = new Frame("BitMap Fonts");

    GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

    canvas.addGLEventListener(new Renderer());

    frame.add(canvas);

    frame.setSize(400, 300);

   

    frame.addWindowListener(new WindowAdapter()

    {

      public void windowClosing(WindowEvent e)

      {

       

        System.exit(0);

      }

    });

    frame.show();

    canvas.requestFocus();

   

  }

 

  static class Renderer implements GLEventListener, KeyListener

  {

   

       

    public void display(GLDrawable gLDrawable)

    {

      String [] fonts = { "BitMap 9 by 15", "BitMap 8 by 13",

            "Times Roman 10 Point ", "Times Roman 24 Point ",

            "Helvetica 10 Point ","Helvetica 12 Point ","Helvetica 18 Point "};

           

      final GL gl = gLDrawable.getGL();

      final GLUT glut = new GLUT();

 

      gl.glClear (GL.GL_COLOR_BUFFER_BIT);  // Set display window to color.

      gl.glColor3f (0.0f, 0.0f, 0.0f);  // Set text e.color to black

      gl.glMatrixMode (GL.GL_MODELVIEW);

      gl.glLoadIdentity();

     

      int x = 20, y=15;

      for (int i=0; i<7;i++){

           

            gl.glRasterPos2i(x,y); // set position

     

            glut.glutBitmapString(gl, i+2, fonts[i]);

      

             y+= 20;

      }

    

    }

   

   

       public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged)

    {

    }

   

    public void init(GLDrawable gLDrawable)

    {

      final GL gl = gLDrawable.getGL();

      final GLU glu = gLDrawable.getGLU();

           

      gl.glMatrixMode (GL.GL_PROJECTION); 

      gl.glClearColor (1.0f, 1.0f, 1.0f, 0.0f);   //set background to white

      glu.gluOrtho2D (0.0, 200.0, 0.0, 150.0);  // define drawing area

      gLDrawable.addKeyListener(this);

    }

   

       public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height)

    {

     

           

    }

 

   

    public void keyPressed(KeyEvent e)

    {

      if (e.getKeyCode() == KeyEvent.VK_ESCAPE)

        System.exit(0);

    }

      

    public void keyReleased(KeyEvent e) {}

   

    public void keyTyped(KeyEvent e) {}

      }

 

 

}