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


    }

}
4

2 回答 2

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

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

于 2012-05-10T12:32:23.113 回答
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 帮助

于 2012-12-12T17:01:14.077 回答