/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
- package simulator.meta.environment.gui;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Container;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.WindowEvent;
- import java.awt.event.WindowListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import simulator.meta.agent.ControlledAgent;
- public class KeyController extends JFrame implements KeyListener {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private JButton up, down, left, right;
- private final Color PRESSED_COLOR = Color.RED;
- private Color RELEASED_COLOR;
-
- private ControlledAgent agent;
- public KeyController(ControlledAgent agent) {
- super(agent.getName());
-
- this.agent = agent;
-
- this.setSize(100, 100);
- Container c = this.getContentPane();
- c.setLayout(new BorderLayout());
- this.up = new JButton("^");
- this.down = new JButton("v");
- this.left = new JButton("<");
- this.right = new JButton(">");
- this.RELEASED_COLOR = this.up.getBackground();
- this.up.addKeyListener(this);
- this.down.addKeyListener(this);
- this.left.addKeyListener(this);
- this.right.addKeyListener(this);
- this.setAlwaysOnTop(true);
- this.setResizable(false);
- this
- .setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
- this.addWindowListener(new WindowListener() {
- public void windowOpened(WindowEvent arg0) {
- }
- public void windowIconified(WindowEvent arg0) {
- }
- public void windowDeiconified(WindowEvent arg0) {
- }
- public void windowDeactivated(WindowEvent arg0) {
- }
- public void windowClosing(WindowEvent arg0) {
- }
- public void windowClosed(WindowEvent arg0) {
- }
- public void windowActivated(WindowEvent arg0) {
- }
- });
- c.add(up, BorderLayout.NORTH);
- c.add(down, BorderLayout.SOUTH);
- c.add(left, BorderLayout.WEST);
- c.add(right, BorderLayout.EAST);
- this.pack();
- this.setVisible(true);
- }
- public void keyTyped(KeyEvent arg0) {
- }
- public void keyReleased(KeyEvent arg0) {
- this.keyUpdate(arg0.getKeyCode(), false);
- }
- public void keyPressed(KeyEvent arg0) {
- this.keyUpdate(arg0.getKeyCode(), true);
- this.moveRobot(arg0.getKeyCode());
- }
- private void keyUpdate(int code, boolean isPressed) {
- switch (code) {
- case KeyEvent.VK_UP: {
- this.up.setBackground((isPressed) ? PRESSED_COLOR : RELEASED_COLOR);
- break;
- }
- case KeyEvent.VK_DOWN: {
- this.down.setBackground((isPressed) ? PRESSED_COLOR
- : RELEASED_COLOR);
- break;
- }
- case KeyEvent.VK_LEFT: {
- this.left.setBackground((isPressed) ? PRESSED_COLOR
- : RELEASED_COLOR);
- break;
- }
- case KeyEvent.VK_RIGHT: {
- this.right.setBackground((isPressed) ? PRESSED_COLOR
- : RELEASED_COLOR);
- break;
- }
- }
- }
- private void moveRobot(int code) {
- switch (code) {
- case KeyEvent.VK_UP: {
- this.agent.stepUp();
- break;
- }
- case KeyEvent.VK_DOWN: {
- this.agent.stepDown();
- break;
- }
- case KeyEvent.VK_LEFT: {
- this.agent.turnLeft();
- break;
- }
- case KeyEvent.VK_RIGHT: {
- this.agent.turnRight();
- break;
- }
- }
- }
- }