Dragable rectangle with text

조회 수: 14 (최근 30일)
keith tan
keith tan 2020년 12월 7일
댓글: J. Alex Lee 2020년 12월 8일
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