• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display 92 jpg images from a folder one at a time in random order.

 
Alex Petsche
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 92 jpg's in a folder. I want to read them from that folder and display them one at a time. The image to be displayed after the first image has been displayed is decided at random.

I've managed to get an image to display as I want it too, but I'm stuck on how to randomly display them. The idea I've come up with is to create an array for each image, but then that defeats the purpose of loading them from a file instead of loading them all into an array at once. (More resource friendly was the initial thought.)

I kind of found a way to get it to work how I want with putting the names of the files into a String[], but the set path on line 67 is an issue if the project ever moves one day and this still isn't all being read from files with this array. I've gotten the end result I want, but not the path to get there. I want to get rid of the array I'm using, but I'm not sure how or with what to replace it with. I've been rambling a bit at this point, so I'll let it be and any ideas on how to solve this are much appreciated!

This is what I have so far:
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> but the set path on line 67 is an issue if the project ever moves one day

You should never hard code and "absolute path". The image files should be placed in a separate director that is relative to your project. Then you can access the file just by specifying the "relative path". Read the section from the Swing tutorial on How to Use Icons for examples of how you can set us this structure and the code that can be used to access the files.

> I want with putting the names of the files into a String[],

That is fine, but again don't hardcode the file names. Instead you can use the File.listFiles(...) method to get the name of all the files in the directory.

> The image to be displayed after the first image has been displayed is decided at random.

Using random is fine but this approach means you may see duplicate images. If you want your images to be random but also unique as you go through then entire list then you can take advantage of the Collections API:



Then you can iterated through the collection one file at a time.
 
Alex Petsche
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I'm tackling these one at a time, so this will be a slow go, but I think I figured out how to fix the first issue you mentioned. I think it might be crude with parsing the string to get the ImageIO.read to accept it, but it appears to be working.

I removed the hard code in my previous post on line 67 and replaced it with what is now on lines 45-58. I'm not sure if this is the "proper" way to do this as the string conversion and parsing seems crude to me. I could be wrong though and if this is correct then yay! haha Thanks for the advice and help so far!

 
Alex Petsche
Ranch Hand
Posts: 42
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I missed a part in that link you posted. I think this is what the demo wanted me to do instead of what I came up with.



I'll compare it to what I have and play around with it. Thanks for the help so far!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!