Problem With ButtonDownFcn after Creating an Image Object

1 view (last 30 days)
In my project, I load a background image to the axes by using imshow. Then I create rectangle objects on the axes, which I can drag around by associating ButtonDownFcn's with them. Everything is fine until I create an image object (a small image from png file) on top of the background object. After that, when I click on an arbitrary point on the background image, sometimes the rectangle objects jumps to where I clicked. It is not the same object, but at different times, different objects jump. Moreover, it does not always happen.
How can I make sure that Matlab chooses the right object (or does not choose any if I click somewhere else).
I have read many things about hittest, putting a hold on etc, but none of them worked. (Changing NextPlot messes up the display of the objects).
Thanks in advance!
Edit:
I created a .m file for this problem. And I realized that the problem occurs when I use 'imrotate' function. Adding another image does not harm itself.
Here is my code. Please replace 'oklahoma.jpg' with a big image and 'truck.png' with a smaller one.
function GUIProblem
f=figure;
set(gcf, 'WindowButtonUpFcn',@stopDragFcn);
handles = guihandles(gcf);
handles.BckImage=fullfile(pwd,'oklahoma.jpg');
okl=imshow(imread(handles.BckImage));
handles.orgImgData=imread('truck.png','png');
handles.ImageObjs=cell(1,1);
handles.NumImageObjs=0;
pB = uicontrol('Style','pushbutton','String','Create 50 rectangles','Position',[30 30 140 60],'Callback',{@pushBtCB,handles});
pB2 = uicontrol('Style','pushbutton','String','Add Image','Position',[30 100 140 60],'Callback',{@pushBtCB2,handles});
pB2 = uicontrol('Style','pushbutton','String','Rotate Last Image','Position',[30 170 140 60],'Callback',{@pushBtCB3});
guidata(gcf, handles);
function stopDragFcn(~, ~, ~)
set(gcf,'WindowButtonMotionFcn','');
function pushBtCB(~,~,handles)
num=50;
[mapX, mapY, mapZ]=size(imread(handles.BckImage));
X=rand(num,1)*mapY;
Y=rand(num,1)*mapX;
for i=1:num
rectangle('FaceColor','r','Position', [X(i)-20/2 Y(i)-20/2 20 20],'Curvature', [1 1], 'linewidth', 1.5,'EdgeColor','r', 'ButtonDownFcn',{@requestClick} );
end
function pushBtCB2(~,~,handles)
hold on
[quad, qmap, qAlpha]=imread('truck.png','png');
obj=image([0 60], [0 60], quad,'Hittest','on');
handles.NumImageObjs=handles.NumImageObjs+1;
handles.ImageObjs{handles.NumImageObjs,1}=obj;
guidata(gcf, handles);
function pushBtCB3(~,~)
handles=guidata(gcf);
if handles.NumImageObjs>0
imgObj=handles.ImageObjs{handles.NumImageObjs,1};
CD=get(imgObj,'CData');
nCD=imrotate(CD,45,'loose');
Xd=get(imgObj,'XData');
Yd=get(imgObj,'YData');
Xmid=(Xd(2)+Xd(1))/2;
Ymid=(Yd(2)+Yd(1))/2;
width=60*length(CD)/length(handles.orgImgData);
layer=(nCD(:,:,1)==0)& (nCD(:,:,2)==0) & (nCD(:,:,3)==0);
set(imgObj,'XData',[Xmid-width/2,Xmid+width/2],'YData',[Ymid-width/2,Ymid+width/2],'CData',nCD,'AlphaData',1-layer,'AlphaDataMapping','scaled');
end
function requestClick(hObject, eventdata)
req=hObject;
mB=get(gcf,'SelectionType');
switch mB
case 'normal'
set(gcf,'WindowButtonMotionFcn', {@dragRequest,req} );
end
function dragRequest(hObject, eventdata,req)
pt=get(gca,'CurrentPoint');
set(req,'Position',[pt(1)-20/2 pt(3)-20/2 20 20]);

Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!