Java Applet - 在屏幕上移动球

发布于 2024-12-27 07:22:26 字数 1003 浏览 1 评论 0原文

好的,我如何使用 Applet 从键盘移动球?

到目前为止我有这段代码,它没有做任何事情。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class KeyboardGame extends Applet implements KeyListener
{

    private static final long serialVersionUID = 1L;
    private static boolean keyboadrRightPressed = false;

    public void init()
    {
         addKeyListener(this);
    }

    public void keyPressed(KeyEvent e) 
    {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_RIGHT)
        {
            keyboadrRightPressed = true;
        }
        else
        {
            keyboadrRightPressed = false;
        }
    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {
    }

    public void paint(Graphics g)
    {
        g.fillOval(20,20,20,20);
        g.drawString("String :"+keyboadrRightPressed,20,30);
    }

}

我还必须了解它是如何工作的。我不明白为什么我的动作侦听器不起作用,我需要一个

while(true)

线程吗?

Ok, how to i move from keyboard a ball using an Applet?

I have so far this code, that don't do anything.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class KeyboardGame extends Applet implements KeyListener
{

    private static final long serialVersionUID = 1L;
    private static boolean keyboadrRightPressed = false;

    public void init()
    {
         addKeyListener(this);
    }

    public void keyPressed(KeyEvent e) 
    {
        int keyCode = e.getKeyCode();
        if(keyCode == KeyEvent.VK_RIGHT)
        {
            keyboadrRightPressed = true;
        }
        else
        {
            keyboadrRightPressed = false;
        }
    }

    public void keyReleased(KeyEvent e) {

    }

    public void keyTyped(KeyEvent e) {
    }

    public void paint(Graphics g)
    {
        g.fillOval(20,20,20,20);
        g.drawString("String :"+keyboadrRightPressed,20,30);
    }

}

And also i have to understand how it works. I don't get why my action listener won't work, do i need an

while(true)

or an Thread?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2025-01-03 07:22:26

您的动作侦听器实际上可能工作正常,但您需要在按下按键时重新绘制小程序,以便您的字符串实际出现。尝试将 keyPressed 更改为:

public void keyPressed(KeyEvent e) 
{
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_RIGHT)
    {
        keyboadrRightPressed = true;
    }
    else
    {
        keyboadrRightPressed = false;
    }
    repaint();
}

实际移动球会有所不同,具体取决于您希望球实际移动的方式。我猜你希望它在按住键时继续向右移动,所以我要做的是实现一个计时器或其他形式的线程,每隔 0.25 秒(或无论你想要多久)检查一次 KeyboardRightPressed 并会如果正确,则将球向右移动。然后,在代码的 keyReleased 部分中,您还应该添加逻辑,以便在松开按键时将 KeyboardRightPressed 设置回 false。

Your action listener might actually be working fine, but you need to repaint the applet when the key is pressed so that your string actually appears. Try changing the keyPressed to this:

public void keyPressed(KeyEvent e) 
{
    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_RIGHT)
    {
        keyboadrRightPressed = true;
    }
    else
    {
        keyboadrRightPressed = false;
    }
    repaint();
}

Actually moving the ball will differ depending on how you want the ball to actually move. I'm guessing you want it to continue moving right while the key is held down, so what I would do is implement a timer or some other form of thread that every .25 seconds (or however long you want) checks the keyboardRightPressed and will move the ball right if it is true. Then in the keyReleased portion of your code you should also add logic to set keyboardRightPressed back to false when you let up on the key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文