简体   繁体   English

按下键即可更改球的方向

[英]change the direction of the ball as the key is pressed

i have been trying to make a ball change its direction as any key is pressed. 我一直试图使一个球改变其方向,因为按下任何键。 if the ball is moving sideways as the key will be pressed the ball will start moving downward and as it touch the bottom it moves back in upward position...i think i have written the right code , i cant find anything wrong in it. 如果在按下键时球正在向侧面移动,则球将开始向下移动,并且当它触摸底部时,它将向后移回向上位置...我想我写的是正确的代码,我找不到任何错误。 so can someone please tell me what is problem in this code ? 所以有人可以告诉我这段代码有什么问题吗?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ASS2 extends JFrame {

    public static void main(String args[]) {
        ASS2 g = new ASS2();
        g.setLayout(new BorderLayout());
        g.setSize(500, 500);
        MyPanel mp = new MyPanel();
        g.add(mp);
        mp.setSize(500, 500);
        mp.setBackground(Color.black);
//mp.addKeyListener(mp);
        g.setVisible(true);
        g.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
}

class MyPanel extends JPanel implements KeyListener {

    {
        addKeyListener(this);
    }
    int xpos = 20, ypos = 200;
    int xtop = 15, ytop = 15;
    int xtemp = 250, ytemp = 250;
    int xbot = 450, ybot = 400;
    int flag = 1, flag1 = 1;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.white);
        g2d.fill(new Ellipse2D.Double(xpos, ypos, 50, 50));
        if (xpos < xbot && flag == 1) {
            xpos++;
            if (xpos == xbot) {
                flag = 0;
            }
        } else if (xpos > xtop && flag == 0) {
            xpos--;
            if (xpos == xtop) {
                flag = 1;
            }
        }

        try {
            Thread.sleep(05);
        } catch (Exception e) {
        }
        repaint(1000);

    }

    public void keyPressed(KeyEvent ae) {

    }

    public void keyReleased(KeyEvent ae) {
        Object t = ae.getKeyCode();
        if (t.equals(KeyEvent.VK_DOWN)) {
            if (ypos < ybot && flag1 == 1) {
                ypos++;
                if (ypos == ybot) {
                    flag1 = 0;
                }
            } else if (ypos > ytop && flag1 == 0) {
                ypos--;
                if (ypos == ytop) {
                    flag1 = 1;
                }
            }
            repaint();
        } else if (t.equals(KeyEvent.VK_RIGHT)) {
            if (xpos < xbot && flag == 1) {
                xpos++;
                if (xpos == xbot) {
                    flag = 0;
                }
            } else if (xpos > xtop && flag == 0) {
                xpos--;
                if (xpos == xtop) {
                    flag = 0;
                }
            }
            repaint();
        }

    }
}

Welcome to the wonderful world of "why you shouldn't use KeyListener s". 欢迎来到“为什么不应该使用KeyListener ”的奇妙世界。

Basically, KeyListener will only raise events when the component that the listener is attached to is focusable AND has focus. 基本上, KeyListener仅在侦听器所连接的组件可聚焦且具有焦点时才引发事件。

Instead, you should be using Key Bindings which allow you to control the focus level at which they will trigger key events. 相反,您应该使用按键绑定 ,该按键绑定可让您控制触发按键事件的焦点级别。

  • Do not use Thread.sleep in any method that is called within the EDT, especially paint methods. 不要在EDT中调用的任何方法(尤其是paint方法)中使用Thread.sleep
  • Do not call repaint(1000); 不要调用repaint(1000); inside any paint method or call any method that might trigger a repaint from within paint methods 在任何paint方法内部或调用任何可能从paint方法内部触发repaint paint方法
  • Do not modify state's within paint methods, paint methods paint, that's all. 不要在paint方法内修改状态, paint方法就是这样。
  • Use some kind of "update" thread/process which is responsible for updating the game model and requesting an update to the view. 使用某种“更新”线程/进程负责更新游戏模型并请求更新视图。 java.swing.Timer is good simple choice to start with. java.swing.Timer是一个很好的简单选择。 See How to use Swing Timers 请参阅如何使用Swing计时器
  • When the Swing Timer becomes to limiting for what you are trying to do and you start exploring the use of Thread s, don't modify any UI components outside the EDT. 当Swing Timer限制您要尝试的操作并且开始探索Thread的使用时,请不要在EDT之外修改任何UI组件。 See Concurrency in Swing for more details 有关更多详细信息,请参见Swing中的并发。
  • Always start your programs from within the context of the EDT, see Initial Threads for more details... 始终从EDT上下文中启动程序,有关更多详细信息,请参见初始线程

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

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