diff options
Diffstat (limited to 'NET/worlds/console/MultiLineLabel.java')
| -rw-r--r-- | NET/worlds/console/MultiLineLabel.java | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/NET/worlds/console/MultiLineLabel.java b/NET/worlds/console/MultiLineLabel.java new file mode 100644 index 0000000..b3443e8 --- /dev/null +++ b/NET/worlds/console/MultiLineLabel.java @@ -0,0 +1,179 @@ +package NET.worlds.console; + +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.util.StringTokenizer; + +public class MultiLineLabel extends Canvas { + private static final long serialVersionUID = -3840151921412552795L; + public static final int LEFT = 0; + public static final int CENTER = 1; + public static final int RIGHT = 2; + protected String[] lines; + protected int num_lines; + protected int margin_width; + protected int margin_height; + protected int line_height; + protected int line_ascent; + protected int[] line_widths; + protected int max_width; + protected int alignment = 0; + + protected void newLabel(String label) { + StringTokenizer t = new StringTokenizer(label, "\n"); + this.num_lines = t.countTokens(); + this.lines = new String[this.num_lines]; + this.line_widths = new int[this.num_lines]; + + for (int i = 0; i < this.num_lines; i++) { + this.lines[i] = t.nextToken(); + } + } + + protected void measure() { + FontMetrics fm = this.getFontMetrics(this.getFont()); + if (fm != null) { + this.line_height = fm.getHeight(); + this.line_ascent = fm.getAscent(); + this.max_width = 0; + + for (int i = 0; i < this.num_lines; i++) { + this.line_widths[i] = fm.stringWidth(this.lines[i]); + if (this.line_widths[i] > this.max_width) { + this.max_width = this.line_widths[i]; + } + } + } + } + + public MultiLineLabel(String label, int margin_width, int margin_height, int alignment) { + this.newLabel(label); + this.margin_width = margin_width; + this.margin_height = margin_height; + this.alignment = alignment; + } + + public MultiLineLabel(String label, int margin_width, int margin_height) { + this(label, margin_width, margin_height, 0); + } + + public MultiLineLabel(String label, int alignment) { + this(label, 10, 10, alignment); + } + + public MultiLineLabel(String label) { + this(label, 10, 10, 0); + } + + public MultiLineLabel() { + this("", 10, 10, 0); + } + + public void setLabel(String label) { + this.newLabel(label); + this.measure(); + this.repaint(); + } + + public String getLabel() { + if (this.lines.length == 0) { + return ""; + } else { + String s = this.lines[0]; + + for (int i = 1; i < this.lines.length; i++) { + s = s + "\n" + this.lines[i]; + } + + return s; + } + } + + @Override + public void setFont(Font f) { + super.setFont(f); + this.measure(); + this.repaint(); + } + + @Override + public void setForeground(Color c) { + super.setForeground(c); + this.repaint(); + } + + public void setAlignment(int a) { + this.alignment = a; + this.repaint(); + } + + public void setMarginWidth(int mw) { + this.margin_width = mw; + this.repaint(); + } + + public void setMarginHeight(int mh) { + this.margin_height = mh; + this.repaint(); + } + + public int getAlignment() { + return this.alignment; + } + + public int getMarginWidth() { + return this.margin_width; + } + + public int getMarginHeight() { + return this.margin_height; + } + + public int getLines() { + return this.num_lines; + } + + @Override + public void addNotify() { + super.addNotify(); + this.measure(); + } + + @Override + public Dimension preferredSize() { + return new Dimension(this.max_width + 2 * this.margin_width, this.num_lines * this.line_height + 2 * this.margin_height); + } + + @Override + public Dimension minimumSize() { + return this.preferredSize(); + } + + @Override + public void paint(Graphics g) { + Dimension d = this.getSize(); + int y = this.line_ascent + (d.height - this.num_lines * this.line_height) / 2; + + for (int i = 0; i < this.num_lines; y += this.line_height) { + int x; + switch (this.alignment) { + case 0: + x = this.margin_width; + break; + case 1: + default: + x = (d.width - this.line_widths[i]) / 2; + break; + case 2: + x = d.width - this.margin_width - this.line_widths[i]; + } + + g.drawString(this.lines[i], x, y); + i++; + } + } +} |