• Post Reply Bookmark Topic Watch Topic
  • New Topic

displaying a PNG in a window  RSS feed

 
Manel Ibanez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's my third day working in Java using Swing and the IntelliJ Idea IDE.
I'm trying to do something as simple as displaying a PNG in a window, and I'm doing this:


the problem is that, in the paintComponent method, the Image is never found when I run my app from the IDE, but it is when I run it from Finder in my Mac. Same application. I think it relates to how the application is launched so I guess what's a proper way to refer to a resource file with an image so it can be displayed no matter how the application is launched?
Also if I generate a Jar for the application, as the resource gets into the Jar compressed file, it can't also be loaded. I'm sure I'm doing it wrong... ??

Not sure if I'm explaining my problem properly... I would appreciate it a lot any kind of help.

Thanks!

EDIT: I know I should NOT load images like this on every paintComponent, tha's just a quick and dirty piece of code to show my problem.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best approach is to put the /Resources/ folder into one of your classpath roots. If you were making an executable jar, then that would be the root of the jar file. Then read the image as a resource:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!