SWT drop cursor position on canvas

我只是一个虾纸丫 提交于 2019-12-12 04:17:57

问题


I'm making a simple "paint" application in JAVA. I would have wanted that when the person clicks the canvas, and make a drag and drop, a listener get the drop cursor location, but I don't find how make a drop listener. How can I find the location of the cursor when the user stop his click?

I have the following code for the drag :

    Canvas paintC = new Canvas(shell, SWT.NONE);
    paintC.addDragDetectListener(new DragDetectListener() {
        public void dragDetected(DragDetectEvent arg0) {
            Point controlRelativePos = new Point(arg0.x, arg0.y);

            displayRelativePos1 = paintC.toDisplay(controlRelativePos);

            GC gc = new GC(paintC);

            gc.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
            gc.fillRectangle(arg0.x, arg0.y, 90, 60);

        }
    });

Should I a drag function in order to get the latest position?

Edit: I've tried this, but it didn't work :

dropTarget.addDropListener(new DropTargetAdapter() {
        @Override
        public void drop(DropTargetEvent event) {
            displayRelativePos2 = dropTarget.getDisplay().getCursorLocation();

            hauteur = displayRelativePos2.y - displayRelativePos1.y;
            largeur = displayRelativePos2.x - displayRelativePos1.x;

            GC gc = new GC(paintC);
            gc.setBackground(SWTResourceManager.getColor(SWT.COLOR_RED));
            gc.fillRectangle(displayRelativePos1.x, displayRelativePos1.y, largeur, hauteur);

            nbFormesAff = nbFormes +1;
            forme = "Rectangle" + nbFormesAff;
            pos = displayRelativePos1.x + ", " + displayRelativePos1.y +"\nhauteur:" + hauteur +" largeur:"+ largeur;

        }

回答1:


DropTargetEvent has x and y fields which contain the Display relative location of the cursor.

Point displayRelativeDrop = new Point(event.x, event.y);

Your fillRectangle must use points which are relative to the Control (paintC) not the display. Use Control.toControl(point) to convert from display relative to control relative.

You should also not try to draw the control in the drop method. Just call redraw on the control and do the drawing in a paint listener.



来源:https://stackoverflow.com/questions/34581087/swt-drop-cursor-position-on-canvas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!