AllExperts > Experts 
Search      

Java

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Java Answers
Question Library

Ask a question about Java
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Artemus Harper
Expertise
I have a BS in computer science and am working towards a Masters degree.

Experience
I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection, and Java java.nio.* knowledge of Java bytecode and annotations. Basics in c++ and c#
 
   

You are here:  Experts > Computing/Technology > Focus on Java > Java > Images

Topic: Java



Expert: Artemus Harper
Date: 6/2/2008
Subject: Images

Question
I am trying to add an image to a swing panel, with the following code...

String location = (String) picOf.getSelectedItem () + "bmp";
guyPic = tools.getImage (location);
Graphics g = getGraphics ();
g.drawImage (guyPic, 20, 20, this);
setVisible (true);

The images however (which are in the same folder as the .java and .class files) never show up. It seems to me like the image is never gotten from the file, since replacing the string with the name of a file that doesn't exist doesn't cause an error. Thanks for your time.

Answer
If you don't give the proper path of an image no error is thrown, you will simply not get an image. The reason for this is that Java dynamically loads the image, so that if you load it over the internet, the user can see the image load. This also means that if you simply draw the image it may not be ready yet for drawing. Also anything you paint with the above code is erased as soon as a redraw happens (e.g. window is minimized and restored). I would recommend using a JLabel with an IconImage.

If you want the image to be aggressively drawn, then use the ImageIO class to load the image. E.g.

import javax.imageio.*;
...
String location = (String) picOf.getSelectedItem () + "bmp";
guyPic = ImageIO.read(MyClass.class.getResource(location));
add(new JLabel(new ImageIcon(guyPic)));
setVisible(true);

This will throw NullPointerException if location is not a file relative to the class MyClass (put your own class name instead), since getResource will return null if location does not point to an existing file.

For you pleasure here is a "one" liner to show an image (the first argument when you run the program, a path to the image).

import javax.swing.*;
import java.net.*;
public class Test
{
  public static void main(String[] args)
  {
     JOptionPane.showMessageDialog(null,new ImageIcon(args[0]));
  }
}


Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.