1

I want to add a picture to my GUI, but it doesn't seem to be able to display the picture. (Maybe it can't find it?) I get no errors, all that happens is a blank JFrame pops up. I'm using Eclipse, and the picture is in the same package I'm working in.

Code:

package josh_package;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import java.awt.Image;
import javax.imageio.ImageIO;


public abstract class TrevCard extends JPanel implements ActionListener{

public static void main(String[] args)
{
    JFrame f = new JFrame("Here's Your Card!");
    JLabel label = new JLabel();  
    label.setIcon(new ImageIcon("Ace.png"));

     f.add(label);
     f.setLocationRelativeTo(null);
     f.setSize(600, 300);
     f.setVisible(true);

System.out.println("This is a program for cards!");
PlayingCard MyCard;
    MyCard = new PlayingCard(5,2);

    System.out.println(MyCard.GetValue());
    System.out.println(MyCard.GetSuit());

}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I am beginning to wonder if people that ask this question ever think to search on it. It must be asked twice a day. – Andrew Thompson Apr 11 '13 at 22:17
  • 1
    I would recommend using `ImageIO` over `ImageIcon` to load your images, `ImageIO` actually throws an exception when something goes wrong. @AndrewThompson Why look when you can ask and we can repeat ourselves :P - Besides, no one elses problems could ever be exactly the same as my own... ;) – MadProgrammer Apr 11 '13 at 23:38
  • @MadProgrammer *"I would typically use .. **`ImageIO.read()`**.."* but for an [animated GIF](http://stackoverflow.com/questions/10836832/show-an-animated-bg-in-swing).. – Andrew Thompson Apr 12 '13 at 00:01
  • @AndrewThompson Good point...but I can't remember the last time I had to use a GIF (sad me ;)) – MadProgrammer Apr 12 '13 at 00:02
  • @MadProgrammer What, not even for a [flying pony](http://stalafos.deviantart.com/art/Pony-GIF-Animation-Fly-258454134)? :( – Andrew Thompson Apr 12 '13 at 00:15
  • @AndrewThompson Check out [this](http://stackoverflow.com/questions/15939188/how-can-i-add-a-picture-to-this-program/15939246#15939246) – MadProgrammer Apr 12 '13 at 00:20
  • @MadProgrammer Huh. I up-voted that without even noticing the images! Time for new (or any) glasses. ;) – Andrew Thompson Apr 12 '13 at 00:36

2 Answers2

2

The ImageIcon constructor you're using takes a file name as argument. This constructor thus tries to find this file on the disk, in the current directory (since the path you passed is a relative path). The current directory is not the directory where the Java source file is. It's the directory from which the java command used to execute your app is launched.

You don't want to use file IO to load your image. Instead, you want this image to be loaded from the classpath (ultimately, from inside the jar containing your packaged application).

To do that, use the constructor taking a URL as argument, and pass the URL of the resource:

new ImageIcon(TrevCard.class.getResource("Ace.png"));

(provided Ace.png is in the same package as the TrevCard class)

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

Instead of

label.setIcon(new ImageIcon("Ace.png"));

Use:

java.net.URL imgURL = TrevCard.class.getResource("Ace.png");
label.setIcon(new ImageIcon(imgURL));
acdcjunior
  • 132,397
  • 37
  • 331
  • 304