source: java-rpg-client/gamegui/Textbox.java

Last change on this file was 55522be, checked in by dportnoy <dmp1488@…>, 17 years ago

[svn r8]

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[55522be]1package gamegui;
2
3import java.awt.*;
4import java.awt.image.*;
5import java.awt.event.*;
6
7public class Textbox extends Member
8{
9 private String label;
10 private String text;
11 private Font font;
12 private int textStart;
13 private boolean selected;
14 private int cursorState;
15 private int blinkInterval;
16 private long lastCursorChange;
17 private boolean password;
18
19 public Textbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont, boolean isPass) {
20 super(newName, newX, newY, newWidth, newHeight);
21
22 label = new String(newLabel);
23 text = new String();
24 font = newFont;
25 textStart = 0;
26 selected = false;
27 cursorState = 0;
28 blinkInterval = 1000/2;
29 password = isPass;
30 }
31
32 public void handleEvent(KeyEvent e) {
33 if(32 <= e.getKeyCode() && e.getKeyCode() <= 127)
34 {
35 if(e.getKeyCode() == 127)
36 {
37 if(text.length() > 0)
38 text = text.substring(0, text.length() - 1);
39 }
40 else
41 text = text + Character.toString(e.getKeyChar());
42
43 }
44 else if(e.getKeyCode() == 8)
45 {
46 if(text.length() > 0)
47 text = text.substring(0, text.length() - 1);
48 }
49 }
50
51 public void draw(Graphics g) {
52 String drawnString = new String();
53 FontMetrics metrics = g.getFontMetrics(font);
54
55 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
56 GraphicsDevice device = env.getDefaultScreenDevice();
57 GraphicsConfiguration gc = device.getDefaultConfiguration();
58
59 BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight());
60 Graphics2D srcGraphics = source.createGraphics();
61
62 if(selected && System.currentTimeMillis() - lastCursorChange > blinkInterval)
63 {
64 if(cursorState == 0)
65 cursorState = 1;
66 else
67 cursorState = 0;
68
69 lastCursorChange = System.currentTimeMillis();
70 }
71
72 if(password)
73 {
74 for(int x=0;x<text.length();x++)
75 drawnString+="*";
76 }
77 else
78 drawnString = text;
79
80 if(metrics.stringWidth(drawnString) + 9 > getWidth())
81 textStart = metrics.stringWidth(drawnString)+9-getWidth();
82 else
83 textStart = 0;
84
85 g.setColor(Color.green);
86 g.setFont(font);
87 srcGraphics.setColor(Color.green);
88 srcGraphics.setFont(font);
89
90 srcGraphics.drawString(drawnString, 5-textStart, (getHeight() + metrics.getHeight())/2 - 2);
91
92 g.drawImage(source, getX(), getY(), null);
93 g.drawString(label, getX() - metrics.stringWidth(label) - 10, getY() + (getHeight() + metrics.getHeight())/2 - 2);
94
95 if(selected && cursorState == 1)
96 g.drawLine(getX() + metrics.stringWidth(drawnString) - textStart + 6, getY() + 5, getX() + metrics.stringWidth(drawnString) - textStart + 6, getY() + getHeight() - 5);
97
98 g.setColor(Color.red);
99 g.drawRect(getX(), getY(), getWidth(), getHeight());
100 }
101
102 public boolean isClicked(int xCoord, int yCoord) {
103 if(xCoord < getX() || getX() + getWidth() < xCoord)
104 return false;
105 if(yCoord < getY() || getY() + getHeight() < yCoord)
106 return false;
107
108 return true;
109 }
110
111 public void clear() {
112 text = "";
113 textStart = 0;
114 selected = false;
115 }
116
117 public int getBlinkInterval() {
118 return blinkInterval;
119 }
120
121 public int getCursorState() {
122 return cursorState;
123 }
124
125 public Font getFont() {
126 return font;
127 }
128
129 public String getLabel() {
130 return label;
131 }
132
133 public long getLastCursorChange() {
134 return lastCursorChange;
135 }
136
137 public boolean isSelected() {
138 return selected;
139 }
140
141 public String getText() {
142 return text;
143 }
144
145 public int getTextStart() {
146 return textStart;
147 }
148
149 public void setText(String newText) {
150 text = newText;
151 }
152
153 public void setSelected(boolean isSelected) {
154 selected = isSelected;
155 }
156
157 public void setTextStart(int textStart) {
158 this.textStart = textStart;
159 }
160
161 public void setCursorState(int cursorState) {
162 this.cursorState = cursorState;
163 }
164
165 public void setLastCursorChange(long lastCursorChange) {
166 this.lastCursorChange = lastCursorChange;
167 }
168}
Note: See TracBrowser for help on using the repository browser.