// hokey subclass, specific to this application // restricts image to fixed width integers // always pads to draw length 3 // assumes white background for glyphs import java.applet.*; import java.awt.*; import NumberFont; public class FixedNumberFont extends NumberFont { static final int padding = 4; // left/right padding static int stringWidth, glyphWidth, glyphHeight; public FixedNumberFont(Applet a) { super(a); glyphWidth = numbers[0].getWidth(a); glyphHeight = numbers[0].getHeight(a); stringWidth = 3 * 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); s = "000"; } else if (n < 10) s = "00" + Integer.toString(n); else if (n < 100) s = "0" + Integer.toString(n); else s = Integer.toString(n); int kern = super.drawString(g, s, x + padding, y); // draw right padding g.fillRect(kern, y, padding, glyphHeight); return stringWidth; } }