Advertisement
huneater

TextField.class

Apr 21st, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package gui;
  2.  
  3. import org.newdawn.slick.Color;
  4. import org.newdawn.slick.GameContainer;
  5. import org.newdawn.slick.Graphics;
  6.  
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9.  
  10. /**
  11.  * User: Zsolt
  12.  * Date: 2013.04.21.
  13.  * Time: 17:52
  14.  */
  15.  
  16. public class TextField extends GUI implements KeyListener {
  17.     private boolean focused = false;
  18.     private String text = "";
  19.  
  20.     public TextField(int x, int y, int width, int height) {
  21.         super(x, y, width, height);
  22.     }
  23.  
  24.     public boolean isFocused() {
  25.         return focused;
  26.     }
  27.  
  28.     public void setFocused(boolean focused) {
  29.         this.focused = focused;
  30.     }
  31.  
  32.     public void append(String text) {
  33.         this.text = this.text + text;
  34.     }
  35.  
  36.     public void remove() {
  37.         if (text.length() > 0) {
  38.             text = text.substring(0, text.length() - 1);
  39.         }
  40.     }
  41.  
  42.     @Override
  43.     public void render(GameContainer gc, Graphics g) {
  44.         if (!focused) {
  45.             g.setColor(Color.gray);
  46.         } else {
  47.             g.setColor(Color.orange);
  48.         }
  49.         g.fillRect(x - width / 2, y - height / 2, width, height);
  50.  
  51.         g.setColor(Color.darkGray);
  52.         g.fillRect(x - width / 2 + 5, y - height / 2 + 5, width - 10, height - 10);
  53.  
  54.         g.setColor(Color.white);
  55.         g.drawString(text, x - g.getFont().getWidth(text) / 2, y - g.getFont().getHeight(text) / 2);
  56.     }
  57.  
  58.     @Override
  59.     public void keyTyped(KeyEvent e) {
  60.     }
  61.  
  62.     @Override
  63.     public void keyPressed(KeyEvent e) {
  64.         System.out.println("asd");
  65.     }
  66.  
  67.     @Override
  68.     public void keyReleased(KeyEvent e) {
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement