【问题标题】:Drag/Moving a shape around jPanel在 jPanel 周围拖动/移动形状
【发布时间】:2018-02-22 21:27:42
【问题描述】:

昨天我问了一个问题,关于如何绘制一个边界框来容纳一个形状,以及如何拖放选定的形状

第一个问题解决了。但是我在移动形状时遇到了一些麻烦。是否有任何特定的转换可以在 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。我想要做的是添加前一点的拖动和实际之间的差异。

当我运行这段代码时,我遇到了问题:

形状只能向右/向左,上下不起作用...

.

【问题讨论】:

  • 请考虑创建和发布您的minimal example program,这是一个我们可以编译、测试的小型测试程序,它可以向我们展示您的问题,并且我们可以尝试提供帮助你改进。还有为什么是 AWT Canvas 而不是 Swing JPanel?
  • 是JPanel 是的。我已经编辑了。对不起

标签: java swing awt


【解决方案1】:

是的,为了拖动组件,您还需要知道起始位置,以便计算鼠标移动的距离。

这里有一些代码显示了移动窗口的一般行为。

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class MoveWindow extends MouseInputAdapter
{
    Point location;
    MouseEvent pressed;

    public void mousePressed(MouseEvent me)
    {
        pressed = me;

    }

    public void mouseDragged(MouseEvent me)
    {
        Component component = me.getComponent();
        location = component.getLocation(location);
        int x = location.x - pressed.getX() + me.getX();
        int y = location.y - pressed.getY() + me.getY();
        component.setLocation(x, y);
     }

    private static void createAndShowGUI()
    {
        JWindow window = new JWindow();
        window.setSize(300, 300);
        window.setLocationRelativeTo( null );
        window.setVisible(true);

        MouseInputAdapter listener = new MoveWindow();
        window.addMouseListener( listener );
        window.addMouseMotionListener( listener );

    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

我会让你为你的目的实现它。

【讨论】:

  • 感谢您的观点。有时问题不在于编程,而在于数学。接下来我必须旋转,调整大小(就像油漆中的形状)。我的老师说使用矩阵的操作,这是最好的方法吗?
【解决方案2】:
int x = e.getX(), y = e.getX();

这应该改成

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

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

【讨论】:

  • 哦!我改变了,现在效果更糟了......它左右移动非常缓慢,而且不是流畅的方式。
  • 如果向上/向下拖动鼠标,形状会完美地向右/向左移动,就像向右/向左拖动一样。
  • 问题解决了,我的代码中还有其他错误。很抱歉浪费了时间,但我希望这段代码可以帮助别人,所以我不会删除它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
相关资源
最近更新 更多