2012-07-18 21 views
1

我正在使用Draw2d庫(不使用GEF)處理小型圖形編輯器。一個要求是,你可以通過拖動鼠標來移動圖形。只要數字之間沒有(折線)連接,這就可以正常工作。當我添加連接時,所有渲染都正確,但不可能移動數字。如何將數字作爲連接的一部分時移動

下面是一個代碼示例,說明此問題:

public class Main { 

    public static void main(String[] args) { 
     Display d = new Display(); 
     final Shell shell = new Shell(d); 
     shell.setSize(400, 400); 
     shell.setText("Draw2d Test"); 
     LightweightSystem lws = new LightweightSystem(shell); 
     Figure contents = new Figure(); 
     XYLayout contentsLayout = new XYLayout(); 
     contents.setLayoutManager(contentsLayout); 

     // create figures 
     Figure f1 = new TestFigure("Test 1"); 
     Figure f2 = new TestFigure("Test 2"); 

     MouseManager mm = new MouseManager(); 

     // register mouse listeners 
     f1.addMouseMotionListener(mm); 
     f1.addMouseListener(mm); 
     f2.addMouseMotionListener(mm); 
     f2.addMouseListener(mm); 

     // set constraints to layout manager 
     contentsLayout.setConstraint(f1, new Rectangle(10, 10, -1, -1)); 
     contentsLayout.setConstraint(f2, new Rectangle(200, 200, -1, -1)); 

     // add to layout manager 
     contents.add(f1); 
     contents.add(f2); 

     // add connection 
     // When uncommenting these lines, dragging works fine 
     PolylineConnection c = new PolylineConnection(); 
     c.setSourceAnchor(new ChopboxAnchor(f1)); 
     c.setTargetAnchor(new ChopboxAnchor(f2)); 
     c.setConnectionRouter(new ManhattanConnectionRouter()); 
     contents.add(c); 

     lws.setContents(contents); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      while (!d.readAndDispatch()) { 
       d.sleep(); 
      } 
     } 
    } 
} 

class MouseManager implements MouseMotionListener, MouseListener { 
    Figure selection; 
    private Point lastDragLocation; 

    @Override 
    public void mousePressed(MouseEvent me) { 
     System.out.println("mouse pressed"); 
     selection = (Figure) me.getSource(); 
    } 

    @Override 
    public void mouseReleased(MouseEvent me) { 
     System.out.println("mouse released"); 
     selection = null; 
     lastDragLocation = null; 
    } 

    @Override 
    public void mouseDragged(MouseEvent me) { 
     if (lastDragLocation != null && selection != null) { 
      int offsetX = me.getLocation().x - lastDragLocation.x; 
      int offsetY = me.getLocation().y - lastDragLocation.y; 
      int newX = selection.getLocation().x + offsetX; 
      int newY = selection.getLocation().y + offsetY; 
      System.out.println(String.format("NewX: %d, NewY: %d", newX, newY)); 
      selection.setBounds(selection.getBounds().getTranslated(offsetX, 
        offsetY)); 

     } 
     lastDragLocation = me.getLocation(); 
    } 

    // [removed empty implementations of the interface for this post] 
} 

class TestFigure extends RectangleFigure { 
    public Color classColor; 

    public TestFigure(String name) { 
     ToolbarLayout layout = new ToolbarLayout(); 
     setLayoutManager(layout); 
     setOpaque(true); 

     classColor = new Color(null, 255, 255, 206); 
     setBackgroundColor(classColor); 

     Label lbl_name = new Label(name); 
     add(lbl_name); 
    } 

    @Override 
    protected void finalize() throws Throwable { 
     classColor.dispose(); 
     super.finalize(); 
    } 
} 

有沒有人有一個想法如何使拖動時可能存在(它時並不需要渲染的拖動兩個數字之間的連接連接)?

回答

1

兩個問題:

  1. mouseDragged功能要更改Figure的界限,而不是改變在父容器圖的約束。
  2. 您不重新驗證父項。

我做了如下的變化和它的工作原理:

public void mouseDragged(MouseEvent me) { 
    if(lastDragLocation != null && selection != null) { 
    int offsetX = me.getLocation().x - lastDragLocation.x; 
    int offsetY = me.getLocation().y - lastDragLocation.y; 
    int newX = selection.getLocation().x + offsetX; 
    int newY = selection.getLocation().y + offsetY; 
    System.out.println(String.format("NewX: %d, NewY: %d", newX, newY)); 
    // selection.setBounds(selection.getBounds().getTranslated(offsetX, offsetY)); <-- this does not work 
    selection.getParent().getLayoutManager() 
     .setConstraint(selection, selection.getBounds().getTranslated(offsetX, offsetY)); 
    selection.getParent().revalidate(); 

    } 
    lastDragLocation = me.getLocation(); 
} 

但我仍然認爲這是與實施問題,因爲如果你把鼠標移動速度太快不知爲何,你能設法讓開圖,它停止移動。我會做的是聽父母圖中的鼠標,捕捉鼠標開始移動到內部圖形的頂部(使用父母Figure.findFigureAt()),然後隨着鼠標移動移動內部圖形。

相關問題