source: galactic-heroes/gamegui/Textbox.java

Last change on this file was 7d9c033, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 6 years ago

Initial commit, code decompiled from a jar

  • Property mode set to 100644
File size: 4.5 KB
Line 
1package gamegui;
2
3import java.awt.Graphics2D;
4import java.awt.image.BufferedImage;
5import java.awt.GraphicsConfiguration;
6import java.awt.GraphicsDevice;
7import java.awt.FontMetrics;
8import java.awt.image.ImageObserver;
9import java.awt.Image;
10import java.awt.Color;
11import java.awt.GraphicsEnvironment;
12import java.awt.Graphics;
13import java.awt.event.KeyEvent;
14import java.awt.Font;
15
16public class Textbox extends Member {
17 private String label;
18 private String text;
19 private Font font;
20 private int textStart;
21 private boolean selected;
22 private int cursorState;
23 private int blinkInterval;
24 private long lastCursorChange;
25 private boolean password;
26
27 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) {
28 super(newName, newX, newY, newWidth, newHeight);
29 this.label = new String(newLabel);
30 this.text = new String();
31 this.font = newFont;
32 this.textStart = 0;
33 this.selected = false;
34 this.cursorState = 0;
35 this.blinkInterval = 500;
36 this.password = isPass;
37 }
38
39 public void handleEvent(final KeyEvent e) {
40 if (32 <= e.getKeyCode() && e.getKeyCode() <= 127) {
41 if (e.getKeyCode() == 127) {
42 if (this.text.length() > 0) {
43 this.text = this.text.substring(0, this.text.length() - 1);
44 }
45 } else {
46 this.text = String.valueOf(this.text) + Character.toString(e.getKeyChar());
47 }
48 } else if (e.getKeyCode() == 8 && this.text.length() > 0) {
49 this.text = this.text.substring(0, this.text.length() - 1);
50 }
51 }
52
53 public void draw(final Graphics g) {
54 String drawnString = new String();
55 final FontMetrics metrics = g.getFontMetrics(this.font);
56 final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
57 final GraphicsDevice device = env.getDefaultScreenDevice();
58 final GraphicsConfiguration gc = device.getDefaultConfiguration();
59 final BufferedImage source = gc.createCompatibleImage(this.getWidth(), this.getHeight());
60 final Graphics2D srcGraphics = source.createGraphics();
61 if (this.selected && System.currentTimeMillis() - this.lastCursorChange > this.blinkInterval) {
62 if (this.cursorState == 0) {
63 this.cursorState = 1;
64 } else {
65 this.cursorState = 0;
66 }
67 this.lastCursorChange = System.currentTimeMillis();
68 }
69 if (this.password) {
70 for (int x = 0; x < this.text.length(); ++x) {
71 drawnString = String.valueOf(drawnString) + "*";
72 }
73 } else {
74 drawnString = this.text;
75 }
76 if (metrics.stringWidth(drawnString) + 9 > this.getWidth()) {
77 this.textStart = metrics.stringWidth(drawnString) + 9 - this.getWidth();
78 }
79 else {
80 this.textStart = 0;
81 }
82 g.setColor(Color.green);
83 g.setFont(this.font);
84 srcGraphics.setColor(Color.green);
85 srcGraphics.setFont(this.font);
86 srcGraphics.drawString(drawnString, -this.textStart + 3, (this.getHeight() + metrics.getHeight()) / 2 - 2);
87 g.drawImage(source, this.getX(), this.getY(), null);
88 g.drawString(this.label, this.getX() - metrics.stringWidth(this.label) - 10, this.getY() + (this.getHeight() + metrics.getHeight()) / 2 - 2);
89 if (this.selected && this.cursorState == 1) {
90 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);
91 }
92 g.setColor(Color.red);
93 g.drawRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
94 }
95
96 public boolean isClicked(final int xCoord, final int yCoord) {
97 return xCoord >= this.getX() && this.getX() + this.getWidth() >= xCoord && yCoord >= this.getY() && this.getY() + this.getHeight() >= yCoord;
98 }
99
100 public void clear() {
101 this.text = "";
102 this.textStart = 0;
103 this.selected = false;
104 }
105
106 public String getText() {
107 return this.text;
108 }
109
110 public void setText(final String newText) {
111 this.text = new String(newText);
112 }
113
114 public void setSelected(final boolean isSelected) {
115 this.selected = isSelected;
116 }
117}
Note: See TracBrowser for help on using the repository browser.