Java mouseDragged逻辑

时间:2013-02-26 11:24:58

标签: java image mouse drag

我有一个图像加载(imagePanel),它在类的前面添加了一个mouseListener。

ImagePanel被拖动,但仅限于图像的左上角(0,0)。下面的逻辑是我尝试偏移鼠标坐标,但似乎没有效果,我相信可能是没有调用mousePressed()。

class MouseMotionHandler extends MouseMotionAdapter implements MouseListener
{
    int diffX, diffY;


    public void mousePressed(MouseEvent e)
    {
        diffX = e.getX() - imagePanel.getImageX();
        diffY = e.getY() - imagePanel.getImageY();
    }

    public void mouseDragged(MouseEvent e) 
    {                           
        imagePanel.setX(e.getX()-diffX);
        imagePanel.setY(e.getY()-diffY);
        imagePanel.repaint();
    }
    @Override
    public void mouseExited(MouseEvent e) {
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    }
}

任何帮助都会很棒。

编辑:已解决

问题出在我初始化监听器的地方。 mousePressed()是MouseListener类的一部分,mouseDragged()是MouseMotionListener类的一部分。所以我需要为每个人添加单独的监听器:

MouseMotionHandler mouseMotionHandler = new MouseMotionHandler();
imagePanel.addMouseListener(mouseMotionHandler);
imagePanel.addMouseMotionListener(mouseMotionHandler);

谢谢, 千斤顶

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用Transfer Handler? 我成功地使用了它

JTable示例

tbItens.setTransferHandler(new TransferHandler(){

    @Override
     protected Transferable createTransferable(JComponent c)
     {
         JTable table= (JTable)c;
         int row = table.getSelectedRow();
         return new StringSelection(String.valueOf(row));
     }

    @Override
    public int getSourceActions(JComponent c)
    {
        return TransferHandler.COPY;
    }



    @Override
    public boolean canImport(TransferHandler.TransferSupport support)
    {
        if(!support.isDrop())
            return false;

        if (!support.isDataFlavorSupported(DataFlavor.stringFlavor))
                return false;

        boolean copySupported = (COPY & support.getSourceDropActions()) == COPY;
            if (copySupported) {
                support.setDropAction(COPY);
                return true;
            }
        return false;
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport support)
    {
        if(!this.canImport(support))
            return false;
        JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();
        int row = dl.getRow();


        String data;
        try
        {
            data = (String)support.getTransferable().getTransferData(DataFlavor.stringFlavor);
        }
        catch (Exception e)
        {
            return false;
        }


        if(data == null || data.isEmpty())
            return false;

        //row = para onde vai
        //i é aonde estava
        int i = Integer.valueOf(data);

        ArrayList<Object> lo = new ArrayList<Object>();
        for(int k=0; k< tbItens.getRowCount(); k++)
        {
            Object v = tbItens.getValueAt(k, 0);
            lo.add(v);
        }
        DefaultTableModel dt = (DefaultTableModel) tbItens.getModel();
        while(dt.getRowCount() != 0)
            dt.removeRow(0);

        Object item = lo.remove((int)i);
        lo.add(row, item);

        for(Object o : lo)
            dt.addRow(new Object[]{o});
        return true;
    }

});

答案 1 :(得分:0)

看一下this,它给出了一个使用鼠标移动监听器的例子。