View Single Post
  #4 (permalink)  
Old 03-21-2008, 05:45 PM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
a few steps that describe what is going on
Code:
public class ImageArray { declare an array of BufferedImage // member variable public ImageArray(BufferedImage[] images) { pass image array into this class assign value of local variable to member variable member variable local variable this.images = images; } private JPanel getContent() { build a component to show the images for(int i = 0; i < images.length; i++) { ImageIcon is the way to add an image to a component JLabel takes an ImageIcon add to component } return component; } public static void main(String[] args) throws IOException { load some images provide path information to locate image files declare and allocate an array of BufferedImage BufferedImage[] images = new BufferedImage[ids.length]; read each image with ImageIO.read for(int i = 0; i < images.length; i++) { String path = prefix + ids[i] + ext; images[i] = ImageIO.read(new File(path)); } instantiate/create an instance of the enclosing class ImageArray app = new ImageArray(images); show the component created in getContent } }
where do I store the images? In the folder located within the project directory?
That would be okay.
Code:
// Images are located in the geek folder // which is located in the images folder // which is located in the current directory. String prefix = "images/geek/geek"; String[] ids = { "-c---", "--g--", "---h-", "----t" }; String ext = ".gif";
The images used in this example are inside a folder in the current directory named "images". Inside the "images" folder is another folder named "geek". Inside the "geek" folder are the four image files.
So the path to the first file is: "images/geek/geek-c---.gif"
This tells java where to find the file.
Where to keep the image files is a matter of choice. You can keep them in the current directory: "geek-c---.gif"
You can keep them inside a folder, aka directory, in the current directory. If the folder is named "images" then the path would be: "images/geek-c---.gif"
The path part is each folder name followed by a forward slash and the image file name with the file extension on the end.
For more about images and file structure see How to Use Icons with special attention to the Loading Images Using getResource section.
How do I reference these images?
Use the path and image file name with extension. To find out what extensions ImageIO can read you can use one of the static methods such as ImageIO.getReaderFileSuffixes.
Reply With Quote