package gamegui; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.FontMetrics; import java.awt.image.ImageObserver; import java.awt.Image; import java.awt.Color; import java.awt.GraphicsEnvironment; import java.awt.Graphics; import java.awt.event.KeyEvent; 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; 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; } public void handleEvent(final KeyEvent e) { if (32 <= e.getKeyCode() && e.getKeyCode() <= 127) { if (e.getKeyCode() == 127) { if (this.text.length() > 0) { this.text = this.text.substring(0, this.text.length() - 1); } } else { 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); } } public void draw(final Graphics g) { String drawnString = new String(); final FontMetrics metrics = g.getFontMetrics(this.font); final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); final GraphicsDevice device = env.getDefaultScreenDevice(); final GraphicsConfiguration gc = device.getDefaultConfiguration(); final BufferedImage source = gc.createCompatibleImage(this.getWidth(), this.getHeight()); final Graphics2D srcGraphics = source.createGraphics(); 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; } g.setColor(Color.green); g.setFont(this.font); srcGraphics.setColor(Color.green); srcGraphics.setFont(this.font); srcGraphics.drawString(drawnString, -this.textStart + 3, (this.getHeight() + metrics.getHeight()) / 2 - 2); g.drawImage(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 + 5, this.getY() + 5, this.getX() + metrics.stringWidth(drawnString) - this.textStart + 5, this.getY() + this.getHeight() - 5); } g.setColor(Color.red); g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight()); } 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; } public void clear() { this.text = ""; this.textStart = 0; this.selected = false; } public String getText() { return this.text; } public void setText(final String newText) { this.text = new String(newText); } public void setSelected(final boolean isSelected) { this.selected = isSelected; } }