package glredbook10;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* This program demonstrates some characters of a stroke (vector) font. The
* characters are represented by display lists, which are given numbers which
* correspond to the ASCII values of the characters. Use of glCallLists() is
* demonstrated.
*
* @author Kiet Le (Java port)
*/
import java.awt.event.*;
import javax.swing.*;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
import java.nio.*;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public class stroke
extends JFrame
implements GLEventListener, KeyListener
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
private GLU glu;
private GLCapabilities caps;
private GLCanvas canvas;
//
private static final int PT = 1;
private static final int STROKE = 2;
private static final int END = 3;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// * C struct. Compare this to charpoint class.
// typedef struct charpoint
// {
// GLfloat x, y;
// int type;
// } CP;
private class charpoint
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public float x;
public float y;
public int type;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public charpoint(float x, float y, int type)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.x = x;
this.y = y;
this.type = type;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// * Saved here from original for you to see the difference
// CP Adata[] =
// {
// { 0, 0, PT}, {0, 9, PT}, {1, 10, PT}, {4, 10, PT},
// {5, 9, PT}, {5, 0, STROKE}, {0, 5, PT}, {5, 5, END}
// };
charpoint Adata[] =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ new charpoint(0, 0, PT), new charpoint(0, 9, PT), //
new charpoint(1, 10, PT), new charpoint(4, 10, PT),//
new charpoint(5, 9, PT), new charpoint(5, 0, STROKE),//
new charpoint(0, 5, PT), new charpoint(5, 5, END) };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
charpoint Edata[] =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ new charpoint(5, 0, PT), new charpoint(0, 0, PT), //
new charpoint(0, 10, PT), new charpoint(5, 10, STROKE),//
new charpoint(0, 5, PT), new charpoint(4, 5, END) };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
charpoint Pdata[] =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ new charpoint(0, 0, PT), new charpoint(0, 10, PT),//
new charpoint(4, 10, PT), new charpoint(5, 9, PT),//
new charpoint(5, 6, PT), new charpoint(4, 5, PT), //
new charpoint(0, 5, END) };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
charpoint Rdata[] =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ new charpoint(0, 0, PT), new charpoint(0, 10, PT),//
new charpoint(4, 10, PT), new charpoint(5, 9, PT),//
new charpoint(5, 6, PT), new charpoint(4, 5, PT),//
new charpoint(0, 5, STROKE), new charpoint(3, 5, PT),//
new charpoint(5, 0, END) };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
charpoint Sdata[] =
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ new charpoint(0, 1, PT), new charpoint(1, 0, PT), //
new charpoint(4, 0, PT), new charpoint(5, 1, PT),//
new charpoint(5, 4, PT), new charpoint(4, 5, PT), //
new charpoint(1, 5, PT), new charpoint(0, 6, PT), //
new charpoint(0, 9, PT), new charpoint(1, 10, PT),//
new charpoint(4, 10, PT), new charpoint(5, 9, END) };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// char *test1 = "A SPARE SERAPE APPEARS AS"; C char ptr to str ![](https://www.cnblogs.com/Images/dot.gif)
// char *test2 = "APES PREPARE RARE PEPPERS";
private String test1 = "A SPARE SERAPE APPEARS AS";// String object
private String test2 = "APES PREPARE RARE PEPPERS";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public stroke()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
super("stroke");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
caps = new GLCapabilities();
canvas = new GLCanvas(caps);
canvas.addGLEventListener(this);
canvas.addKeyListener(this);
getContentPane().add(canvas);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void run()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
setSize(440, 120);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
canvas.requestFocusInWindow();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static void main(String[] args)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
new stroke().run();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//* Create a display list for each of 6 characters */
public void init(GLAutoDrawable drawable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GL gl = drawable.getGL();
glu = new GLU();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int base;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
gl.glShadeModel(GL.GL_FLAT);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base = gl.glGenLists(128);
gl.glListBase(base);
gl.glNewList(base + 'A', GL.GL_COMPILE);
drawLetter(gl, Adata);
gl.glEndList();
gl.glNewList(base + 'E', GL.GL_COMPILE);
drawLetter(gl, Edata);
gl.glEndList();
gl.glNewList(base + 'P', GL.GL_COMPILE);
drawLetter(gl, Pdata);
gl.glEndList();
gl.glNewList(base + 'R', GL.GL_COMPILE);
drawLetter(gl, Rdata);
gl.glEndList();
gl.glNewList(base + 'S', GL.GL_COMPILE);
drawLetter(gl, Sdata);
gl.glEndList();
gl.glNewList(base + ' ', GL.GL_COMPILE);
gl.glTranslatef(8.0f, 0.0f, 0.0f);
gl.glEndList();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void display(GLAutoDrawable drawable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GL gl = drawable.getGL();
//
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3f(1.0f, 1.0f, 1.0f);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
gl.glPushMatrix();
gl.glScalef(2.0f, 2.0f, 2.0f);
gl.glTranslatef(10.0f, 30.0f, 0.0f);
this.printStrokedString(gl, test1);
gl.glPopMatrix();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
gl.glPushMatrix();
gl.glScalef(2.0f, 2.0f, 2.0f);
gl.glTranslatef(10.0f, 13.0f, 0.0f);
this.printStrokedString(gl, test2);
gl.glPopMatrix();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
gl.glFlush();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GL gl = drawable.getGL();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluOrtho2D(0, w, 0, h);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
boolean deviceChanged)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* interprets the instructions from the array for that letter and renders the
* letter with line segments.
*/
private void drawLetter(GL gl, charpoint[] l)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int i = 0;
gl.glBegin(GL.GL_LINE_STRIP);
while (i < l.length)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch (l[i].type)
{
case PT:
gl.glVertex2f(l[i].x, l[i].y);
break;
case STROKE:
gl.glVertex2f(l[i].x, l[i].y);
gl.glEnd();
gl.glBegin(GL.GL_LINE_STRIP);
break;
case END:
gl.glVertex2f(l[i].x, l[i].y);
gl.glEnd();
gl.glTranslatef(8.0f, 0.0f, 0.0f);
return;
}
i++;// System.out.println(i+" ");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// private void printStrokedString(char *s)
private void printStrokedString(GL gl, String s)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int len = s.length();
ByteBuffer str = BufferUtil.newByteBuffer(len);
str.put(s.getBytes());
str.rewind();
gl.glCallLists(len, GL.GL_BYTE, str);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void keyTyped(KeyEvent key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void keyPressed(KeyEvent key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch (key.getKeyCode())
{
case KeyEvent.VK_ESCAPE:
System.exit(0);
break;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
default:
break;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void keyReleased(KeyEvent key)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
posted @
2007-04-09 11:39
健忘猪
阅读(
841)
评论()
编辑
收藏
举报