AboutArtemus 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#
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.
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]));
}
}