File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java 6: Need to have an array of images of which I can randomly select Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java 6: Need to have an array of images of which I can randomly select" Watch "Java 6: Need to have an array of images of which I can randomly select" New topic
Author

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

Ratko Corgan
Greenhorn

Joined: Jun 02, 2011
Posts: 2
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

Joined: Oct 13, 2005
Posts: 36514
    
  16
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: Jun 02, 2011
Posts: 2
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 6: Need to have an array of images of which I can randomly select
 
Similar Threads
Accessing Remote Files
Where Do I place the Folder for Java Applet Images?
Accesing image files in java beans
URLyBird search for the database records issue - advice needed
Tricky NullPointerException Problem