是这样的,我看的斯坦福cs106a自学的,用的acm的package,然后在编写一个弹球的程序时候,程序无法响应我鼠标的拖拽。我尝试着把弹球动画的部分去掉,也就是shootBall()函数,小板pad是可以拖动的,然而加了动画就不行了。下面第一个程序就是我的程序。另外还有另一个网上的前辈写的程序,虽然弹球的一些设计感觉不太合理,但他的小板可以拖动,和我的程序很接近,找了很多天的问题了,希望能得到解答。【我的程序】
import acm.graphics.*;
import acm.program.*;
import acm.util.*;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
 * 1.无法解决小球单个角触碰的反应问题;
 * 2.pad无法拖动;
 * 3.小球的两个角碰到两个对象时卡死;
 */
public class MainProg extends GraphicsProgram {

public void run(){
initBricks(); initPad(); initBall();
        for (int i = 0; i < 3; i += 1) {
         shootBall();
         if (isWin) break;
        }

timeLine = 0;
addMouseListeners();
}


//to confirm if the ball is dead;
public boolean isDead() {
if (Ball.getY() > getHeight() - BALL_DIAMETER) {
return true;
} else return false;
}

//to confirm if the ball contacts the left or right boundary
public boolean isLRContacted() {
if ((getElementAt(cPoint[0]) != null && getElementAt(cPoint[2]) != null) || 
(getElementAt(cPoint[1]) != null && getElementAt(cPoint[3]) != null)) {
return true;
}
return false;
}

//to confirm if the ball contacts the upper or downer boundary
public boolean isUDContacted() {
if ((getElementAt(cPoint[0]) != null && getElementAt(cPoint[1]) != null) || 
(getElementAt(cPoint[2]) != null && getElementAt(cPoint[3]) != null)) {
return true;
}
return false;
}

//to confirm if the ball contacts the left or right boundary
public boolean isLRBContacted() {
if (Ball.getX() <= 0 || Ball.getX() >= getWidth() - BALL_DIAMETER) {
return true;
}
return false;
}

//to confirm if the ball contact the pad or the bricks;
public GObject getContactObj() {
cPoint = new GPoint[4];
cPoint[0] = new GPoint(Ball.getX(), Ball.getY());
cPoint[1] = new GPoint(Ball.getX() + BALL_DIAMETER, Ball.getY());
cPoint[2] = new GPoint(Ball.getX(), Ball.getY() + BALL_DIAMETER);
cPoint[3] = new GPoint(Ball.getX() + BALL_DIAMETER, Ball.getY() + BALL_DIAMETER);
for (int i = 0; i < 4; i += 1) {
if (cPoint[i] != null){
return getElementAt(cPoint[i]);
}
}
return null;
} //how the ball is shot off;【problem here】
private void shootBall() {
int n = 0; //record number of eliminated bricks;
int m = 0; //record number of lose;

double x = (getWidth() - BALL_DIAMETER) / 2;
double y = getHeight() - BREAKOUT_GAP - BALL_DIAMETER;
GOval ball = new GOval(BALL_DIAMETER, BALL_DIAMETER);
Ball = ball;
add(ball, x, y);
//initial velocity
vx = 1;
vy = -3.0;
//if(rgen.nextBoolean(0.5)) vx = -vx;
//four corner sensors of the ball

while(true) {



//basic ball moving
ball.move(vx, vy);
pause(PAUSE_TIME);

GObject obj = getContactObj();
//bounce when touching the boundaries or pad;
if (isLRBContacted()) {
vx = -vx;
} else if (ball.getY() <= 0 || obj == pad) 
vy = -vy;

//when the ball die;
else if (isDead()) {
remove(ball); initBall(); 
m += 1;
pause(2000);
break;


//when the ball hits the bricks
else if (isLRContacted()) {
vx = -vx;
remove(obj);
n += 1;
} else if (isUDContacted()) {
vy = -vy;
remove(obj);
n += 1;
}

//if lose;
if (m == 3) {
GLabel sayLose = new GLabel("YOU LOSE!");
add(sayLose, (getWidth() - sayLose.getWidth())/2, getHeight()/2);
break;
}

//if win;
if (n == BRICK_LINES * BRICK_COLUMNS) {
GLabel sayWin = new GLabel("YOU WIN!");
add(sayWin, (getWidth() - sayWin.getWidth())/2, getHeight()/2);
isWin = true;
break;
}
}
/*Thread ballThread = new Thread(BallMove);
ballThread.start();*/

}

/*start dragging pad*/
public void mousePressed(MouseEvent e) {
last = new GPoint(e.getPoint());
}

/*dragging the pad*/
public void mouseDragged(MouseEvent e) {
if ((e.getX() - last.getX() > 0 || pad.getX() >= 0) && (e.getX() - last.getX() < 0 || pad.getX() + pad.getWidth() <= getWidth())) {
pad.move(e.getX() - last.getX(), 0);
last = new GPoint(e.getPoint());
}
}
/*initialize the brick array*/
private void initBricks() {
double y = BREAKOUT_GAP;
for (int i = 0;i < BRICK_LINES;i += 1) {
double x = (getWidth() - BRICK_COLUMNS * BRICK_LENGTH 
- (BRICK_COLUMNS - 1) * GAP) / 2; 
for (int j = 0;j < BRICK_COLUMNS;j += 1) {
GRect brick = new GRect(BRICK_LENGTH, BRICK_WIDTH);
brick.setFilled(true);
if(i < 2) {
brick.setColor(Color.red);
} else if (i < 4) {
brick.setColor(Color.ORANGE);
} else if (i < 6) {
brick.setColor(Color.YELLOW);
} else if (i < 9) {
brick.setColor(Color.GREEN);
}
add(brick, x, y);
x += BRICK_LENGTH + GAP;
}
y += BRICK_WIDTH + GAP;
}
} /*initialize the pad*/
private void initPad() {
double x = (getWidth() - PAD_LENGTH) / 2;
double y = getHeight() - BREAKOUT_GAP;
pad = new Pad(PAD_LENGTH, PAD_WIDTH);
add(pad, x, y);
}

public class Pad extends GRect { public Pad(double length, double width) {
super(length, width);
this.setFilled(true);
}

}

/*initialize the ball*/
private void initBall() { }

/*IVars*/
private GOval Ball;
private GPoint last;
private GRect pad;
private int timeLine;
/*parameters of the ball */
//velocity
private double vx;
private double vy;
//sensor
private GPoint[] cPoint;

private boolean isWin = false;

/*random generator*/
private RandomGenerator rgen = RandomGenerator.getInstance();

/*constant values for brick array*/
private static final int BRICK_LINES   = 9;
private static final int BRICK_COLUMNS = 10;
private static final double BRICK_LENGTH = 16;
private static final double BRICK_WIDTH  = 4;
private static final double GAP          = 0;
private static final double BREAKOUT_GAP = 30;
/*constant values for pad*/
private static final double PAD_LENGTH = 40;
private static final double PAD_WIDTH  = 6;
/*constant values for ball*/
private static final double BALL_DIAMETER  = 6;
/*constant value for pause time*/
private static final double PAUSE_TIME  = 50;

}

解决方案 »

  1.   

    是这样的,如果我删掉那个带有延时的动画代码,也就是shootBall(),那小板是可以拖动的,所以个人认为鼠标监听应该没有问题,你说 的这个可能在父类里已经做过这个事情了
      

  2.   

    既然你知道有差别 可以只看一下有差别的地方就好了嘛.. 
    主要是..好绿.. 不太想看了有一个建议是debug一下  看一看你的延时代码是不是导致坐标计算出了问题 
      

  3.   


    在鼠标事件的地方设置断点然后debug的时候点击拖动却也不反应。。
    对比了几次但是找不出问题所在。。
    如果可以的话能可以帮我看下shootBall()方法
      

  4.   


    在鼠标事件的地方设置断点然后debug的时候点击拖动却也不反应。。
    对比了几次但是找不出问题所在。。
    如果可以的话能可以帮我看下shootBall()方法就是看不下去呢... 这样提问很难回答
    你的代码只有你自己有, 只能是你自己来看. 
     而且也不知道你要的效果是什么样的. 或者你把你做的东西截个图解释一下..