sam
sam

Reputation: 7

Whats the easiest way to declare 52 image icons in an array

I have an imageicon array whats an easier way of declaring them than just declaring each individual image. ImageArray[0] = new ImageIcon("resources/images/cards/6.png");

Upvotes: 0

Views: 4249

Answers (2)

Alex Abdugafarov
Alex Abdugafarov

Reputation: 6412

Two most common ways:

  • In a cycle - name your icons 1.png, 2.png and so on, then iterate using a temporary variable instead of a name
  • Recursively read the directory content, adding all images into an ArrayList, then (if needed) call toArray()

Upvotes: 0

lobster1234
lobster1234

Reputation: 7779

Assuming the images are 0.png through 51.png in the resources.images/cards folder:

ImageIcon[] images = new ImageIcon[52]
for(int i=0;i<52;i++){
  images[i] = new ImageIcon("resources/images/cards/"+i+".png");
  }

Upvotes: 5

Related Questions