简体   繁体   English

在Java中通过单击鼠标移动球

[英]Move a ball on mouse click in Java

I'm trying to create the classic Breakout game as part of my programming assignment. 我正在尝试创建经典的Breakout游戏,并将其作为编程任务的一部分。 I have to start moving the ball on a mouse click from the user. 我必须从用户的鼠标单击开始移动球。 So I'm using a mouselistener to achieve that. 所以我正在使用鼠标监听器来实现这一目标。 The code below is just a smaller, simpler version of what I'm trying to do. 下面的代码只是我尝试做的一个更小,更简单的版本。 But it does not move the ball in gradual steps. 但是,它不会逐步移动球。 It just displays the ball at it's final position after the while loop is done executing. 在执行while循环后,它仅将球显示在其最终位置。

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

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

public class BallMoveTest extends GraphicsProgram {

    public void run() {
        ball = new GOval(20,20);
        ball.setFilled(true);
        add(ball, 100, 100);

        addMouseListeners();
    }

    public void mouseClicked(MouseEvent e) {
        while (counter < 100) {
            moveBall();
            counter++;
            pause(20);
        }
    }

    public void moveBall(){
        ball.move(2, 2);
    }

    // Private instance variables
    private GOval ball;
    private int counter = 1;
}

However this alternate code works wonderfully well, but does not allow the user to click to start the movement of the ball. 但是,此替代代码效果很好,但不允许用户单击以开始球的运动。

import acm.program.*;
import acm.graphics.*;

public class TestGOval extends GraphicsProgram {

    public void run() {
        int counter = 1;
        GOval ball = new GOval(20,20);
        ball.setFilled(true);
        add(ball,100,100);

        while (counter < 100) {
            ball.move(2, 2);
            counter++;
            pause(20);
        }

    }
}

Could someone point out what I'm doing wrong here and more importantly, why the first code block not work as intended? 有人可以指出我在这里做错了什么,更重要的是,为什么第一个代码块无法按预期工作?

PS: This is my first question, and I'm a novice at programming. PS:这是我的第一个问题,我是编程的新手。 Go easy on me if you can. 如果可以的话,对我放松。 :) :)

One conceptual solution could be to add a thread class which can access all the objects positions (at least the ball in your case). 一种概念上的解决方案是添加一个线程类,该类可以访问所有对象位置(至少在您的情况下是球)。 This thread must be able to refresh the canvas of your GraphicsProgram class. 该线程必须能够刷新GraphicsProgram类的画布。 You can give to this thread a refresh frequency of 30Hz, making it sleep for 33ms after each refresh. 您可以为该线程提供30Hz的刷新频率,使其在每次刷新后休眠33ms。 If you need more details about how to refresh your canvas you should provide us more details. 如果您需要有关如何刷新画布的更多详细信息,则应向我们提供更多详细信息。

With such solution you also need to put a sleep of 33 ms into your while loop. 使用这种解决方案,您还需要在while循环中放置33 ms的睡眠while

It may just be that you aren't showing all of your code, but you should have a class that implements MouseListener. 可能只是您没有显示所有代码,但是应该有一个实现MouseListener的类。 Just having the mouse clicked method isn't enough for java to recognise that this is your intention; 仅使用鼠标单击方法还不足以让Java识别出这是您的意图。 there's a tutorial here that has more detail: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html 这里有一个更详细的教程: http : //docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

use this code: 使用此代码:

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

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

public class BallMoveTest extends GraphicsProgram {

    public void run() {
        ball = new GOval(20,20);
        ball.setFilled(true);
        add(ball, 100, 100);

        addMouseListeners();

        waitForClick();

        animation(); 
}

public void animation() {
    while(counter<100){
       moveBall();
       pause(DELAY);
    }
}

public void moveBall(){
    ball.move(2, 2);
}

    // Private instance variables
       private GOval ball;
       private int counter = 1;
       private int DELAY=20;
}

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

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