diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/console/TextImageButtons.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/console/TextImageButtons.java')
| -rw-r--r-- | NET/worlds/console/TextImageButtons.java | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/NET/worlds/console/TextImageButtons.java b/NET/worlds/console/TextImageButtons.java new file mode 100644 index 0000000..3f9b36b --- /dev/null +++ b/NET/worlds/console/TextImageButtons.java @@ -0,0 +1,77 @@ +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; + } +} |