Dragable rectangle with text
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 7 Dez. 2020
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
am 7 Dez. 2020
Bearbeitet: J. Alex Lee
am 7 Dez. 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.
Antworten (1)
J. Alex Lee
am 7 Dez. 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 Kommentare
J. Alex Lee
am 8 Dez. 2020
Bearbeitet: J. Alex Lee
am 8 Dez. 2020
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
am 8 Dez. 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
Siehe auch
Kategorien
Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!