Java Arrow键KeyLIstener逻辑

时间:2014-05-14 12:51:36

标签: java logic keylistener

所以我试图为响应箭头键的箭头键创建一个keyListener,并且可以一次处理多个键。我试图将按键放入ArrayList,然后在我的repaint()方法中处理它们。但是,我有一个问题。我不确定在哪里添加密钥,以及删除它们的位置。我不太关注代码解决方案和应该落后的逻辑。

    //graham

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; 
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

class Arrows extends JPanel implements KeyListener {
private int c = 0;
private int x = 250;
private int y = 250;
private static ArrayList<Integer> keys = new ArrayList<Integer>(); 


public Arrows() {
    this.setPreferredSize(new Dimension(500, 500));
    addKeyListener(this);
}

public void addNotify() {
    super.addNotify();
    requestFocus();
}

public void paintComponent(Graphics g) {

    //*********
    //need to update to make so can press (and hold) multiple different keys at once
    //*********

    g.setColor(Color.WHITE);
    g.fillRect(x, y , 20, 20);
    for(int i = 0; i < keys.size(); i++){ //********> only want to handle one at a time
        //handle each key
        c = keys.get(i);
        switch(c){
        case 37: 
            //left arrow 
            x -= 10;
            keys.remove(i);
            break;
        case 38:
            // up arrow
            y -= 10;
            keys.remove(i);
            break;
        case 39:
            //right arrow
            x += 10;
            keys.remove(i);
            break;
        case 40:
            //down arrow
            y += 10;
            keys.remove(i);
            break;
        }
    }
    g.setColor(Color.BLUE);
    g.fillRect(x, y, 20, 20);
}



public void keyPressed(KeyEvent e) { 
    //******
    //need to create a list of keys pressed, then process them in the repaint. Later delete them after pressed
    //******
    keys.add(e.getKeyCode());
    //repaint();
}
public void keyReleased(KeyEvent e) { 
    //****
    //here need to repaint --- need to correct to do something proper later --- due to holding down a key teleports..
    //****
    repaint();
}
public void keyTyped(KeyEvent e) {
}

public static void main(String[] s) {
    JFrame f = new JFrame();
    f.getContentPane().add(new Arrows());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);

    //need to null out keys
    keys.add(65);
}
}

1 个答案:

答案 0 :(得分:1)

这里看看你是否可以使用我的代码作为例子。他们非常相似。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class MyGame extends JPanel implements ActionListener, KeyListener {
    Timer t = new Timer(5, this);
    int x = 0, y = 0, velx =0, vely =0, g = 0;
    private Color color;

    public MyGame() {
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800, 1000);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(color);
        g.fillRect(x, y, 50, 30);



    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();

        {
            if (code == KeyEvent.VK_DOWN) {
                vely = 1; // removing velx = 0 allows us to go vertically and horizontlly at the same time

            }
            if (code == KeyEvent.VK_UP) {
                vely = -1; // same goes for here

            }
            if (code == KeyEvent.VK_LEFT) {

                velx = -1;
            }

            {
                if (code == KeyEvent.VK_RIGHT) {

                    velx = 1;

                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {
        velx = 0;
        vely = 0;
    }




    public static void main (String arge[]){

        JFrame frame = new JFrame("Testing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new Incoming());           
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }
}