How to make an image appear in different locations

1 visualizzazione (ultimi 30 giorni)
ss
ss il 12 Lug 2022
Commentato: Rena Berman il 1 Nov 2022
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 Commenti
Rik
Rik il 26 Ago 2022
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

Accedi per commentare.

Risposte (1)

Jonas
Jonas il 12 Lug 2022
Modificato: Jonas il 12 Lug 2022
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

Community Treasure Hunt

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

Start Hunting!

Translated by