Reputation: 91
I am currently trying to simply display an image within a JFrame
. I have tried using the below code but it doesn't seem to work. I can't work out where I am going wrong - any ideas?
This is the error message displayed in the console:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Day1.PictureTester.<init>(PictureTester.java:22)
at Day1.PictureTester.main(PictureTester.java:14)
Here is the code:
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PictureTester extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new PictureTester().setVisible(true);
}
public PictureTester(){
super();
setSize(600, 600);
setLayout(new GridLayout());
java.net.URL imgUrl = PictureTester.class.getResource("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg");
ImageIcon image = new ImageIcon(imgUrl);
JLabel display = new JLabel(image);
add (display);
}
}
Upvotes: 2
Views: 83
Reputation: 2322
Icon i=new ImageIcon("src//pics//scope.jpg");
JLabel l1=new JLabel(i);
Use simply like this use JLabel
or ImageIcon
Upvotes: 0
Reputation: 1103
You can do it like the following example, by using just an image and a label. also you can remove all the extra code if you intend to use this image only once.
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PictureTester extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new PictureTester().setVisible(true);
}
public PictureTester(){
super();
setSize(600, 600);
setLayout(new GridLayout());
add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));
}
}
Upvotes: 1
Reputation: 91
This code solved it, it helped my thinking to create it within a new project:
package NewAppIdea;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class PictureSplash extends JFrame {
private static final long serialVersionUID = 1L;
JLabel l1;
PictureSplash(){
setTitle("Pic");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg")));
setLayout(new FlowLayout());
setSize(399,399);
setSize(400,400);
}
public static void main(String args[]) {
new PictureSplash();
}
}
Upvotes: 1
Reputation: 691635
Of course that won't work. Read the javadoc of Class.getResource()
. It uses the ClassLoader to load a resource from the classpath, using a slash separated path.
Put your image in the same package as the PictureTester
class, and just use
PictureTester.class.getResource("pug-image3.jpg");
Or put it in some random package of your source folder (like com.foo.bar
), and load it using
PictureTester.class.getResource("/com/foo/bar/pug-image3.jpg");
Upvotes: 4