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

import java.awt.Color;
import java.awt.event.*;

public class TestingClass2 extends GraphicsProgram implements MouseMotionListener{

    //dimensions of play board
    private static final int boardWidth = 402;
    private static final int boardHeight = 600;

    //paddle
    private static final int paddleWidth = 60;
    private static final int paddleHeight = 10;
    private double xPosition;
    GRect paddle;

    public void run(){
        setSize(boardWidth, boardHeight);
        setPaddle();
        addMouseMotionListener(this); 
    }

    public void mouseDragged(MouseEvent e){
        xPosition = e.getX();
        if(xPosition <= 0 && xPosition <= boardWidth - paddleWidth){
        paddle.setLocation(xPosition, 580);
        }
    }

    public void mouseMoved(MouseEvent e){

    }

    public void setPaddle(){
        paddle = new GRect(boardWidth / 2 - 30.0, 580, paddleWidth, paddleHeight);
        paddle.setFillColor(Color.BLACK);
        paddle.setFilled(true);
        add(paddle);
    }

}
4

1 回答 1

0

使用addMouseMotionListeners而不是addMouseMotionListener注册MouseListeners实现GraphicsProgram

addMouseMotionListeners();
于 2013-06-30T23:55:48.507 回答