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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Copyright (c) 2007 Erik Tollerud (erik.tollerud@gmail.com) All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* The names of Erik Tollerud, Sun Microsystems, Inc. or the names of
* contributors may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. ERIK TOLLERUD,
* SUN MICROSYSTEMS, INC. ("SUN"), AND SUN'S LICENSORS SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL ERIK
* TOLLERUD, SUN, OR SUN'S LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT
* OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
* PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF ERIK
* TOLLERUD OR SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use
* in the design, construction, operation or maintenance of any nuclear
* facility.
*/
package net.java.joglutils.demos;
import net.java.joglutils.jogltext.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.Animator;
/**
* This Demo application uses the Lighting class and the FontDrawer class to render 3D text into a GLJFrame.
* See the console for keyboard commands and command line option instructions.
* @author Erik J. Tollerud
* @created January 12, 2007
*/
public class FontDrawerDemo {
/**
* Generates {@link net.java.joglutils.GLJFrame}
* Generates a GLJFrame with a FontDrawer demo. Console output describes input.
* @param args Command Line argument order: textDepth xRotspeed yRotspeed zRotspeed filled flatnormal
*
*/
public static void main(String[] args) {
final float[] rotSteps = {0.0f, 0.3f, 0.0f};
System.out.println("Option Command line argument order (first 4 numerical, last 2 boolean)\ntextDepth xRotspeed yRotspeed zRotspeed filled flatnormal");
System.out.println("Keyboard Inputs (case sensitive)\n" +
"r: toggle rotation\n" +
"R: change rotation speeds\n" +
"n: toggle flat normals\n" +
"f: toggle filled text\n" +
"t: edit text\n" +
"</>: decrease/increase text depth\n" +
"F: change font\n" +
"s: resize font");
final String[] argsFin = args;
Font font = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()[5];
final FontDrawer dttf = new FontDrawer(font);
final StringBuffer upperStr = new StringBuffer("0,0");
final StringBuffer lowerStr = new StringBuffer("-1,-1");
GLEventListener listener = new GLEventListener() {
GLU glu;
float xrot,yrot,zrot;
float dpth = 0.2f;
boolean filled = true, fnorm = true;
net.java.joglutils.lighting.Light lt;
net.java.joglutils.lighting.Material mt;
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
public void init(GLAutoDrawable drawable) {
drawable.setGL(new DebugGL(drawable.getGL()));
glu = new GLU();
switch(argsFin.length) {
case 6:
fnorm = Boolean.parseBoolean(argsFin[5]);
case 5:
filled = Boolean.parseBoolean(argsFin[4]);
case 4:
rotSteps[2] = Float.parseFloat(argsFin[3]);
case 3:
rotSteps[1] = Float.parseFloat(argsFin[2]);
case 2:
rotSteps[0] = Float.parseFloat(argsFin[1]);
case 1:
dpth = Float.parseFloat(argsFin[0]);
}
dttf.setDepth(dpth);
dttf.setFill(filled);
dttf.setFlatNormals(fnorm);
xrot = 0;yrot = 0;zrot = 0;
GL gl = drawable.getGL();
lt = new net.java.joglutils.lighting.Light(gl);
//mt = new net.java.joglutils.lighting.Material(gl);
lt.setLightPosition(0,0,1);
lt.enable();
lt.apply();
//mt.apply();
gl.glColorMaterial( GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE ) ;
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glEnable(GL.GL_LIGHTING);
gl.glEnable(GL.GL_NORMALIZE);
gl.glClearColor(0.3f,0.5f,0.2f,0);
}
public void drawAxis(GL gl) {
gl.glDisable(GL.GL_LIGHTING);
gl.glBegin(GL.GL_LINES);
gl.glColor3f(1,0,0);
gl.glVertex3i(0,0,0);
gl.glVertex3i(10,0,0);
gl.glColor3f(0,1,0);
gl.glVertex3i(0,0,0);
gl.glVertex3i(0,10,0);
gl.glColor3f(0,0,1);
gl.glVertex3i(0,0,0);
gl.glVertex3i(0,0,10);
gl.glEnd();
gl.glEnable(GL.GL_LIGHTING);
}
public void display(GLAutoDrawable drawable) {
GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(90,1,0.001,10);
//gl.glFrustum(-1.5f,1.5f,-1.5f,1.5f,1,5);
//gl.glOrtho(-5,5,-5,5,-5,5);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
glu.gluLookAt(0,0,2,0,0,0,0,1,0);
//drawAxis(gl);
gl.glRotatef(xrot,1.0f,0,0);
gl.glRotatef(yrot,0,1.0f,0);
gl.glRotatef(zrot,0,0,1.0f);
xrot+=rotSteps[0];
yrot+=rotSteps[1];
zrot+=rotSteps[2];
drawAxis(gl);
dttf.drawString(upperStr.toString(),glu,gl);
dttf.drawString(lowerStr.toString(),glu,gl,-0.8f,-0.8f,0);
}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
};
final net.java.joglutils.GLJFrame gljf = new net.java.joglutils.GLJFrame("FontDrawerDemo", listener, 600, 600);
gljf.setDefaultCloseOperation(gljf.EXIT_ON_CLOSE);
gljf.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(KeyEvent e) {
switch (e.getKeyChar()) {
case 'f':
FontDrawer fd = dttf;
dttf.setFill(!dttf.isFill());
break;
case 'n':
dttf.setFlatNormals(!dttf.isFlatNormals());
break;
case 'r':
Animator anim = gljf.getAnimator();
if(anim.isAnimating())
anim.stop();
else
anim.start();
break;
case 'R':
//TODO:change rotation speed
String strRes;
strRes = JOptionPane.showInputDialog("X Rotation Speed?",Float.toString(rotSteps[0]));
if (strRes != null) {
rotSteps[0] = Float.parseFloat(strRes);
strRes = JOptionPane.showInputDialog("Y Rotation Speed?",Float.toString(rotSteps[1]));
if (strRes != null) {
rotSteps[1] = Float.parseFloat(strRes);
strRes = JOptionPane.showInputDialog("Z Rotation Speed?",Float.toString(rotSteps[2]));
if (strRes != null)
rotSteps[2] = Float.parseFloat(strRes);
}
}
break;
case '>':
case '.':
float depthStep = dttf.getFont().getSize()/20.0f;
dttf.setDepth(dttf.getDepth() + depthStep);
break;
case '<':
case ',':
depthStep = dttf.getFont().getSize()/20.0f;
dttf.setDepth(dttf.getDepth() - depthStep);
break;
case 't':
case 'T':
String up = JOptionPane.showInputDialog("Upper Text:",upperStr.toString());
upperStr.delete(0,upperStr.length());
upperStr.append(up);
String dn = JOptionPane.showInputDialog("Lower Text:",lowerStr.toString());
lowerStr.delete(0,lowerStr.length());
lowerStr.append(dn);
break;
case 'F':
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
JPanel pan = new JPanel();
JComboBox cb = new JComboBox();
for (Font f : fonts)
cb.addItem(f);
cb.setSelectedItem(dttf.getFont());
pan.add(cb);
net.java.joglutils.JPanelDialog jpd = new net.java.joglutils.JPanelDialog("Choose Font",pan);
if (jpd.showAsModal())
dttf.setFont((Font)cb.getSelectedItem());
break;
case 's':
Font currFont = dttf.getFont();
String resultStr = JOptionPane.showInputDialog("Font Size?",Integer.toString(currFont.getSize()));
float targSize = Float.parseFloat(resultStr);
dttf.setFont(currFont.deriveFont(currFont.getStyle(),targSize));
break;
}
gljf.repaint();
}
});
gljf.generateAnimator();
gljf.setVisible(true);
}
}
|