线程中的MouseMotionListener [英] MouseMotionListener in a Thread

查看:89
本文介绍了线程中的MouseMotionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,现在我正试图了解Threads.

我目前正在研究使球弹跳或丢球"游戏.

在此游戏中,有一个弹起墙"或大梁"的球.

墙随着光标的x位置移动.没有运动.

现在我不能使用Timer,因为如果这样做,我将在MouseMotion和Ball Movement中具有相同的延迟.

我要实现的目标是延迟10毫秒获得mousemotion信息,延迟200毫秒获得球的运动.

使用计时器,我必须选择墙壁和球的延迟时间. 10毫秒对于墙运动非常有用,因为当我移动鼠标时它会立即运动,但对于球运动来说却太快了.甚至诺里斯(Chuck Norris)都无法如此快地移动鼠标,以使球从墙壁上弹起.

我尝试创建2个不同的计时器,但是它不接受鼠标侦听器. ActionListenerTimer唯一接受的侦听器,但是由于某些原因,如果我使用单个计时器并将this作为参数,它仍会触发actionPerformed方法旁边的鼠标移动方法.但这给了我相同的球和墙延迟时间.

所以这就是为什么我需要线程来做到这一点的原因.但是我不知道如何在Thread内从MouseListenerpaintComponent获取鼠标运动信息.

希望你们能理解我的问题.

这就是我现在试图获得10ms的墙延迟和200ms的球延迟的方式:

private Timer trigger = new Timer(200, this);
private Timer wall = new Timer(10, this);

在paintComponent中

if ((start.getText() == "Spiel aktiv..." || trigger.isRunning() == true) || wall.isRunning() == true) {
g.setColor(new Color(255, 254, 102));
g.fillRect(x, screen.getHeight() - 15, length, 10);
}

if ((start.getText() == "Spiel aktiv..." || trigger.isRunning() == true) && wall.isRunning() == false) {

        ball_x = ball_x + dx;
        ball_y = ball_y + dy;

        if (ball_x > (screen.getWidth() - 100) || ball_x < 10) {
        dx = -dx;
        bounce = true;

    }

        if (ball_y > (screen.getHeight() - 115) || ball_y < 10) {
        dy = -dy;
        bounce = true;

    }
    }

    g.setColor(new Color(103, 104, 255));
    g.drawRect(ball_x, ball_y, 100, 100);
    g.setColor(new Color(255, 254, 102));
    g.fillOval(ball_x, ball_y, 60, 60);


    }

}

在执行中,一旦用户单击开始,便启动墙壁和触发器(球)计时器.

但是我不能让控球和挂墙计时器分开工作.墙上计时器更快,可以完成完整的重新粉刷,但我只想让它重新粉刷墙壁而不是球.

解决方案

照常添加侦听器(Maus扩展了MouseMotionAdapter),而没有任何其他线程. Event Dispather Thread用于处理鼠标事件.

使用javax.swing.Timer任意延迟只是为了改变球的位置.

I am new to Java and right now i am trying to understand Threads.

I am currently working on a "make the ball bounce or you lose" game.

In this game there is a ball that bounces of a "wall" or a "Girder".

The Wall moves with the x position of the cursor. no y movement.

Now i can't use Timer because if i do that i will have the same delay for MouseMotion and Ball Movement.

What i am trying to achieve is getting the mousemotion information with a delay of 10ms and the ball movement with a delay of 200 ms.

With Timer i have to choose either delay for both wall and ball. 10ms is great for the wall movement as it moves instantly when i move my mouse but it is way too fast for the ball movement. not even chuck norris could move the mouse so fast to let the ball bounce off the wall.

I tried creating 2 different Timers but it won't accept the mouse listener. ActionListeners are the only Listeners the Timer accepts but for some reason it still triggers the mouse motion method beside the actionPerformed method if I use a single timer with this as the parameter. But that gives me the same delay for both ball and wall.

So that is why I need threads to do it. But I have no idea how to get the mouse motion information from the MouseListener to paintComponent within a Thread.

I hope you guys understand my problem.

EDIT:

This is how i am now trying to get a wall delay of 10ms and ball delay of 200ms:

private Timer trigger = new Timer(200, this);
private Timer wall = new Timer(10, this);

in paintComponent

if ((start.getText() == "Spiel aktiv..." || trigger.isRunning() == true) || wall.isRunning() == true) {
g.setColor(new Color(255, 254, 102));
g.fillRect(x, screen.getHeight() - 15, length, 10);
}

if ((start.getText() == "Spiel aktiv..." || trigger.isRunning() == true) && wall.isRunning() == false) {

        ball_x = ball_x + dx;
        ball_y = ball_y + dy;

        if (ball_x > (screen.getWidth() - 100) || ball_x < 10) {
        dx = -dx;
        bounce = true;

    }

        if (ball_y > (screen.getHeight() - 115) || ball_y < 10) {
        dy = -dy;
        bounce = true;

    }
    }

    g.setColor(new Color(103, 104, 255));
    g.drawRect(ball_x, ball_y, 100, 100);
    g.setColor(new Color(255, 254, 102));
    g.fillOval(ball_x, ball_y, 60, 60);


    }

}

in actionPerformed i start the wall and trigger(ball) timers as soon as start is clicked by the user.

But i can't get the ball and wall timers to work seperately. wall timer is faster and does a complete repaint but i just want it to repaint the wall not the ball.

解决方案

Add your listener (the Maus extends MouseMotionAdapter) as usual without any additional threads. Event Dispather Thread is used to process mouse events.

Use javax.swing.Timer with any desired delay just to change the ball position.

这篇关于线程中的MouseMotionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