简体   繁体   English

Java Graphics 2D和KeyListener

[英]Java Graphics 2D and KeyListener

I am teaching myself 2D graphics in Java right now. 我现在正在用Java自学2D图形。 I am trying to create a block-stacking game. 我正在尝试创建一个积木游戏。 So far, I am able to get the first row moving and stopping, but my aim is to generate a new row below the first while the first row stays in place. 到目前为止,我能够移动和停止第一行,但我的目标是在第一行保持原位的同时在第一行下方生成一个新行。 I feel like the fix for this is a simple concept I haven't learned yet. 觉得解决此问题的方法很简单,我还没学会。 However, any advice will be appreciated. 但是,任何建议将不胜感激。 If you want to point me in the right direction to where I can teach myself 2D Graphics in general, I will appreciate that also. 如果您想为我指明正确的方向,以便我总体上可以自学2D图形,那么我也将不胜感激。

My code for the JFrame is as follows: 我的JFrame代码如下:

public class  BlockStacker extends JFrame {
public class AL extends KeyAdapter {
    stack2 s2 = new stack2();
    public void keyPressed(KeyEvent e){
        int keyCode = e.getKeyCode();
    }

}
public static void main(String[] args){
    stack2 s = new stack2();
    JFrame f = new JFrame();
    f.add(s);
    f.setVisible(true);
    f.setSize(1920, 1080);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setTitle("Block Stacker");


}

}

The code with the graphics and the keylistener is this: 带有图形和键侦听器的代码是这样的:

public class stack2 extends JPanel implements ActionListener, KeyListener{
    Timer t = new Timer(250, this);
    double x, y, velX = 253;


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

    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        setBackground(Color.BLACK);
        Color blue = new Color(0,0,255);
        g2.setColor(blue);

        Rectangle2D.Double rectangle = new Rectangle.Double(x + 210, y, 200, 100);
        g2.fill(rectangle);

        Rectangle2D.Double rectangle2 = new Rectangle.Double(x, y, 200, 100);
        g2.fill(rectangle2);

        Rectangle2D.Double rectangle3 = new Rectangle.Double(x + 420, y, 200, 100);
        g2.fill(rectangle3);

    }

    public void actionPerformed(ActionEvent e) {
        if (x < 0) {
            velX = -velX;
        }

        x += velX;
        repaint();

        if (x < 10 || x > 1200) {
          velX = -velX;
          repaint();
        }
    }

    public void space() {
        velX = 0;
        repaint();

    }
public void space2() {


    }


public void keyPressed(KeyEvent e) {
  int code = e.getKeyCode();
     if (code == KeyEvent.VK_SPACE) {
       space();
            }
 }     

public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
    if (code == KeyEvent.VK_SPACE) {
        space2();
    }
}

}

There are multiple problems... 有多个问题...

  1. You attach the KeyListener to JFrame, but a JFrame has a JRootPane, content pane and it's content between it and the user, any of those components could have keyboard focus, preventing the frame from receiving key events 您将KeyListener附加到JFrame,但是JFrame具有JRootPane,内容窗格以及它与用户之间的内容,这些组件中的任何一个都可能具有键盘焦点,从而阻止框架接收键事件
  2. You create a instance of Stack in both the KeyAdapter and then the frame, so the instance of Stack you are trying to change isn't the version that's on the screen 您在KeyAdapter和框架中都创建了Stack实例,因此您要更改的Stack实例不是屏幕上的版本
  3. KeyListener only responds to key events when the component that they are registed to are focus able and have focus... 仅当注册关键组件的组件具有焦点能力并具有焦点时,KeyListener才响应它们。

Use key bindings instead? 改用键绑定? See How to use key bindings 请参阅如何使用键绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM