在绘图问题中绘制矩形

时间:2012-05-10 05:36:19

标签: swt eclipse-rcp eclipse-gef draw2d

我们正在为我们的项目创建一些图形应用程序。我们在项目中使用Draw2d和GEF技术。我们必须在绘图区域绘制一个矩形。功能如下。

  1. 使用单击工具栏中的矩形按钮。
  2. 将光标更改为Cross curson symbol。
  3. 当用户单击该区域并拖动鼠标时,它会根据拖动鼠标显示矩形。
  4. 直到现在它工作正常。现在的问题是,我们有放大和缩小功能。 当使用放大并绘制矩形时,它不会进入所需位置。 它位于区域的下方。然后用户必须滚动并查看矩形。仅当我们使用放大和缩小时才会出现此问题。

    如何解决此问题?请参阅下面的代码。

    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) {
    
    
        }
    
    }
    

2 个答案:

答案 0 :(得分:0)

您需要根据缩放管理器当前使用的缩放级别缩放鼠标事件坐标。鼠标事件是绝对像素,但ZoomManger会导致比例因子应用于图形的坐标。我认为您还需要考虑ViewPort的客户区域。

首先,当您从FigureCanvas构建ViewPort中的ZoomManager传递时:

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()));

这可能需要调整以使其正确,让我知道....

答案 1 :(得分: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