package gamegui; import java.awt.FontMetrics; import java.awt.image.ImageObserver; import java.awt.Image; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; import utils.Utils; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.awt.Font; public class Textbox extends Member { private String label; private String text; private Font font; private int textStart; private boolean selected; private int cursorState; private int blinkInterval; private long lastCursorChange; private boolean password; protected boolean noBorder; protected BufferedImage source; protected Graphics2D srcGraphics; public Textbox(final String newName, final int newX, final int newY, final int newWidth, final int newHeight, final String newLabel, final Font newFont, final boolean isPass) { super(newName, newX, newY, newWidth, newHeight); this.label = new String(newLabel); this.text = new String(); this.font = newFont; this.textStart = 0; this.selected = false; this.cursorState = 0; this.blinkInterval = 500; this.password = isPass; this.noBorder = false; this.source = Utils.gc.createCompatibleImage(this.getWidth(), this.getHeight()); this.srcGraphics = this.source.createGraphics(); } public void setBorder(final boolean b) { this.noBorder = !b; } public void handleEvent(final KeyEvent e) { if (' ' <= e.getKeyChar() && e.getKeyChar() <= '~') { this.text = String.valueOf(this.text) + Character.toString(e.getKeyChar()); } else if (e.getKeyCode() == 8 && this.text.length() > 0) { this.text = this.text.substring(0, this.text.length() - 1); } } @Override public void draw(final Graphics g) { String drawnString = new String(); final FontMetrics metrics = g.getFontMetrics(this.font); if (this.selected && System.currentTimeMillis() - this.lastCursorChange > this.blinkInterval) { if (this.cursorState == 0) { this.cursorState = 1; } else { this.cursorState = 0; } this.lastCursorChange = System.currentTimeMillis(); } if (this.password) { for (int x = 0; x < this.text.length(); ++x) { drawnString = String.valueOf(drawnString) + "*"; } } else { drawnString = this.text; } if (metrics.stringWidth(drawnString) + 9 > this.getWidth()) { this.textStart = metrics.stringWidth(drawnString) + 9 - this.getWidth(); } else { this.textStart = 0; } this.srcGraphics.setColor(Color.black); this.srcGraphics.fillRect(0, 0, this.source.getWidth(), this.source.getHeight()); g.setColor(Color.green); g.setFont(this.font); this.srcGraphics.setColor(Color.green); this.srcGraphics.setFont(this.font); this.srcGraphics.drawString(drawnString, 5 - this.textStart, (this.getHeight() + metrics.getHeight()) / 2 - 2); g.drawImage(this.source, this.getX(), this.getY(), null); g.drawString(this.label, this.getX() - metrics.stringWidth(this.label) - 10, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2); if (this.selected && this.cursorState == 1) { g.drawLine(this.getX() + metrics.stringWidth(drawnString) - this.textStart + 6, this.getY() + 5, this.getX() + metrics.stringWidth(drawnString) - this.textStart + 6, this.getY() + this.getHeight() - 5); } g.setColor(Color.red); if (!this.noBorder) { g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); } } @Override public boolean isClicked(final int xCoord, final int yCoord) { return xCoord >= this.getX() && this.getX() + this.getWidth() >= xCoord && yCoord >= this.getY() && this.getY() + this.getHeight() >= yCoord; } @Override public void clear() { this.text = ""; this.textStart = 0; this.selected = false; } public int getBlinkInterval() { return this.blinkInterval; } public int getCursorState() { return this.cursorState; } public Font getFont() { return this.font; } public String getLabel() { return this.label; } public long getLastCursorChange() { return this.lastCursorChange; } public boolean isSelected() { return this.selected; } public String getText() { return this.text; } public int getTextStart() { return this.textStart; } public void setText(final String newText) { this.text = newText; } public void setSelected(final boolean isSelected) { this.selected = isSelected; } public void setTextStart(final int textStart) { this.textStart = textStart; } public void setCursorState(final int cursorState) { this.cursorState = cursorState; } public void setLastCursorChange(final long lastCursorChange) { this.lastCursorChange = lastCursorChange; } }