How to make an image appear in different locations
1 次查看(过去 30 天)
显示 更早的评论
I want to do this , can someone help please,
expl: a ball (or an image) appears in random locations on the screen, when it appears(each time in different place), the user must click with the mouse , when he click the image disapear
thank you for your help
8 个评论
Rik
2022-8-26
Deleted thread:
How to make an image appear in different locations
I want to do this , can someone help please,
expl: a ball (or an image) appears in random locations on the screen, when it appears(each time in different place), the user must click with the mouse , when he click the image disapear
thank you for your help
OP on 12 Jul 2022
Thank you for your answer,
when the image is displayed, how can the user press to the mouse to disappears ?
OP on 13 Jul 2022
it's an image jpg , i want to diplayed in different places each time, when the user see it , he click with the mouse
OP on 27 Jul 2022 at 7:45
my problem is i want to identify the position of the mouse click (when the user click to the image ) and the time when he click if it's possible
回答(1 个)
Jonas
2022-7-12
编辑:Jonas
2022-7-12
you can try this raw code, how often the image can change the position is defined in the function
close all;
clear all;
im=imread('peppers.png');
width=0.2;
height=0.3;
fig=figure('Visible','off'); im=imshow(im,'Border','tight'); im.HitTest='off';
fig.ButtonDownFcn=@(src,~) handleClick(src,width,height);
set(fig,'Units','normalized','Position',[0.4 0.4 width height],'MenuBar','none','NumberTitle','off','Visible','on');
set(gca,'Units','normalized','InnerPosition',[0 0 1 1]);
function handleClick(src,width,height)
persistent howManyTimes;
if isempty(howManyTimes)
howManyTimes=10;
end
if howManyTimes>0
newX=(1-width)*randi([0 100])/100;
newY=(1-height)*randi([0 100])/100;
src.Position=[newX newY width height];
howManyTimes=howManyTimes-1;
else
close(src);
end
end
condensed version:
close all;
clear all;
im=imread('peppers.png');
width=0.2;
height=0.3;
fig=figure('Visible','off'); % hide window first
im=imshow(im,'Border','tight'); im.HitTest='off'; % plot and interpret klick on th eimage as click on the figure
fig.ButtonDownFcn=@(src,~) handleClick(src,width,height); % set action on button click
set(fig,'Units','normalized','Position',[0.4 0.4 width height],'Visible','on'); % edit position of figure and set visible
function handleClick(src,width,height)
persistent howManyTimes;
if isempty(howManyTimes)
howManyTimes=10;
end
if howManyTimes>0
newX=(1-width)*randi([0 100])/100; % create random x position with 101 possible values between 0 and (1-width)
newY=(1-height)*randi([0 100])/100; % create random y position with 101 possible values between 0 and (1-height)
src.Position=[newX newY width height];
howManyTimes=howManyTimes-1;
else
close(src); % close window if we reached the howManyTimes==0
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image display and manipulation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!