Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Problem adding images into an array

    I'm trying to develop some code that will output an array of images (145). Eventually I'll add buttons & other JPanel objects, but right now I'm just trying to understand the logic. The resource folder is a series of .jpg files with the name of a card. My problem is I don't know a way to add each file to the array in a for loop since the names are dynamic. Is there a way to implement a scanner object into a folder structure or am I missing out on a better java tool? Any ideas on how to do this? Thanks in advance for any advice that can be offered!

    Here's what I'm working with:

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.Color.*;
    
    public class ImageDisplay extends JFrame
    {
    	String[] cardsArray = new String[145];
    	
    	for( int i = 0; i < 145; i++ )
    	{
    		/* if the file names were abc1, abc2, abc3.. etc, the String.valueOf(i+1) 
    		would be fine, but I'm looking for another way to reference a filename here */
    	   cardsArray[i] = "C:\\Users\\John Smith\\AppData\\Local\\Forge\\Cache\\pics\\cards\\ARB\\" + String.valueOf(i+1) + ".jpg";
    	}
    	
    	JFrame myJFrame = new JFrame();
    	myJFrame.setVisible( true );
    
    	myJFrame.setLayout( new FlowLayout() );
    	
    	for ( String imgName : cardsArray )
    	{
    	    myJFrame.add( new JLabel( new ImageIcon( imgName ) ) );
    	}
    	
    	myJFrame.setTitle( "ImageIcon Demo" );
    	myJFrame.setSize( 800, 600 );
    	myJFrame.setLocation( 200, 100 );
    	myJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	
    	System.out.println( Arrays.toString( cardsArray ) );
    }
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Problem adding images into an array

    a way to add each file to the array in a for loop since the names are dynamic.
    Can you explain what you mean by "dynamic name"?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem adding images into an array

    You might want to read up on both the File and Files classes. You can open up a directory and read in a list of file names based on a filter.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    I think dynamic was just a poor choice of words. I just meant that the file names are non-numerical and non-sequential. The folder and it's contents will remain static and unchanging.
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  5. #5
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Thanks for the pointer, Jim. I'm always interested to learn more about the java API. So could I call a File.getName() method in place of my
    Java Code:
    + String.valueOf(i+1) + ".jpg";
    code?
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem adding images into an array

    Yes. There are also other methods for simple names, etc. I suggest you write some test code just to open a directory and list the files. You can apply a filter too to see how it works.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Problem adding images into an array

    Jim was referring to methods in the classes he mentioned that will return a list of files that are in a folder.
    Read the API doc for those classes to see which methods might be useful.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Thanks for the tips, guys. I'll take a look at it some more later when I have the time. This should help get me started.
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  9. #9
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Okay, I'm making some headway after looking at some of the Java API concerning File objects. Currently I can verify that there are 145 files in the directory based on my code output. However it's still unclear to me how to call a file by name using these methods. Can anyone offer some advice on how to do this? Thanks! Here's the code I have:
    Java Code:
    import java.io.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    import java.lang.*;
    
    public class FileTest
    {
    	public static void main( String [] args )
    	{
    		String[] cardsArray = new String[145];
    		for( int i = 0; i < 145; i++ )
    		{
    			File file = new File( "C:\\Users\\John Smith\\AppData\\Local\\Forge\\Cache\\pics\\cards\\ARB\\" );
    		   cardsArray[i] = file.getName() + ".jpg";
    			System.out.println( file.getName().toString() );
    		}
    	}
    }
    And my output looks like this:
    Java Code:
    ARB
    ARB
    ...145 entries of ARB (directory location)
    Java Code:
    ARB
    
     ----jGRASP wedge2: exit code for process is 0.
     ----jGRASP: operation complete.
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  10. #10
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Okay I'm making progress. The code I have compiles with no errors but I'm still not understanding how to reference a file by name. Can anyone offer any advice on how to do this? Thanks! Here's my code & output:
    Java Code:
    import java.io.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    import java.lang.*;
    
    public class FileTest
    {
    	public static void main( String [] args )
    	{
    		String[] cardsArray = new String[145];
    		for( int i = 0; i < 145; i++ )
    		{
    			File file = new File( "C:\\Users\\John Smith\\AppData\\Local\\Forge\\Cache\\pics\\cards\\ARB\\" );
    		   cardsArray[i] = file.getName() + ".jpg";
    			System.out.println( file.getName().toString() );
    		}
    	}
    }
    output:
    ARB
    ARB
    ARB
    ... repeats for each of 145 files followed by:
    Java Code:
     ----jGRASP wedge2: exit code for process is 0.
     ----jGRASP: operation complete.
    Last edited by Sizzlewump; 09-13-2015 at 11:47 PM. Reason: my original message did not appear in my browser
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Problem adding images into an array

    how to call a file by name using these methods.
    What is wrong with the code you have posted? It looks like it would print out the filename.
    The filename isn't changed in the loop, so the same name would be printed 145 times.

    What is it you want to print?
    Last edited by Norm; 09-13-2015 at 11:50 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    My goal here is to print each unique filename in the output for 145 files with different names.
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Problem adding images into an array

    The File class has a method to get the names of all the files in a folder. Read the API doc for the File class for a method to list the contents of a folder.

    The code you've posted does not do anything to generate a list of filenames.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Are you referring to the list() & listFiles() methods? I'm not sure those will suit my needs. What I would like to do is have an array of .jpg objects that I can later refer to by their respective filenames. So I'll want to use my other code to randomly pick 15 of these by name and then display the 15 images in an applet.
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  15. #15
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Here's what I'm working on. The applet displays an empty window. It should output an array of 145 images once I work out the bugs.
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ImageDisplay extends JFrame
    {
    	public static void main( String [] args )
    	{	
    	   String[] cardsArray = new String[145];
    		for( int i = 0; i < 145; i++ )
    		{
    			File file = new File( "C:\\Users\\John Thrush\\AppData\\Local\\Forge\\Cache\\pics\\cards\\ARB\\" );
    
    		   cardsArray[i] = file.getName() + ".jpg";
    		}
    		JFrame myJFrame = new JFrame();
    		myJFrame.setVisible( true );
    	
    		myJFrame.setLayout( new FlowLayout() );
    		
    		for ( String imgName : cardsArray )
    		{
    		    myJFrame.add( new JLabel( new ImageIcon( imgName ) ) );
    		}
    		
    		myJFrame.setTitle( "ImageIcon Demo" );
    		myJFrame.setSize( 800, 600 );
    		myJFrame.setLocation( 200, 100 );
    		myJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	}
    }
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Problem adding images into an array

    referring to the list() & listFiles() methods?
    Yes, those methods would give you a list of all the files in a folder. They can use helpers classes as filters to select only the files you want in the list. Read the API doc about filters.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Here's what I'm working on. The applet displays an empty window. It should output an array of 145 images once I work out the bugs.
    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    import java.lang.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ImageDisplay extends JFrame
    {
    	public static void main( String [] args )
    	{	
    	   String[] cardsArray = new String[145];
    		for( int i = 0; i < 145; i++ )
    		{
    			File file = new File( "C:\\Users\\John Smith\\AppData\\Local\\Forge\\Cache\\pics\\cards\\ARB\\" );
    
    		   cardsArray[i] = file.getName() + ".jpg";
    		}
    		JFrame myJFrame = new JFrame();
    		myJFrame.setVisible( true );
    	
    		myJFrame.setLayout( new FlowLayout() );
    		
    		for ( String imgName : cardsArray )
    		{
    		    myJFrame.add( new JLabel( new ImageIcon( imgName ) ) );
    		}
    		
    		myJFrame.setTitle( "ImageIcon Demo" );
    		myJFrame.setSize( 800, 600 );
    		myJFrame.setLocation( 200, 100 );
    		myJFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	}
    }
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Problem adding images into an array

    Why are you adding the .jpg suffix on the filename? Seems to me they should already have that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Problem adding images into an array

    The two methods of the File class you listed could be used to get a list of files from a selected folder.

    Try writing a small program like that in post#10 that uses one of those methods to get a list of files from a folder and prints their names.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    Sizzlewump's Avatar
    Sizzlewump is offline Member
    Join Date
    Oct 2010
    Location
    MI, USA
    Posts
    68
    Rep Power
    0

    Default Re: Problem adding images into an array

    Norm, I took your suggestion and tried the list() and listFiles() methods. The output shows a hash code (I think) of the .jpg files rather than the images. Here's what I did:
    Java Code:
    import java.io.*;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    import java.lang.*;
     
    public class FileTest
    {
        public static void main( String [] args )
        {
            String[] cardsArray = new String[145];
            for( int i = 0; i < 145; i++ )
            {
                File file = new File( "C:\\Users\\John Smith\\AppData\\Local\\Forge\\Cache\\pics\\cards\\ARB\\" );
                cardsArray[i] = file.getName();
                System.out.println( file.list().toString() );
            }
        }
    }
    output: (sample of 145 entries)
    Java Code:
    [Ljava.io.File;@5b3caecd
    [Ljava.io.File;@6cf84386
    [Ljava.io.File;@1f194a4e
    [Ljava.io.File;@355d56d5
    [Ljava.io.File;@2efd552
    [Ljava.io.File;@4f9dfbff
    [Ljava.io.File;@d0b4b2f
    [Ljava.io.File;@60750159
    "The secret to getting what you want is to reject everything that you don't want." -Wolbers

Page 1 of 2 12 LastLast

Similar Threads

  1. error when adding Images
    By Daryn in forum New To Java
    Replies: 5
    Last Post: 01-18-2014, 01:27 AM
  2. adding images
    By alexix in forum New To Java
    Replies: 3
    Last Post: 03-04-2013, 10:36 PM
  3. adding different images using if statements
    By JoePenguin in forum New To Java
    Replies: 3
    Last Post: 02-16-2012, 03:54 AM
  4. Replies: 8
    Last Post: 02-28-2010, 08:36 AM
  5. [new] need help on GUI adding images
    By counterfox in forum AWT / Swing
    Replies: 5
    Last Post: 03-19-2009, 10:29 AM

Tags for this Thread

Posting Permissions

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