Changeset 8edd04e in lost-haven for gamegui/Textbox.java
- Timestamp:
- Jun 7, 2020, 3:04:32 PM (5 years ago)
- Branches:
- master
- Children:
- a49176d
- Parents:
- 155577b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
gamegui/Textbox.java
r155577b r8edd04e 2 2 3 3 import java.awt.*; 4 import java.awt. image.*;5 import java.awt. event.*;4 import java.awt.event.KeyEvent; 5 import java.awt.image.BufferedImage; 6 6 7 public 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()); 7 public class Textbox extends Member { 42 8 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+="*";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 protected boolean noBorder; 19 20 public Textbox(String newName, int newX, int newY, int newWidth, int newHeight, String newLabel, Font newFont, boolean isPass) { 21 super(newName, newX, newY, newWidth, newHeight); 22 this.label = new String(newLabel); 23 this.text = new String(); 24 this.font = newFont; 25 this.textStart = 0; 26 this.selected = false; 27 this.cursorState = 0; 28 this.blinkInterval = 500; 29 this.password = isPass; 30 this.noBorder = false; 31 } 32 33 public void setBorder(boolean b) { 34 this.noBorder = !b; 35 } 36 37 public void handleEvent(KeyEvent e) { 38 if (32 <= e.getKeyCode() && e.getKeyCode() <= 127) { 39 if (e.getKeyCode() == 127) { 40 if (this.text.length() > 0) { 41 this.text = this.text.substring(0, this.text.length() - 1); 76 42 } 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); 43 } else { 44 this.text = String.valueOf(this.text) + Character.toString(e.getKeyChar()); 45 } 46 } else if (e.getKeyCode() == 8) { 47 if (this.text.length() > 0) { 48 this.text = this.text.substring(0, this.text.length() - 1); 49 } 50 } 51 } 89 52 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 } 53 public void draw(Graphics g) { 54 String drawnString = new String(); 55 FontMetrics metrics = g.getFontMetrics(this.font); 56 GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); 57 GraphicsDevice device = env.getDefaultScreenDevice(); 58 GraphicsConfiguration gc = device.getDefaultConfiguration(); 59 BufferedImage source = gc.createCompatibleImage(getWidth(), getHeight()); 60 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 > getWidth()) { 77 this.textStart = metrics.stringWidth(drawnString) + 9 - getWidth(); 78 } else { 79 this.textStart = 0; 80 } 81 g.setColor(Color.green); 82 g.setFont(this.font); 83 srcGraphics.setColor(Color.green); 84 srcGraphics.setFont(this.font); 85 srcGraphics.drawString(drawnString, 5 - this.textStart, (getHeight() + metrics.getHeight()) / 2 - 2); 86 g.drawImage(source, getX(), getY(), null); 87 g.drawString(this.label, getX() - metrics.stringWidth(this.label) - 10, getY() + (getHeight() + metrics.getHeight()) / 2 - 2); 88 if (this.selected && this.cursorState == 1) { 89 g.drawLine(getX() + metrics.stringWidth(drawnString) - this.textStart + 6, getY() + 5, getX() + metrics.stringWidth(drawnString) - this.textStart + 6, getY() + getHeight() - 5); 90 } 91 g.setColor(Color.red); 92 if (!this.noBorder) { 93 g.drawRect(getX(), getY(), getWidth(), getHeight()); 94 } 95 } 116 96 117 public int getBlinkInterval() { 118 return blinkInterval; 119 } 97 public boolean isClicked(int xCoord, int yCoord) { 98 if (xCoord < getX() || getX() + getWidth() < xCoord) { 99 return false; 100 } 101 if (yCoord < getY() || getY() + getHeight() < yCoord) { 102 return false; 103 } 104 return true; 105 } 120 106 121 public int getCursorState() { 122 return cursorState; 123 } 107 public void clear() { 108 this.text = ""; 109 this.textStart = 0; 110 this.selected = false; 111 } 124 112 125 public Font getFont() {126 return font;127 113 public int getBlinkInterval() { 114 return this.blinkInterval; 115 } 128 116 129 public String getLabel() {130 return label;131 117 public int getCursorState() { 118 return this.cursorState; 119 } 132 120 133 public long getLastCursorChange() {134 return lastCursorChange;135 121 public Font getFont() { 122 return this.font; 123 } 136 124 137 public boolean isSelected() {138 return selected;139 125 public String getLabel() { 126 return this.label; 127 } 140 128 141 public String getText() {142 return text;143 129 public long getLastCursorChange() { 130 return this.lastCursorChange; 131 } 144 132 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 } 133 public boolean isSelected() { 134 return this.selected; 135 } 156 136 157 public void setTextStart(int textStart) { 158 this.textStart = textStart; 159 } 160 161 public void setCursorState(int cursorState) { 162 this.cursorState = cursorState; 163 } 137 public String getText() { 138 return this.text; 139 } 164 140 165 public void setLastCursorChange(long lastCursorChange) { 166 this.lastCursorChange = lastCursorChange; 167 } 141 public int getTextStart() { 142 return this.textStart; 143 } 144 145 public void setText(String newText) { 146 this.text = newText; 147 } 148 149 public void setSelected(boolean isSelected) { 150 this.selected = isSelected; 151 } 152 153 public void setTextStart(int textStart) { 154 this.textStart = textStart; 155 } 156 157 public void setCursorState(int cursorState) { 158 this.cursorState = cursorState; 159 } 160 161 public void setLastCursorChange(long lastCursorChange) { 162 this.lastCursorChange = lastCursorChange; 163 } 168 164 }
Note:
See TracChangeset
for help on using the changeset viewer.