Solved

Loading an image with ImageIcon for a static context/class

Posted on 2009-04-14
10
1,021 Views
Last Modified: 2013-11-23
Hi, really simple, here's what I want to do:
I have "public class Global" with static variables inside, then I want to load an image with ImageIcon but the image doesn't load, see the code:
import javax.swing.ImageIcon;
 

public class Global {

    public static int test = 5;
 

    public static ImageIcon[] images = new ImageIcon[Main.NOMBRE_ELEMENTS];

    static

    {

        images[Main.MONSTRE1] = new ImageIcon("/Interface/monstre1.jpg");

        images[Main.MONSTRE2] = new ImageIcon("/Interface/monstre2.jpg");

        images[Main.HERO] = new ImageIcon("/Interface/hero.jpg");

        images[Main.GRASS] = new ImageIcon("/Interface/grass.jpg");

    }

}
 

// Later in the code, if I write:

System.out.println( test );

System.out.println( Global.images[0].getWidth() );

// , then I get '-1' since the image did not load, but the variable 'test' shows up correctly

Open in new window

0
Comment
Question by:Dimitri04
  • 5
  • 4
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24141320
I would use ImageIO
images[Main.MONSTRE1] = ImageIO.read(new File("/Interface/monstre1.jpg"));

Open in new window

0
 

Author Comment

by:Dimitri04
ID: 24141551
I tried it, but it did not work, see code for what I tried:
// Says Inconvertible types:

images[Main.MONSTRE1] = (ImageIcon) (ImageIO.read(new File("/Interface/monstre1.jpg"));
 

// Says Incompatible types

images[Main.MONSTRE1] = ImageIO.read( new File("/Interface/monstre1.jpg") );
 

// Says unreported exception

images[Main.MONSTRE1] = new ImageIcon( ImageIO.read( new File("/Interface/monstre1.jpg") ) );

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24141634
You need to change

>>public static ImageIcon[] images = new ImageIcon[Main.NOMBRE_ELEMENTS];

to
public static Image[] images = new Image[Main.NOMBRE_ELEMENTS];

Open in new window

0
 

Author Comment

by:Dimitri04
ID: 24141798
Well, I can't I must stick with ImageIcon because later in the project I am using:
lbl.setIcon( images[Main.MONSTRE1] );

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24141839
You can then do
  	
 

lbl.setIcon(new ImageIcon(images[Main.MONSTRE1]));

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
ID: 24143907
I don't see any need to complicate things by using ImageIO. where are the images located?

0
 

Author Comment

by:Dimitri04
ID: 24150278
I just can't make it work CEHJ, ImageIO doesn't seem to work well for what I wat to do.

The images are located in the source folder, so all I need is import them. It is working well in a non-static context, but as soon as I am in a static context, it's not working, i.e. images not showing, getWidth() = -1
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24150347
Actually i get so used to people using ImageIcon to load images here when the icon isn't required, i thought that's what you might be doing. Let's stick with your original code:

>> images[Main.MONSTRE1] = new ImageIcon("/Interface/monstre1.jpg");

That will try to load the image from the root of your file system in /Interface, which i suspect is not what you intend. If Interface is at your package root, try instead
images[Main.MONSTRE1] = new ImageIcon(Global.class.getResource("/Interface/monstre1.jpg"));

Open in new window

0
 

Author Comment

by:Dimitri04
ID: 24151032
Thanks!! exactly what I needed
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24151191
:-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 81
wordlen challenge 3 49
Groovy:unable to resolve class error 2 32
eclipse java  build path 6 25
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now