Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2008, 11:55 PM
Member
 
Join Date: Mar 2008
Posts: 14
wco5002 is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-21-2008, 08:42 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
You can find the geek images at the bottom of this page: Using Swing Components: Examples
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-21-2008, 10:49 AM
Member
 
Join Date: Mar 2008
Posts: 14
wco5002 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-21-2008, 04:45 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using reflection to create, fill, and display an array Java Tip java.lang 0 04-14-2008 09:43 PM
Images not displaying in JSP in IE7 chadscc Advanced Java 0 11-13-2007 04:24 PM
Help using images in Java toby Advanced Java 1 08-07-2007 06:54 AM
Help with images... toby Java Applets 1 08-04-2007 06:25 AM
Images in JSP Daniel JavaServer Pages (JSP) and JSTL 1 06-05-2007 07:01 AM


All times are GMT +3. The time now is 02:06 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org