Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8618993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:14:18+00:00 2026-06-12T06:14:18+00:00

I have 30 ImageIcons that I have declared in a ImageIcon array. I have

  • 0

I have 30 ImageIcons that I have declared in a ImageIcon array. I have declared them all using a FOR loop. THe only problem is that when I try to add the ImageIcon to a JLabel and display the JPanel on the screen, it doesn’t work.Here is my code:

package screens;

import javax.swing.*;

import java.awt.*;

public class gameScreen extends JPanel {
        private static final long serialVersionUID = 1L;
        // -------------VARIABLES---------------//
        Image wallpaper  = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/wallpaper.jpg"));


        ImageIcon[] teamsImg = new ImageIcon[30];
        public gameScreen() {

        for(int i = 0;i>30;i++){
            teamsImg[i] = new ImageIcon(Toolkit.getDefaultToolkit().getImage(
        getClass().getResource("images/img.png")));


         }

         JLabel label = new JLabel(teamsImg[27]);
         add(label);

    }

// -------------PAINT FUNCTION----------//
public void paintComponent(Graphics g) {
     g.drawImage(wallpaper,0,0,null);



}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:14:19+00:00Added an answer on June 12, 2026 at 6:14 am

    I would take a really long, hard look at this line…

    for (int i = 0; i > 30; i++) {
    

    As Adam Savage would say, “well, there’s your problem”.

    Personally, I would use ImageIO over Toolkit.getImage, unless you have a really good reason not to (like you need the background loading capabilities of Toolkit.getImage due to slow network connection). ImageIO will give you access to a much wider range of possible image formats, thanks to it’s plugin architecture as well as guarantee that when it returns, the image will fully loaded.

    Something like…

    try {
        wallpaper = ImageIO.read(getClass().getResource("images/wallpaper.jpg"));
    
        for (int i = 0; i < 30; i++) {
    
            teamsImg[i] = ImageIO.read(getClass().getResource("images/img.png"));
    
        }
    } catch (IOException iOException) {
        System.err.println("Bad things happened");
        iOException.printStackTrace();
    }
    

    This will require to use Image instead of ImageIcon to store the images, but it’s a little sacrifice for the benefit it brings you.

    Also, you do know, you’re loading the same image over and over again??? You could just load it once and set the reference to each element in the array, it’s effectively the same thing…(as I understand it, the image API is using a cached version to make repeated loads quicker)

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have an ImageIcon in a JLabel that i will be able
I have created a class that extends JLabel to use as my object moving
I have a java/maven project that uses tango icons, so I'm using the following
I have tried using the code below to display ImageIcons in JTable. But when
I have a small Java program that uses ImageIcons on JLabels to display pictures.
So I have an image: ImageIcon i = new ImageIcon(foo.png); JLabel j = new
I have a JComboBox that its renderer is a JLabel with an Icon for
I have a JLabel inside which I have saved my ImageIcon like this: ImageIcon
I have many JLabel s (which includes ImageIcon s) in a JPanel . And
I have snippet of code that I am using for the purpose of resizing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.