//Titel:         keyStroke01
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class keyStroke2 extends JFrame {

  JButton BnUp = new JButton("Up");
  JButton BnDown = new JButton("Down");

  JButton BnLeft = new JButton("Left");
  JButton BnRight = new JButton("Right");

  JButton BnEsc = new JButton("Abbruch");

  int t, l;

  public keyStroke2() {
   // setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    t=120;
    l=170;
    setGUI();
  }

  private void setGUI() {
    setSize(600, 430);
    setTitle("JFrame mit keyStroke");

    BnUp.setFont( new Font("Arial", Font.BOLD,18)  );
    BnDown.setFont( new Font("Arial", Font.BOLD,18)  );
    BnLeft.setFont( new Font("Arial", Font.BOLD,18)  );
    BnRight.setFont( new Font("Arial", Font.BOLD,18)  );
    BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().setLayout( null );

    BnUp.setBounds(200, 20, 90, 25);
    BnDown.setBounds(200, 220, 90, 25);
    BnLeft.setBounds(20, 120, 90, 25);
    BnRight.setBounds(400, 120, 90, 25);
    BnEsc.setBounds(l,t, 120, 25);

    this.getContentPane().add(BnUp);
    this.getContentPane().add(BnDown);
    this.getContentPane().add(BnLeft);
    this.getContentPane().add(BnRight);
    this.getContentPane().add(BnEsc);

    BnEsc.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
           BnEsc_Click();
       }
    });

    // ---------------------------    

    // Define Up-ActionListener
    Action Up_actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        JButton source = (JButton) actionEvent.getSource();
        BnUp_Click();
      }
    };
     // 0 == InputEvent.SHIFT_MASK
   KeyStroke keyUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true);
    InputMap inputMap = BnUp.getInputMap();
    inputMap.put(keyUp, "Up");
    ActionMap actionMap = BnUp.getActionMap();
    actionMap.put("Up", Up_actionListener);
    BnUp.setActionMap(actionMap);

    // ---------------------------    

    // Define Down-ActionListener
    Action Down_actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        JButton source = (JButton) actionEvent.getSource();
        System.out.println("Down");
        BnDown_Click();
      }
    };
   KeyStroke keyDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true);
   InputMap inputMap2 = BnDown.getInputMap();
   inputMap2.put(keyDown, "Down");
   ActionMap actionMap2 = BnDown.getActionMap();
   actionMap2.put("Down", Down_actionListener);
   BnDown.setActionMap(actionMap2);


    // ---------------------------    

    // Define Left-ActionListener
    Action Left_actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        JButton source = (JButton) actionEvent.getSource();
        System.out.println("Left");
        BnLeft_Click();
      }
    };
   KeyStroke keyLeft = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true);
   inputMap = BnLeft.getInputMap();
   inputMap.put(keyLeft, "Left");
   actionMap = BnLeft.getActionMap();
   actionMap.put("Left", Left_actionListener);
   BnLeft.setActionMap(actionMap);

    // ---------------------------    

    // Define Left-ActionListener
    Action Right_actionListener = new AbstractAction() {
      public void actionPerformed(ActionEvent actionEvent) {
        JButton source = (JButton) actionEvent.getSource();
        System.out.println("Right");
        BnRight_Click();
      }
    };
   KeyStroke keyRight = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true);
   inputMap = BnRight.getInputMap();
   inputMap.put(keyRight, "Right");
   actionMap = BnRight.getActionMap();
   actionMap.put("Right", Right_actionListener);
   BnRight.setActionMap(actionMap);

  } // setGUI


    private void BnUp_Click() {
        System.out.println("Hier in Up");
        if (t>0) t--;
        BnEsc.setBounds(l,t, 120, 25);
    }
    private void BnDown_Click() {
        System.out.println("Hier in Down");
        t++;
        BnEsc.setBounds(l,t, 120, 25);
    }
    private void BnLeft_Click() {
        if (l>0) l--;
        BnEsc.setBounds(l,t, 120, 25);
        System.out.println("Hier in Left");
    }
    private void BnRight_Click() {
        l++;
        BnEsc.setBounds(l,t, 120, 25);
        System.out.println("Hier in Right");
    }

    private void BnEsc_Click() {
        setVisible(false);
        dispose();
    }

  public static void main(String[] args) {
    keyStroke2 frame = new keyStroke2();
    frame.setVisible(true);
 }

}  // keyStroke
