Java Applet 游戏循环停止鼠标/键盘输入?

发布于 2024-11-18 22:13:49 字数 2367 浏览 4 评论 0原文

我遇到了一个问题,在尝试了大约 2 小时后我无法开始工作。我想要一个循环执行两种方法(绘制和更新),但也监听鼠标/键盘事件。我有一个绘制和更新的循环,但在循环之外不执行任何操作(监听事件)我尝试了很多东西,但没有任何效果。请帮忙?

我尝试使用Runnable Thread,使用不同的顺序,使用wait()和notify(),我尝试了很多东西。但基本上我想知道如何运行循环并仍然检查用户输入

此外,当我尝试单击红色“X”退出程序时,它不会退出但仍然有效

这是代码:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class main extends Applet implements MouseListener, Runnable {

    public main() {
        super();
        init();
    }
    Thread t;
    Screen screen = new Screen();
    String Text = "Hello";
    boolean Running = true;
    boolean Click = false;
    int R = 0x00;
    int G = 0x00;
    int B = 0x00;
    int xpoints[] = {25, 40, 40, 25, 25};
    int ypoints[] = {40, 40, 25, 25, 25};
    int npoints = 5;

    public void run() {
        while (Running) {
            GameLoop();
        }
    }

    public void init() {
        this.addMouseListener(this);
        this.setSize(400, 300); //manually set your Frame's size
        t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
        g.setColor(new Color(R, B, G));
        g.fillPolygon(xpoints, ypoints, npoints);
        Running = true;
        t.run();
    }

    public void mousePressed(MouseEvent e) { //On Mouse Click
        System.exit(0);
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        System.exit(0);
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public boolean keyDown(Event e, int key) {
        return true;
    }

    public void GameLoop() {
        if (Running) {
            if (R != 0xff) {
                R++;
            } else {
                if (G != 0xff) {
                    G++;
                } else {
                    if (B != 0xff) {
                        B++;

                    } else {
                        System.exit(0);
                    }
                }
            }
            try {
                sleep(20);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            paint(getGraphics());
        }
    }

    public void sleep(int time) throws InterruptedException {
        Thread.sleep(time, 0);
    }
}

I got a problem I couldn't get to work after about 2 Hours of trying. I want to have a loop that do 2 Methods (Draw and update) but also listen to Mouse/Keyboard events. I have a loop that Draws and Updates, but does nothing outside of the loop ( Listening to events ) I tried alot of things but nothing worked. Help Please?

I tried using the Runnable Thread, using different orders, using wait() and notify(), I've tried alot of things. But basicly I want to know how to run a loop and still check for User Input

Also when I try to quit the program clicking the red "X", it won't quit but still work

Here's the Code:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class main extends Applet implements MouseListener, Runnable {

    public main() {
        super();
        init();
    }
    Thread t;
    Screen screen = new Screen();
    String Text = "Hello";
    boolean Running = true;
    boolean Click = false;
    int R = 0x00;
    int G = 0x00;
    int B = 0x00;
    int xpoints[] = {25, 40, 40, 25, 25};
    int ypoints[] = {40, 40, 25, 25, 25};
    int npoints = 5;

    public void run() {
        while (Running) {
            GameLoop();
        }
    }

    public void init() {
        this.addMouseListener(this);
        this.setSize(400, 300); //manually set your Frame's size
        t = new Thread(this);
        t.start();
    }

    public void paint(Graphics g) {
        g.setColor(new Color(R, B, G));
        g.fillPolygon(xpoints, ypoints, npoints);
        Running = true;
        t.run();
    }

    public void mousePressed(MouseEvent e) { //On Mouse Click
        System.exit(0);
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        System.exit(0);
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public boolean keyDown(Event e, int key) {
        return true;
    }

    public void GameLoop() {
        if (Running) {
            if (R != 0xff) {
                R++;
            } else {
                if (G != 0xff) {
                    G++;
                } else {
                    if (B != 0xff) {
                        B++;

                    } else {
                        System.exit(0);
                    }
                }
            }
            try {
                sleep(20);
            } catch (InterruptedException e) {

                e.printStackTrace();
            }
            paint(getGraphics());
        }
    }

    public void sleep(int time) throws InterruptedException {
        Thread.sleep(time, 0);
    }
}

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

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

发布评论

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

评论(2

宫墨修音 2024-11-25 22:13:50

本教程应该提供一些关于如何构建程序的见解。 这个对于鼠标监听器很有帮助。

您应该解决的问题:
1) 你正在使用 paint 方法做一些可疑的事情。你为什么要在那里调用t.run()?线程t已经在运行并循环不断地调用paint()方法来重绘屏幕。删除这个调用,看看你会得到什么。

1)你的线程/应用程序的破坏很差。上面的第一个示例提供了一种更好的方法来实现这一点

2) 您将 System.Exit(0) 放在 mousePressed() 上,并添加注释 //on鼠标单击,但 mouseClicked() 中没有任何内容...它可以工作,但它的约定不好

3) 将类命名为 main 是非常糟糕的约定,既令人困惑且不切实际。将您的类重命名为“Game”或类似名称。

4)如果不使用Screen为什么要声明它?

This tutorial should provide some insight as to how your program should be structured. And this one is helpful for the mouse listener.

Issues you should address:
1) You're doing something fishy with the paint method. Why are you calling t.run() in there? The thread t is already running and looping constantly calling the paint() method to redraw the screen. Remove this call and see what you get.

1) The destruction of your thread/applciation is poor. The first example above provides a better way for that to occur

2) You have your System.Exit(0) on mousePressed() with the comment //on mouse click but nothing in mouseClicked()... it works but its bad convention

3)Having your class named main is extremely poor convention that is both confusing and impractical. Rename your class to something like "Game" or similar.

4) Why declare Screen if you don't use it?

陌上芳菲 2024-11-25 22:13:50

我看到您在初始化时将 Running 变量定义为 true。该变量用于确定游戏是否应该停止。但是,我没有看到任何将此变量的值修改为 false 的地方。这可以解释为什么你的游戏永远不会退出。

至于游戏无法运行,请尝试在 IDE 中调试应用程序。然后,您应该注意抛出的异常(如果有)以及您所质疑的任何变量的值。希望这能让您深入了解应用程序的行为。

不要忘记向我们通报您发现的任何新信息,以便我们一路为您提供帮助。

I see that you define a Running variable to be true upon initialization. This variable is used to determine whether or not the game should stop. I, however, don't see any place where you modify the value of this variable to false. This would explain why your game never exits.

As for the the game not working, try debugging the application in an IDE. You should then pay attention to what, if any, Exception are being thrown and the values of any variables you are questioning. Hopefully this will give you insight into the behavior of your app.

Don't forget to update us with any new info you discover so we can help you out along the way.

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