1

For a project I am working on, I have made a Bingo Card. The basic functionality is that a Card is randomly generated and displayed in STD out as well as in a Swing GUI I made by hand. This application has multiple java files within.

BingoFinal.java - The main file. This is what's run when the program is run. Bingo_Card.java - creates the Bingo card, prints it to STD out, and checks for bingo BingoBG.java - Draws the background of the GUI with 2D graphics DrawBingoCard.java - Calls BingoBG and also creates 25 labels with the board values. When Bingo_Card finds a matching number(inputted by STD IN), it no longer prints the number, and prints Chip.png(which is in the same package folder as the java files), a bingo chip image, making it look covered.

This works flawlessly when I run it through NetBeans, but when I clean and build it, then run the jar in terminal, everything works except for displaying the bingo chip image. Does anybody know why that would happen?

EDIT: Here is how I load the Image

ImageIcon chip; //declare ImageIcon
chip = new ImageIcon("chip75.png"); //define it as chip75.png. It is stored in package folder
JLabel B1Chip; //declare empty Label
B1Chip = new JLabel(chip); //define the Label with just the ImageIcon
B1Chip.setBounds(22, 112, chip.getIconWidth(), chip.getIconHeight()); //place at (22,112)
frame.add(B1Chip, null); //Add to frame
Michael Dornisch
  • 177
  • 1
  • 2
  • 8

2 Answers2

3

You should be accessing the images, by using :

ImageIcon chip = new ImageIcon(ClassName.class.getResource("/chip75.png"))

More information can be found on the info page of tag: embedded-resource A detailed answer regarding adding images to the NetBeans Project is mentioned on the info-page link.

More importantly, it would be wise, that you use ImageIO.read, since atleast it will let you know, if something goes wrong, by throwing IOException

Throws:

IllegalArgumentException - if input is null.
IOException - if an error occurs during reading.

ImageIcon on the other hand will hide the exception if any :-)

Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
0

copy the image files into jar or give absolute path of the image files

Crickcoder
  • 2,135
  • 4
  • 22
  • 36