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
답변 (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!