使用mouseDragged方法拖动Rectangle以使用mousePressed创建已创建的Rect

时间:2018-11-07 19:58:58

标签: java swing awt mouselistener mousemotionlistener

我想用鼠标单击将创建的矩形拖动到画布中。但是它一直在创建新的矩形,然后将所有的矩形拖在一起,就像我所附的图片一样。有想法吗?

    public class ModelClass {
    public static Image mCanvas = null;

protected void initCanvas() {
        mCanvas = new BufferedImage(mWidth, mHeight, BufferedImage.TYPE_INT_RGB);
        }

        public void repaintRect(int _x, int _y) {
            if (mCanvas == null) {
                initCanvas();
            }

             graph = mCanvas.getGraphics();
             graph.setColor(Color.blue);
             graph.drawRect(_x, _y, 30, 30);
             update(mCanvas);

        }

-------------------控制器类-------------------

@Override
        public void mousePressed(MouseEvent _event) {
mStartingPoint = _event.getPoint();
mModel.repaintRect((int)mStartingPoint.getX(), (int)mStartingPoint.getY());}

@Override
        public void mouseDragged(MouseEvent _event) {
            Point pt = _event.getPoint();
            mModel.repaintRect((int)pt.getX(), (int)pt.getY());

        }

当我尝试拖动创建的矩形时。一直在绘图和拖动...

enter image description here

0 个答案:

没有答案