Key-Listener in Java

javaman1

Grünschnabel
Hallo Leute,

ich programmiere zurzeit das Videospiel Snake. Ich bekomme es jedoch nicht hin, dass der KeyListener funktioniert. Hier ist die hauptklasse und im voraus schon einmal sorry für den Code Salat. Ich habe jetzt noch die Buttons zum steuern mit eingebunden, die waren jedoch nur zum Testen. ganz unten sind meine KeyEvents. Es passiert jedoch garnichts,wenn ich die Tasten drücke.
Was muss ich an meinem Code verändern, dass die KeyPressed Methode aufgerufen wird, sobald ich eine Taste drücke?


Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class Snake extends JFrame implements KeyListener,EventListener{
    static int last_direction=0;
    static int height=20;
    static int width=20;
    static JTextArea displayArea;
    static Graphics_array graph_ar = new Graphics_array(height,width);
    static JFrame f = new JFrame("Snake");
    static BorderLayout bl= new BorderLayout();
    static boolean started_game=true;
    static Graf_thread t1 = new Graf_thread();
    static JPanel game_graf = new JPanel() {
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2 =(Graphics2D)g;
      
      int x = (game_graf.getSize().width);
      int y = (game_graf.getSize().height);
      for (int i1 = 0; i1 < height; i1++) {
        for (int i2 = 0; i2 < width; i2++) {
            g2.setColor(graph_ar.get_part(i1,i2).get_Color());
            g2.fillRect((x/width)*i1,y-((y/height)*(i2+1)),x/width,y/height);
        }
      }
      
    }
  };
  static JPanel north_pan = new JPanel();
  static GridLayout gl_north = new GridLayout(0,4);
 
  static JButton but_down = new JButton("down");
  static JButton but_up = new JButton("up");
  static JButton but_right = new JButton("right");
  static JButton but_left = new JButton("left");
 
  public static void reset(){
      graph_ar = null;
      graph_ar = new Graphics_array(height,width);
      game_graf.repaint();
      started_game= true;
  }
  public static void main(String[] args){
      f.setSize(600,600);
      f.setVisible(true);
      f.add(game_graf, bl.CENTER);
      but_left.addActionListener(ear_left);
      but_right.addActionListener(ear_right);
      but_up.addActionListener(ear_up);
      but_down.addActionListener(ear_down);
      north_pan.setLayout(gl_north);
      north_pan.add(but_left);
      north_pan.add(but_right);
      north_pan.add(but_up);
      north_pan.add(but_down);
      f.add(north_pan,bl.NORTH);

      
      
      try{
          t1.start();
      } catch(Exception e) {
          System.out.println(e);
      }
  }
 
 
  static ActionListener ear_left = new ActionListener(){
    public void actionPerformed(ActionEvent e){
      if(last_direction!=0){
        graph_ar.chng_direction(2);
        last_direction=2;
      }
    }
  };
  static ActionListener ear_right = new ActionListener(){
    public void actionPerformed(ActionEvent e){
      if(last_direction!=2){
        graph_ar.chng_direction(0);
        last_direction=0;
      }
    }
  };
  static ActionListener ear_up = new ActionListener(){
    public void actionPerformed(ActionEvent e){
      if(last_direction!=1){
        graph_ar.chng_direction(3);
        last_direction=3;
      }
    }
  };
  static ActionListener ear_down = new ActionListener(){
    public void actionPerformed(ActionEvent e){
      if(last_direction!=3){
        graph_ar.chng_direction(1);
        last_direction=1;
      }
    }
  };
  public void keyTyped(KeyEvent e) {
      
  }   
  public void keyPressed(KeyEvent e) {
      int keyCode = e.getKeyCode();
      switch( keyCode ) {
          case KeyEvent.VK_UP:
            if(last_direction!=1){
                graph_ar.chng_direction(3);
                last_direction=3;
            }
            break;
          case KeyEvent.VK_DOWN:
            graph_ar.chng_direction(1);
            break;
          case KeyEvent.VK_LEFT:
            graph_ar.chng_direction(2);
            break;
          case KeyEvent.VK_RIGHT :
            graph_ar.chng_direction(0);
            break;
      }
  }

  public void keyReleased(KeyEvent e) {
      System.out.println("pressed");
  }
 
}
 

Neue Beiträge

Zurück