[英]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. 相反,您应该使用按键绑定 ,该按键绑定可让您控制触发按键事件的焦点级别。
Thread.sleep
in any method that is called within the EDT, especially paint
methods. paint
方法)中使用Thread.sleep
。 repaint(1000);
repaint(1000);
inside any paint method or call any method that might trigger a repaint
from within paint
methods paint
方法内部触发repaint
paint
方法 paint
methods, paint
methods paint, that's all. paint
方法内修改状态, paint
方法就是这样。 java.swing.Timer
is good simple choice to start with. java.swing.Timer
是一个很好的简单选择。 See How to use Swing Timers 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. Timer
限制您要尝试的操作并且开始探索Thread
的使用时,请不要在EDT之外修改任何UI组件。 See Concurrency in Swing for more details
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.