Chris McCall
Chris McCall

Reputation: 1

Java Read images from a file into an ArrayList and displaying onto a JPanel

I am trying to take a file full of images and read them into an ArrayList to make a deck of cards. Then display it on a JPanel. Here is my code:

   private static class CardDealer extends JFrame 
{        
    private ImageIcon[] cards = new ImageIcon[52];
    private ArrayList<ImageIcon> deck = new ArrayList<ImageIcon>();




    private JButton deal;
    private JPanel faceDown, faceUp, button;
    private JLabel backs, fronts;
    private Random card = new Random(52);


    public CardDealer() throws FileNotFoundException
    {

       File images = new File("src/Images");
       Scanner file = new Scanner(images);
       for(int i=0; i<cards.length; i++)
       {

            cards[i] = new ImageIcon(Arrays.toString(images.list()));

            deck.add(cards[i]);
        }


        //setTitle to set the title of the window

        setTitle("Card Dealer");

        //set the application to close on exit

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Call all our build panel methods
        buildButtonPanel();
        buildFaceDownPanel();
        buildFaceUpPanel();

        setLayout(new BorderLayout());

        add(button, BorderLayout.SOUTH);
        add(faceDown, BorderLayout.WEST);
        add(faceUp, BorderLayout.EAST);

        pack();

        validate();

        setVisible(true);
    }

    private void buildButtonPanel()
    {
        button = new JPanel();

        deal = new JButton("Deal");

        deal.addActionListener(new buttonListener());

        button.add(deal);

    }

    private void buildFaceDownPanel()
    {
        faceDown = new JPanel();

        backs = new JLabel();

        backs.setText("Cards");

        backs.setIcon(new ImageIcon("Blue.bmp"));



        faceDown.add(backs);

    }

    private void buildFaceUpPanel()
    {
        faceUp = new JPanel();

        fronts = new JLabel();

        faceUp.add(fronts);
    }

    private class buttonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
           fronts.setIcon(deck.get(card.nextInt()));
        }
    }

}


public static void main(String[] args) throws FileNotFoundException
{
    new CardDealer();
}

I don't know why no images show up when I run the program. Even the backs JLabel image does not show when running. Any help is appreciated!!

Upvotes: 0

Views: 2439

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347332

Lets start with File#list will return an array of File objects which are contained within the specified file location, this makes

cards[i] = new ImageIcon(Arrays.toString(images.list()));

very worrisome for two reasons, apart from the fact that you are simply converting a list of files into a String, doing this cards.length is woefully inefficient...

Next, you should NEVER reference src in any path within your code, once built and packaged, src will no longer exist and you won't be able to access your "files" like normal files (as they will likely be embedded inside your jar file).

This raises another issue, since it's not easy to list resources embedded within a jar file (when you don't even know the name of the Jar file), you need a different approach.

One might be to name the images in a common, sequential manner, like /images/Card0.png, /images/Card1.png for example, then simply load the images via a loop

File images = new File("src/Images");
for(int i=0; i < cards.length; i++)
{
    cards[i] = new ImageIcon(
                       ImageIO.read(
                           getClass().getResource("/images/Card" + i + ".png")));
    deck.add(cards[i]);
}

Take a look at Reading/Loading an Image for more details about reading/loading images

Another solution might be to create a text file, which can be stored within the application context, which lists all the card names. You would use Class#getResource or Class#getResourceAsStream to load the file, read it's contents and load each image...

Upvotes: 2

Related Questions