Results 1 to 4 of 4
- 03-20-2008, 10:55 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
How do I create an Array of Images
Hi,
I'm creating a program that basically is a slideshow. It has buttons that allow the user to switch from image to image, and has some audio in the background. I was wondering how I might create an array to store the images. I'm very new to Java, so please no detail is too small. I've just started using arrays, so I'm not sure where I would store the files or how to call the images. Please be descriptive and as dumbed down as possible :)
Thanks for the help!
- 03-21-2008, 07:42 AM #2
You can find the geek images at the bottom of this page: Using Swing Components: Examples
Java Code:import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; import javax.swing.*; public class ImageArray { BufferedImage[] images; public ImageArray(BufferedImage[] images) { this.images = images; } private JPanel getContent() { JPanel panel = new JPanel(new GridLayout(0,2,5,5)); panel.setBackground(Color.green.darker()); for(int i = 0; i < images.length; i++) { ImageIcon icon = new ImageIcon(images[i]); JLabel label = new JLabel(icon, JLabel.CENTER); panel.add(label); } return panel; } public static void main(String[] args) throws IOException { // 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"; BufferedImage[] images = new BufferedImage[ids.length]; for(int i = 0; i < images.length; i++) { String path = prefix + ids[i] + ext; images[i] = ImageIO.read(new File(path)); } ImageArray app = new ImageArray(images); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(app.getContent()); f.pack(); f.setLocation(200,200); f.setVisible(true); } }
- 03-21-2008, 09:49 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 14
- Rep Power
- 0
Thanks for all the help; however, I'm a little confused with some of the code, as I am very new to Java. If you could give me a few steps that describe what is going on, I would greatly appreciate it. Also, I understand now how to implement the array, but where do I store the images? In the folder located within the project directory? How do I reference these images? Thanks for the help.
- 03-21-2008, 03:45 PM #4
a few steps that describe what is going on
where do I store the images? In the folder located within the project directory?Java 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 [i]getContent[/i] } }
That would be okay.
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.Java 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";
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.
Similar Threads
-
Using reflection to create, fill, and display an array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:43 PM -
Images not displaying in JSP in IE7
By chadscc in forum Advanced JavaReplies: 0Last Post: 11-13-2007, 03:24 PM -
Help using images in Java
By toby in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:54 AM -
Help with images...
By toby in forum Java AppletsReplies: 1Last Post: 08-04-2007, 05:25 AM -
Images in JSP
By Daniel in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-05-2007, 06:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks