Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 6: Need to have an array of images of which I can randomly select

 
Ratko Corgan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to make an application that tests for color blindness, in which a person doing the test is seeing a picture with a number obscured in it. In the most ideal situation I have the pictures in a folder, which the application picks up when starting up. These images would then be held in an array, and each time an image needs to be displayed it randomly selects from the array.

I do not know if it is complex to get the pictures dynamically, as an alternative the pictures could be hard coded in the code. The advantage of loading images dynamically is that I can add pictures to the folder and they would be picked up, whereas the hard coded way needs to have code adjusted to accommodate.

My question is how to go about doing this. I have seen more than one way of loading pictures, and I think it might've been because of different java versions. I see the use of Image, the use of ImageIcons, different ways of loading (File streams, IO, etc.).

I am using Java 6, the latest NetBeans, and I am using the GUI components from there. I already added a package (folder) that contains the images.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I wish I could remember anything about images . . .
Find the Java™ Tutorials and there should be something about images in that. I think you are supposed to call getClass().getResource("myImage.png"); and that returns a URL object, whence you can obtain the Image. But the first example you see in the tutorial is much simpler than that. You can see for yourself.

You can easily put the images into an array and create a java.util.Random object and call its nextInt() method with the size of the array as an argument, to get the images in unpredictable order.
 
Paul Clapham
Sheriff
Posts: 20713
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you're trying to figure out everything all at once. You have at least two questions:

(1) How do I display an image?

(2) How do I get an array of images which I can choose from?

And there's probably more if I read more closely. But anyway it reminds me of Jackie Chan fighting off half a dozen attackers all at once. You aren't Jackie Chan yet so I recommend working on one question at a time. Try to break down your question into simpler pieces.
 
Ratko Corgan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for welcoming me to the ranch

Yes, I know about the Java tutorials. These have the ImageIO as was trying to tell about (I called it IO) and uses an ImageBuffer:



But there is also this:



So to not Jackie Chan this:

I've decided to hard code the pictures in the code.

1. Which one do I use?
2. Should I go for Image, ImageIcon, or BufferedImage?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic