MATLAB Answers

Dragable rectangle with text

21 views (last 30 days)
keith tan
keith tan on 7 Dec 2020
Commented: J. Alex Lee on 8 Dec 2020
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 Comments
J. Alex Lee
J. Alex Lee on 7 Dec 2020
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.

Sign in to comment.

Answers (1)

J. Alex Lee
J. Alex Lee on 7 Dec 2020
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 Comments
J. Alex Lee
J. Alex Lee on 8 Dec 2020
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by