I am trying to do my final project for my java class. I am attempting to take a .png picture and use it as a component that I can add to my JFrame. However, when I try to do this it throws an exception and does what is in the catch statement. I do not understand why it would do this. I have the .png file in the same folder as my .java files.
package InventoryApp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
/**
*
* @author Curtis
*/
public class FinalProject extends DFrame
{
//main method
public static void main(String[] args)
{
start();
}
//building splash screen
public static void start()
{ DFrame splashFrame = new DFrame();
try
{
BufferedImage myPicture = ImageIO.read(new File("logo.png"));
JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
splashFrame.add(picLabel);
}
catch(IOException g)
{
JLabel error = new JLabel("Picture Could Not Be Found");
splashFrame.add(error);
}
JButton create = new JButton("Click to Create Item List");
JButton view = new JButton("Click to View Item List");
splashFrame.add(create);
splashFrame.add(view);
}
}
When you create a
Fileobject with no path specified, it assumes the directory the program was launched from, not the directory the current class file is in. You probably want to instead useFinalProject.class.getResource():