How to make an image appear in different locations

1 回表示 (過去 30 日間)
ss
ss 2022 年 7 月 12 日
コメント済み: Rena Berman 2022 年 11 月 1 日
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
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
Rena Berman
Rena Berman 2022 年 11 月 1 日
(Answers Dev) Restored edit

サインインしてコメントする。

回答 (1 件)

Jonas
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

カテゴリ

Help Center および File ExchangeImage display and manipulation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by