How to make an image appear in different locations
3 ビュー (過去 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 件のコメント
回答 (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 件のコメント
参考
カテゴリ
Find more on Image display and manipulation 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!