2012-05-10 116 views
1

我們正在爲我們的項目創建一些圖形應用程序。我們在項目中使用Draw2d和GEF技術。我們必須在繪圖區繪製一個矩形。功能如下。繪圖中的矩形問題

  1. 使用單擊工具欄上的矩形按鈕。
  2. 將光標更改爲十字光標符號。
  3. 當用戶點擊區域並拖動鼠標時,它會根據鼠標的拖動顯示矩形。

直到現在它工作正常。現在的問題是,我們已經放大和縮小功能。 當使用放大和繪製矩形時,它不會到達所需的位置。 它正在區域的下方。然後用戶必須滾動並看到矩形。只有當我們使用放大和縮小時,纔會出現此問題。

如何解決這個問題?請參閱下面的代碼。

package draw2dview; 

import org.eclipse.draw2d.ColorConstants; 
import org.eclipse.draw2d.FigureCanvas; 
import org.eclipse.draw2d.FreeformLayout; 
import org.eclipse.draw2d.FreeformViewport; 
import org.eclipse.draw2d.IFigure; 
import org.eclipse.draw2d.LightweightSystem; 
import org.eclipse.draw2d.MouseEvent; 
import org.eclipse.draw2d.MouseListener; 
import org.eclipse.draw2d.RectangleFigure; 
import org.eclipse.draw2d.ScalableFigure; 
import org.eclipse.draw2d.ScalableFreeformLayeredPane; 
import org.eclipse.draw2d.ToolbarLayout; 
import org.eclipse.draw2d.XYLayout; 
import org.eclipse.draw2d.geometry.Point; 
import org.eclipse.draw2d.geometry.Rectangle; 
import org.eclipse.gef.DefaultEditDomain; 
import org.eclipse.gef.EditDomain; 
import org.eclipse.gef.EditPart; 
import org.eclipse.gef.EditPartFactory; 
import org.eclipse.gef.GraphicalViewer; 
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart; 
import org.eclipse.gef.editparts.ScalableRootEditPart; 
import org.eclipse.gef.editparts.ZoomManager; 
import org.eclipse.gef.ui.actions.ZoomInAction; 
import org.eclipse.gef.ui.actions.ZoomOutAction; 
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer; 
import org.eclipse.jface.action.Action; 
import org.eclipse.jface.action.IToolBarManager; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.ui.IActionBars; 
import org.eclipse.ui.part.ViewPart; 

public class View extends ViewPart implements org.eclipse.swt.events.MouseListener { 
    public static final String ID = "draw2dview.view"; 



    private Action drawAction; 
    private ScalableFreeformLayeredPane root; 
    ScalableRootEditPart editPart = null ; 
    private XYLayout layout; 
    private ZoomManager zoomManager ; 

    EditDomain editDomain = new DefaultEditDomain(null); 
    GraphicalViewer graphicalViewer = new ScrollingGraphicalViewer(); 
    ScalableFreeformRootEditPart rootEditPart = new ScalableFreeformRootEditPart(); 



    private FigureCanvas createContents(Composite parent){ 



     root = new ScalableFreeformLayeredPane(); 
     zoomManager = new ZoomManager(root,new FreeformViewport()); 
     root.setFont(parent.getFont()); 
     //layout = new XYLayout(); 

     layout= new FreeformLayout(); 
     root.setLayoutManager(layout); 




     FigureCanvas figureCanvas = new FigureCanvas(parent,SWT.DOUBLE_BUFFERED); 
     figureCanvas.addMouseListener(this); 
     figureCanvas.setBackground(ColorConstants.white); 
     LightweightSystem lws = new LightweightSystem(figureCanvas); 
     lws.setContents(root); 
     return figureCanvas ; 

    } 




    private IFigure createPersonFigure() { 
     RectangleFigure rectangleFigure = new RectangleFigure(); 
     rectangleFigure.setBackgroundColor(ColorConstants.blue); 
     rectangleFigure.setLayoutManager(new ToolbarLayout()); 
     rectangleFigure.setPreferredSize(100, 100); 
     return rectangleFigure ; 
    } 




