import java.applet.Applet; import java.awt.*; import go.*; import goText.*; class Hello extends GoInterface { String[] fontList = { "hersheyFonts/cursive", "hersheyFonts/times.r", "hersheyFonts/times.g", "hersheyFonts/times.rb", "hersheyFonts/times.i", "hersheyFonts/times.ib", "hersheyFonts/astrology", "hersheyFonts/futura.m", "hersheyFonts/greek", "hersheyFonts/math.upp", "hersheyFonts/symbolic", "hersheyFonts/gothic.eng", "hersheyFonts/japanese", "hersheyFonts/meteorology", "hersheyFonts/cyrillic", "hersheyFonts/gothic.ger", "hersheyFonts/markers", "hersheyFonts/music", "hersheyFonts/futura.l", "hersheyFonts/gothic.ita", "hersheyFonts/math.low", "hersheyFonts/script" }; final int FONT_NUM = fontList.length; GoText[] text = new GoText[FONT_NUM]; int currentFont; // font currently being displayed // View variables. double eyeX, eyeY, eyeZ; double centerX, centerY, centerZ; double upX, upY, upZ; double radius; boolean perspective; Hello() { // // Create "Hello World" with all the different fonts. // for(int i = 0; i < FONT_NUM; i++) { text[i] = new GoText(0.0, 0.0, 0.0, /* position */ 1.0, 0.0, 0.0, /* path */ 0.0, 1.0, 0.0, /* up */ 1.0, 1.0, /* scale */ GoText.CENTER_CENTER, /* alignment */ new GoHershey(fontList[i]), /* font */ "Hello World"); /* text */ } currentFont = 0; // // Setup modelview matrix. // modelview(); } // // Interface render method. // public void render() { go.clear(Go.IMAGE); text[currentFont].render(go); swap(); go.rotate(2, 1, 1, 1); rerender(); } // // Interface resized method. // public void resized(int width, int height) { projection(); } // // Interface mousePressed method. // public void mousePressed(int x, int y) { nextFont(); modelview(); projection(); } // // Set to next font in fontList. // void nextFont() { if(currentFont == FONT_NUM - 1) { currentFont = 0; } else { currentFont++; } } // // Sets up the modelview matrix. // void modelview() { // // Calculate the bounding box. // go.clear(Go.BOUND_BOX); go.renderMode(Go.BOUND_BOX); go.identity(); text[currentFont].render(go); go.renderMode(Go.IMAGE); GoBoundBox boundBox = go.boundBox(); // // Setup modelview variables. // double eyeMult = 3.0; eyeX = 1.3 * eyeMult; eyeY = -2.4 * eyeMult; eyeZ = 2.0 * eyeMult; centerX = (boundBox.xMin() + boundBox.xMax()) / 2.0; centerY = (boundBox.yMin() + boundBox.yMax()) / 2.0; centerZ = (boundBox.zMin() + boundBox.zMax()) / 2.0; upX = 0.0; upY = 0.0; upZ = 1.0; radius = distance(centerX, centerY, centerZ, boundBox.xMax(), boundBox.yMax(), boundBox.zMax()); perspective = true; // // Setup modelview matrix. // go.lookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ); } // // Sets up the projection matrix. // void projection() { int width = go.width(); int height = go.height(); double dx = eyeX - centerX; double dy = eyeY - centerY; double dz = eyeZ - centerZ; double d = Math.sqrt(dx * dx + dy * dy + dz * dz); double fovy = 2.0 * 180.0/Math.PI * Math.atan(radius / d); double near = d - radius; double far = d + radius; double xUnits, yUnits; if(width >= height) { xUnits = 2 * radius * width / height; yUnits = 2 * radius; } else { xUnits = 2 * radius; yUnits = 2 * radius * height / width; } go.matrixMode(Go.PROJECTION); go.identity(); if(perspective) { if(width >= height) { go.perspective(fovy, xUnits / yUnits, near, far); } else { double radiusModified = radius * yUnits / xUnits; double fovyModified = Math.atan(radiusModified / d) * 180.0 / Math.PI * 2.0; go.perspective(fovyModified, xUnits / yUnits, near, far); } } else { go.ortho(-xUnits / 2.0, xUnits / 2.0, -yUnits / 2.0, yUnits / 2.0, near, far); } go.matrixMode(Go.MODELVIEW); } // // Calculate distance between two points. // double distance(double x0, double y0, double z0, double x1, double y1, double z1) { double dx = x1 - x0; double dy = y1 - y0; double dz = z1 - z0; return Math.sqrt(dx*dx + dy*dy + dz*dz); } } public class hello extends Applet { public void init() { // // Fonts will be found relative to the html document // that includes this applet. // GoFont.path(getDocumentBase()); // // Create Hello World. // setLayout(new GridLayout(1, 1)); add(new Hello()); } }