/IAFrameWork-Research/simulator/meta/environment/gui/KeyController.java

http://artificialintelligenceframework.googlecode.com/ · Java · 144 lines · 112 code · 29 blank · 3 comment · 2 complexity · 4819492d1994731d2ec4c1c1400e1a00 MD5 · raw file

  1. package simulator.meta.environment.gui;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Container;
  5. import java.awt.event.KeyEvent;
  6. import java.awt.event.KeyListener;
  7. import java.awt.event.WindowEvent;
  8. import java.awt.event.WindowListener;
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import simulator.meta.agent.ControlledAgent;
  12. public class KeyController extends JFrame implements KeyListener {
  13. /**
  14. *
  15. */
  16. private static final long serialVersionUID = 1L;
  17. private JButton up, down, left, right;
  18. private final Color PRESSED_COLOR = Color.RED;
  19. private Color RELEASED_COLOR;
  20. private ControlledAgent agent;
  21. public KeyController(ControlledAgent agent) {
  22. super(agent.getName());
  23. this.agent = agent;
  24. this.setSize(100, 100);
  25. Container c = this.getContentPane();
  26. c.setLayout(new BorderLayout());
  27. this.up = new JButton("^");
  28. this.down = new JButton("v");
  29. this.left = new JButton("<");
  30. this.right = new JButton(">");
  31. this.RELEASED_COLOR = this.up.getBackground();
  32. this.up.addKeyListener(this);
  33. this.down.addKeyListener(this);
  34. this.left.addKeyListener(this);
  35. this.right.addKeyListener(this);
  36. this.setAlwaysOnTop(true);
  37. this.setResizable(false);
  38. this
  39. .setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
  40. this.addWindowListener(new WindowListener() {
  41. public void windowOpened(WindowEvent arg0) {
  42. }
  43. public void windowIconified(WindowEvent arg0) {
  44. }
  45. public void windowDeiconified(WindowEvent arg0) {
  46. }
  47. public void windowDeactivated(WindowEvent arg0) {
  48. }
  49. public void windowClosing(WindowEvent arg0) {
  50. }
  51. public void windowClosed(WindowEvent arg0) {
  52. }
  53. public void windowActivated(WindowEvent arg0) {
  54. }
  55. });
  56. c.add(up, BorderLayout.NORTH);
  57. c.add(down, BorderLayout.SOUTH);
  58. c.add(left, BorderLayout.WEST);
  59. c.add(right, BorderLayout.EAST);
  60. this.pack();
  61. this.setVisible(true);
  62. }
  63. public void keyTyped(KeyEvent arg0) {
  64. }
  65. public void keyReleased(KeyEvent arg0) {
  66. this.keyUpdate(arg0.getKeyCode(), false);
  67. }
  68. public void keyPressed(KeyEvent arg0) {
  69. this.keyUpdate(arg0.getKeyCode(), true);
  70. this.moveRobot(arg0.getKeyCode());
  71. }
  72. private void keyUpdate(int code, boolean isPressed) {
  73. switch (code) {
  74. case KeyEvent.VK_UP: {
  75. this.up.setBackground((isPressed) ? PRESSED_COLOR : RELEASED_COLOR);
  76. break;
  77. }
  78. case KeyEvent.VK_DOWN: {
  79. this.down.setBackground((isPressed) ? PRESSED_COLOR
  80. : RELEASED_COLOR);
  81. break;
  82. }
  83. case KeyEvent.VK_LEFT: {
  84. this.left.setBackground((isPressed) ? PRESSED_COLOR
  85. : RELEASED_COLOR);
  86. break;
  87. }
  88. case KeyEvent.VK_RIGHT: {
  89. this.right.setBackground((isPressed) ? PRESSED_COLOR
  90. : RELEASED_COLOR);
  91. break;
  92. }
  93. }
  94. }
  95. private void moveRobot(int code) {
  96. switch (code) {
  97. case KeyEvent.VK_UP: {
  98. this.agent.stepUp();
  99. break;
  100. }
  101. case KeyEvent.VK_DOWN: {
  102. this.agent.stepDown();
  103. break;
  104. }
  105. case KeyEvent.VK_LEFT: {
  106. this.agent.turnLeft();
  107. break;
  108. }
  109. case KeyEvent.VK_RIGHT: {
  110. this.agent.turnRight();
  111. break;
  112. }
  113. }
  114. }
  115. }