    /** 
    * This is a callback that will allow us to create the viewer and initialize 
    * it. 
    */ 
    public void createPartControl(Composite parent) { 

    /* graphicalViewer.createControl(parent); 
     editDomain.addViewer(graphicalViewer); 
     graphicalViewer.setRootEditPart(rootEditPart);*/ 

     createContents(parent); 
     createAction(); 
     contributeToActionBars(); 
    } 

    private void contributeToActionBars() { 
     IActionBars bars = getViewSite().getActionBars(); 

     addToToolBar(bars.getToolBarManager()); 
    } 


    private void addToToolBar(IToolBarManager toolBarManager2){ 
     toolBarManager2.add(drawAction); 
     toolBarManager2.add(new ZoomInAction(zoomManager)); 
     toolBarManager2.add(new ZoomOutAction(zoomManager)); 


    } 

    private void createAction() { 
     drawAction = new Action() { 
      public void run() { 
       System.out.println("execued.."); 


      } 
     }; 
     drawAction.setText("Draw"); 
     drawAction.setImageDescriptor(Activator.getImageDescriptor("icons/alt_window_16.gif")); 
    } 

    /** 
    * Passing the focus request to the viewer's control. 
    */ 
    public void setFocus() { 
    // viewer.getControl().setFocus(); 
    } 




    @Override 
    public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) { 


    } 




    @Override 
    public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 

     System.out.println("inside..Mousedeown:: "+e.x+","+e.y); 
     IFigure personFigure = createPersonFigure();     
     root.add(personFigure); 
     layout.setConstraint(personFigure, new Rectangle(new Point(e.x,e.y),personFigure.getPreferredSize())); 
     //layout.setConstraint(personFigure, new Rectangle(new Point(e.x,e.y),personFigure.getPreferredSize())); 
    } 




    @Override 
    public void mouseUp(org.eclipse.swt.events.MouseEvent e) { 


    } 

} 
+0

的問題可能是由你實現了變焦的方式莖 - 你需要發佈一些代碼,如果你想有一個明確的答案 –

+0

您好感謝您的回覆。我已經添加了代碼。我已經使用GEF縮放管理器進行放大和縮小。 – Parameswaran

回答

0

您需要調整您的鼠標事件,根據目前的縮放管理是使用縮放級別的座標。鼠標事件是絕對像素,但是您的ZoomManger會將縮放因子應用於您的圖形座標。我認爲你還需要考慮你的ViewPort的客戶區。

首先,當你constuct你ZoomManager在視口中通過從FigureCanvas:

zoomManager = new ZoomManager(root, figureCanvas.getViewPort()); 

然後嘗試類似:

double scaleFactor = zoomManager.getZoom(); 
Rectangle r = figureCanvas.getViewport().getClientArea(); 
layout.setConstraint(personFigure, new Rectangle(new Point((e.x + r.x) * scaleFactor,(e.y + r.y) * scaleFactor),personFigure.getPreferredSize())); 

這可能需要調整,以得到它的權利,讓我知道...

+0

不,它不工作。我可以收到你的電子郵件嗎?我想問你一些疑惑!!!如果你不介意的話。 – Parameswaran

+0

我寧願把它放在堆棧上。新代碼是否改進了一些問題?現在至少現在必須以不同的方式被打破? –

0

您必須從鼠標事件獲取的絕對座標轉換爲相對座標,轉換爲人物圖形的父級:

@Override 
public void mouseDown(org.eclipse.swt.events.MouseEvent e) { 
    System.out.println("inside..Mousedeown:: "+e.x+","+e.y); 
    IFigure personFigure = createPersonFigure();     
    root.add(personFigure); 
    Point p = new PrecisionPoint(e.x,e.y); 
    personFigure.translateToRelative(p); 
    layout.setConstraint(personFigure, new Rectangle(p,personFigure.getPreferredSize())); 
} 

欲瞭解更多信息,看看draw2d help