加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 在jPanel周围拖动/移动形状

发布时间:2020-12-15 04:23:14 所属栏目:Java 来源:网络整理
导读:昨天我问一个关于如何绘制一个边界框以保持内部形状以及如何拖放所选形状的问题. 第一个问题解决了.但我在移动形状方面遇到了一些麻烦.是否有任何特定的转换来移动jPanel周围的形状? 我有这个代码: public boolean drag(MouseEvent e) { if(points.isEmpty
昨天我问一个关于如何绘制一个边界框以保持内部形状以及如何拖放所选形状的问题.

第一个问题解决了.但我在移动形状方面遇到了一些麻烦.是否有任何特定的转换来移动jPanel周围的形状?

我有这个代码:

public boolean drag(MouseEvent e) {
    if(points.isEmpty()) //if point's vector is empty
        return false;

    if(!selected)
        return false;

    int x = e.getX(),y = e.getX();

    if (!dragging)
        lastMovePoint.setLocation(x,y);

    dragging = true;

    int deslocX = 0;
    int deslocY = 0;

    int oldX = -1;
    int oldY = -1;

    int size = points.size();
    for(int i = 0; i < size; i++) {
        oldX = lastMovePoint.x;
        oldY = lastMovePoint.y;

        deslocX = x - oldX;
        deslocY = y - oldY;

        points.set(i,new Point(points.get(i).x + deslocX,points.get(i).y + deslocY));
//set the vector of points so that when there is a repaint() it repaints the shape with the new
//coordinates


    }
     lastMovePoint.setLocation(x,y); //set the location of the old point
    return true;
}

此方法由侦听器mouseDragged调用,并在成功时返回true.我想要做的是添加前一个拖拽点和实际值之间的差异.

当我运行此代码时,我遇到了一个问题:

形状只向右/向左,向上和向下不起作用……

.

解决方法

int x = e.getX(),y = e.getX();

这可能应该改为

int x = e.getX(),y = e.getY();

这就是为什么它只能在x方向上工作,你实际上并没有考虑Y方向

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读