SWT在画布上放置光标位置 [英] SWT drop cursor position on canvas
问题描述
我在JAVA中创建一个简单的绘制应用程序。我想要的是当人们点击画布,并进行拖放,一个监听器获取放置光标位置,但我不知道如何使一个下拉监听程序。当用户停止点击时,如何找到光标的位置?
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?
我有以下代码拖动:
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?
编辑:我试过这个,但它不工作:
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;
}
推荐答案
DropTargetEvent
具有包含显示相对位置的 x
和 y
DropTargetEvent
has x
and y
fields which contain the Display relative location of the cursor.
Point displayRelativeDrop = new Point(event.x, event.y);
您的 fillRectangle
必须使用相对到控件(paintC)不的显示。使用 Control.toControl(point)
从显示相对于控件相对的转换。
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.
在 drop
方法中绘制控件。只需在控件上调用 redraw
,然后在绘画监听器中绘制图形。
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.
这篇关于SWT在画布上放置光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!