package NET.worlds.console; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Toolkit; class TextImageButtons extends ImageButtons { private static final long serialVersionUID = -3744924750348174006L; private int buttonCount; private int[] xText; private String[] texts; private int[] buttonBottoms; private Font font; private static Font defFont = new Font(Console.message("ButtonFont"), 0, 9); private static final int yBaseline = 4; public static Dimension measure(String text, Font f) { Dimension sz = new Dimension(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f); sz.width = fm.stringWidth(text); sz.height = fm.getHeight(); return sz; } public TextImageButtons(String imageName, int buttonWidth, int[] buttonHeights, int[] textLefts, String[] texts, ImageButtonsCallback handler) { this(imageName, buttonWidth, buttonHeights, textLefts, texts, handler, defFont); } public TextImageButtons(String imageName, int buttonWidth, int[] buttonHeights, int[] textLefts, String[] texts, ImageButtonsCallback handler, Font f) { super(imageName, buttonWidth, buttonHeights, handler); this.xText = textLefts; this.texts = texts; this.buttonCount = buttonHeights.length; this.font = f; this.buttonBottoms = new int[this.buttonCount]; int lastBottom = 0; for (int i = 0; i < this.buttonCount; i++) { lastBottom += buttonHeights[i]; this.buttonBottoms[i] = lastBottom; } } public void setTexts(String[] newTexts) { assert newTexts.length == this.texts.length; this.texts = newTexts; this.repaint(); } public String getText(int i) { return this.texts[i]; } @Override protected Graphics drawButton(Graphics g, int button, int state) { return this.drawButton(g, button, state, Color.white); } protected Graphics drawButton(Graphics g, int button, int state, Color c) { if (button >= 0 && button < this.buttonCount && this.texts[button] != null) { g = super.drawButton(g, button, state); if (g != null || (g = this.getGraphics()) != null) { g.setColor(c); g.setFont(this.font); g.drawString(this.texts[button], this.xText[button], this.buttonBottoms[button] - 4); } } else { g = super.drawButton(g, button, 0); } return g; } }