Page 1 of 1

Displaying Random Images

Posted: Sun Jan 11, 2015 5:03 pm
by Reganhay
I'm making an application that when a percentage is reached an image appears. There are 4 different outcomes: <=70, >=71 and <95, >=95 and <100, and finally just 100. This percentage is created using random(100).

I have 4 different folders, 1 folder for each outcome, with many different images in each. When the outcome is derived i would like to take a random image from the corresponding folder and display it. How could this be done?

Thanks
Reganhay

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 5:24 pm
by richmond62
Well, let's suppose your chosen folder [ in the example I have used a folder called "folder3" ] contains 50 images . . .

and each one is named "1.png", "2.png", "3.png" and so on . . .

then you can do something like this:

put random(50) into RNUM
import paint from file "/folder3/" & RNUM & ".png"

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 6:37 pm
by bn
Hi Regan,

welcome to the forum.

You also could place an image object and set it's filename to a valid path to your folder. (look a "fileName" in the dictionary)

For the random part go like Richmond said.

The difference is that Richmond's method imports the image into the stack and will fill up the stack with images, whereas using the image object and setting it's filename will only display one image when needed and not fill up your stack.
There is definitely a place for importing images but I think in your situation just displaying a referenced image is OK.

Kind regards
Bernd

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 7:22 pm
by Reganhay
bn wrote:Hi Regan,

welcome to the forum.

You also could place an image object and set it's filename to a valid path to your folder. (look a "fileName" in the dictionary)

For the random part go like Richmond said.

The difference is that Richmond's method imports the image into the stack and will fill up the stack with images, whereas using the image object and setting it's filename will only display one image when needed and not fill up your stack.
There is definitely a place for importing images but I think in your situation just displaying a referenced image is OK.

Kind regards
Bernd
I understand everything you mean apart from the part when you mention the image object. I'm new with livecode and don't know what such things are. Is it one of the tools on the tools palette?

Thank you for the replies and further help would be much appreciated

Reganhay

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 7:27 pm
by richmond62
Personally I would always go for importing the image, then renaming it to a standard name,
so that it can be deleted on the next randomised image import. This will allow you greater control
over the image.

set the name of the last object to "PIKTURE"

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 7:32 pm
by jmburnod
Hi Regan,
... the image object. I'm new with livecode and don't know what such things are. Is it one of the tools on the tools palette?
Yes, the image object is one of tools palette.
When you pass the mouse over it you can read "image area" on the contextual help
Best regards
Jean-Marc

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 7:56 pm
by Reganhay
richmond62 wrote:Well, let's suppose your chosen folder [ in the example I have used a folder called "folder3" ] contains 50 images . . .

and each one is named "1.png", "2.png", "3.png" and so on . . .

then you can do something like this:

put random(50) into RNUM
import paint from file "/folder3/" & RNUM & ".png"
i attempted this method, the image appears but is situated in the middle of the application. How could i get the image to go to a specific area on the page?

Thanks

Reganhay

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 8:01 pm
by Klaus
Hi Reganhay,

1. welcome to the forum! :D

2. Please check these stacks to get he very basics of Livecode:
http://www.hyperactivesw.com/revscriptc ... ences.html

3. Take small steps and separate your task into two smaller task, what they actually are.

The first one is actually a simple logical thing 8)
Can you assign one of your folders to a varaible depending on the different "outcomes"?
Know what I mean?

Something like this:
if the outcome < 75 then put "path/to/folder swith images 1" into tFolder
etc... (Although I would recommend a SWITCH CASE structure here.)
Get the picture?

The second problem will be the get ONE RANDOM images out of the above defined folder, but that's for later :D

The mentioned solution of using ONE image object and just set the FILENAME is the way to go!
IMPORT PAINT... will always create a new image object in your stack and you need to manage this
and delete the previously imported image if any etc... etc... Wayyyy to much work 8)


Best

Klaus

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 8:05 pm
by richmond62
put random(50) into RNUM
import paint from file "/folder3/" & RNUM & ".png"
set the name of the last object to "PIKTURE"
move img "PIKTURE" to 400,400

The other thing you can do is scale your image if it is too big to display
in the stack.

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 8:25 pm
by Reganhay
richmond62 wrote:put random(50) into RNUM
import paint from file "/folder3/" & RNUM & ".png"
set the name of the last object to "PIKTURE"
move img "PIKTURE" to 400,400

The other thing you can do is scale your image if it is too big to display
in the stack.
renaming the picture doesn't work, i tried:

set the name "/folder3/" & RNUM & ".png" to "PIKTURE"

However this doesn't work either.

Any way you can help?

Thanks

Reganhay

Re: Displaying Random Images

Posted: Sun Jan 11, 2015 8:34 pm
by richmond62
Here's a script I had in a stack a while back that imports and scales an image:

on mouseUp
if exists(img "picture") then
delete img "picture"
end if
answer file "choose the image to import"
if the result = "cancel" then
exit mouseUp
else
import paint from file it
set the name of the last image to "picture"
end if
put the width of img "picture" into WIDD
put the height of img "picture" into HITE
if HITE > WIDD then
set the height of img "picture" to 350
put HITE/350 into DIVVER
put WIDD/DIVVER into WDIV
set the width of img "picture" to (WDIV div 1)
else
set the width of img "picture" to 350
put WIDD/350 into DIVVER
put HITE/DIVVER into HDIV
set the height of img "picture" to (HDIV div 1)
end if
set the moveSpeed to 65000
move img "picture" to 400,300
end mouseUp

Here's the stack :
imager.livecode.zip
(8.19 KiB) Downloaded 188 times

Re: Displaying Random Images

Posted: Mon Jan 12, 2015 1:57 pm
by Klaus
Reganhay wrote:...
set the name "/folder3/" & RNUM & ".png" to "PIKTURE"
...
You should do yourself a big favour and work through the above mentioned stacks!

Re: Displaying Random Images

Posted: Mon Jan 12, 2015 6:16 pm
by Reganhay
Klaus wrote:
Reganhay wrote:...
set the name "/folder3/" & RNUM & ".png" to "PIKTURE"
...
You should do yourself a big favour and work through the above mentioned stacks!
I have started, thank you fro your help. I am also learning livecode at school. Some of the content i know and others i don't so i'm working through them now.

Thanks

Reganhay