For the record, I'm not too great at Java yet, I'm just starting out with it. Currently, I'm working on a small personal project that randomly displays images from a folder for 5 seconds before transitioning to another one. I'd like to add as many images as possible, but once I make an array of over 100+ objects, managing it, as well as the files, becomes quite a chore, especially if I want to start rearranging files or add/remove some.
I've already had to start renaming the files to sequential numbers to get the random cycling to work, which has been a chore in it's own, so I was curious if there was a way to at least shorten the array and have it cycle through the files. Or maybe there's an easier solution that doesn't require the array at all since the files are named sequentially with numbers.
This is what the code looks like so far:
var delay=5000 //set delay in miliseconds
var curindex=0
var randomimages=new Array()
randomimages[0]="1.png"
randomimages[1]="2.jpg"
randomimages[2]="3.png"
randomimages[3]="4.png"
randomimages[4]="5.png"
randomimages[5]="6.png"
randomimages[6]="7.png"
randomimages[7]="8.jpg"
randomimages[8]="9.png"
randomimages[9]="10.png"
// Cut out the 100 extra lines
// Continues like this for multiple lines and variates between PNG and JPG images
randomimages[103]="104.jpg"
var preload=new Array()
for (n=0;n<randomimages.length;n++)
{
preload[n]=new Image()
preload[n].src=randomimages[n]
}
document.write('<div><img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'"></div>')
function rotateimage()
{
if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length)))){
curindex=curindex==0? 1 : curindex-1
}
else
curindex=tempindex
document.images.defaultimage.src=randomimages[curindex]
}
setInterval("rotateimage()",delay)
Any help is appreciated.