我想移动GRect。我使用了mouseMotionListener,但它不起作用。检查代码,并尝试提供帮助。谢谢

时间:2013-06-30 20:38:02

标签: java awt mouse-listeners mousemotionevent

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);
    }

}

1 个答案:

答案 0 :(得分:0)

使用addMouseMotionListeners而不是addMouseMotionListenerMouseListeners

注册已实施的GraphicsProgram
addMouseMotionListeners();