// hokey subclass, specific to this application // restricts image to fixed width integers // always pads to draw length "digits" // assumes white background for glyphs import java.applet.*; import java.awt.*; import NumberFont; public class FixedNumberFont extends NumberFont { int padding; // left/right padding int stringWidth, glyphWidth, glyphHeight; int digits; public FixedNumberFont(Applet a, String dirName, int d, int pad) { super(a, dirName); digits = d; padding = pad; glyphWidth = numbers[0].getWidth(a); glyphHeight = numbers[0].getHeight(a); stringWidth = digits * glyphWidth + 2 * padding; } public int getWidth() { return stringWidth; } public int getHeight() { return glyphHeight; } public int drawNumber(Graphics g, int n, int x, int y) { // draw left padding g.setColor(Color.white); g.fillRect(x, y, padding, glyphHeight); // create a fixed length string, draw it String s; if (n < 0) { System.out.println("Negative number " + n); n = 0; } s = Integer.toString(n); while (s.length() < digits) s = "0" + s; // make lots of garbage int kern = super.drawString(g, s, x + padding, y); // draw right padding g.fillRect(kern, y, padding, glyphHeight); return stringWidth; } }