Dragable rectangle with text

10 次查看(过去 30 天)
keith tan
keith tan 2020-12-7
Hi, im trying to make a rectangle that is labelled which can be dragged. However i only managed to do it with annotations, and want to change it to dragging with a rectangle. How do i do it?
This is my code:
function drag_drop
dragging = [];
orPos = [];
f = figure('WindowButtonUpFcn',@dropObject,'units','normalized','WindowButtonMotionFcn',@moveObject);
a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','StudyTable','ButtonDownFcn',@dragObject);
a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','CoffeeTable','ButtonDownFcn',@dragObject);
a = annotation('textbox','position',[0.35, 0.35, 0.35, 0.35],'String','Room','ButtonDownFcn',@dragObject);
rectangle('textbox','position',[0.35, 0.35, 0.35, 0.35],'String','Room','ButtonDownFcn',@dragObject);
function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end
function dropObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
set(dragging,'Position',get(dragging,'Position') + [posDiff(1:2) 0 0]);
end
end
end
  4 个评论
Rik
Rik 2020-12-7
It only seems that way because of the lack of proper indentation. All functions are nested functions within the main function, so they actually share some variables. I would prefer a more explicit method of sharing handles, but this should work.
J. Alex Lee
J. Alex Lee 2020-12-7
编辑:J. Alex Lee 2020-12-7
well, i actually tried to run the code...the problem is not implementation of drag-and-drop...the problem is about endowing a rectangle class with some text. [edit] removed code that doesn't answer the OP, and made answer below.

请先登录,再进行评论。

回答(1 个)

J. Alex Lee
J. Alex Lee 2020-12-7
Ugly, but minimal changes to original code:
function drag_drop
close all
dragging = [];
orPos = [];
f = figure('WindowButtonUpFcn',@dropObject,'units','normalized','WindowButtonMotionFcn',@moveObject);
% a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','StudyTable','ButtonDownFcn',@dragObject);
% a = annotation('textbox','position',[0.1, 0.1, 0.1, 0.1],'String','CoffeeTable','ButtonDownFcn',@dragObject);
% a = annotation('textbox','position',[0.35, 0.35, 0.35, 0.35],'String','Room','ButtonDownFcn',@dragObject);
tmp = text(0.35,0.35,'Room','VerticalAlignment','bottom');
a = rectangle('position',[0.35, 0.35, 0.35, 0.35],'EdgeColor','r','ButtonDownFcn',@dragObject,'UserData',tmp);
tmp = text(0.1,0.1,'StudyTable','VerticalAlignment','bottom');
b = rectangle('position',[0.1, 0.1, 0.1, 0.1],'EdgeColor','r','ButtonDownFcn',@dragObject,'UserData',tmp);
tmp = text(0.1,0.1,'CoffeeTable','VerticalAlignment','bottom');
c = rectangle('position',[0.1, 0.1, 0.1, 0.1],'EdgeColor','r','ButtonDownFcn',@dragObject,'UserData',tmp);
set(gca,'XLim',[0,1],'YLim',[0,1])
function dragObject(hObject,eventdata)
dragging = hObject;
orPos = get(gcf,'CurrentPoint');
end
function dropObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
rectpos = get(dragging,'Position') + [posDiff(1:2) 0 0];
set(dragging,'Position',rectpos);
set(dragging.UserData,'Position',rectpos(1:3));
dragging = [];
end
end
function moveObject(hObject,eventdata)
if ~isempty(dragging)
newPos = get(gcf,'CurrentPoint');
posDiff = newPos - orPos;
orPos = newPos;
rectpos = get(dragging,'Position') + [posDiff(1:2) 0 0];
set(dragging,'Position',rectpos);
set(dragging.UserData,'Position',rectpos(1:3));
end
end
end
  3 个评论
J. Alex Lee
J. Alex Lee 2020-12-8
编辑:J. Alex Lee 2020-12-8
hmm, you ran the above code and it failed? what version of matlab are you running? i ran on 2020b.
By the way you have to grab the edge of the rectangles. I just confirmed that if you specify a FaceColor, you can grab anywhere in the rectangle.
J. Alex Lee
J. Alex Lee 2020-12-8
I think to do anything more sophisticated you will want to organize your box+labels into some class and treat the thing as an "app", which will draw on OOP concepts.
WIth rectangles I bet you can only rotate 90 degress at a time, which would amount to just inverting the height/width, but then you need to make decisions like which corner of the box wil be your reference point for positioning.
If you want general angles, look into patch() or fill() maybe

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by