First I used JLabel and ImageIcon. It worked fine. However, the image doesn't show up when I exported it to JAR.
So, then I created my own ImagePanel and used the drawImage method to display it.
But it terminates and throws an
exception in thread "main" java.lang.IllegalArgumentException: input == null!
well, my code follows below..
startMenu
public class startMenu extends JFrame{
...
URL imgUrl = getClass().getResource("images/contacts.png");
public startMenu(){
...
ImagePanel imgPanel = new ImagePanel(imgUrl);
}
}
ImagePanel
public class ImagePanel extends JPanel{
private Image image;
private URL imgUrl;
public ImagePanel(URL url){
this.imgUrl= url;
try{
image = ImageIO.read(imgUrl);
}
catch (IOException e) {
System.out.println("no such file");
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(image!=null){
g.drawImage(image, 10, 12, 128, 128, this);
}
else{
g.drawImage(image, 0, 0, this);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150,150);
}
}
If I would use ImageIcon with an imageUrl. It shows the same error message.
URL imgUrl = getClass().getResource("images/contacts.png");
private ImageIcon adBook = new ImageIcon(imgUrl);
private JLabel mainImg = new JLabel(adBook);