Results 1 to 4 of 4
  1. #1
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default 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!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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);
        }
    }

  3. #3
    wco5002 is offline Member
    Join Date
    Mar 2008
    Posts
    14
    Rep Power
    0

    Default

    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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    a few steps that describe what is going on
    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]
        }
    
    }
    where do I store the images? In the folder located within the project directory?
    That would be okay.
    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";
    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.

Similar Threads

  1. Replies: 0
    Last Post: 04-14-2008, 08:43 PM
  2. Images not displaying in JSP in IE7
    By chadscc in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2007, 03:24 PM
  3. Help using images in Java
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:54 AM
  4. Help with images...
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:25 AM
  5. Images in JSP
    By Daniel in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-05-2007, 06:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •