Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading JPGs into Swing Apps

I think I'm suffering from some classpath blues here. I've been following the examples. I read the tutorials. But nothing seems to be working for me.

Let's say I have a JPEG at the following URI: C:\Users\MyUser\someIcon.jpeg

And let's say I have a Swing app that has a JPanel, and I want to add this JPEG into the JPanel (positioning/layout doesn't matter).

How would SO accomplish this? I'm not getting any errors or exceptions, just not seeing the JPEG load. The code I'm trying to use is this:

String someIconUri = "C:\Users\MyUser\someIcon.jpeg";
URL imageUrl = getClass().getResource(someIconUri);
ImageIcon imageIcon = new ImageIcon(imageUrl);
myPanel.add(imageIcon);

Please advise...I've been dealing with this since noon today...and its midnight. Thanks in advance for any nudges in the right direction.

like image 753
IAmYourFaja Avatar asked Dec 28 '22 09:12

IAmYourFaja


1 Answers

String someIconUri = "C:\Users\MyUser\someIcon.jpeg";

No it is not 'some icon URI', it is not a valid URI at all. Come to think of it, it is not even a compilable statement, since the String contains illegal escape chars!

It might be something along the lines of..

String someIconUri = "file:///C:/Users/MyUser/someIcon.jpeg";

But then getResource() is neither needed or useful, since it will only locate resources on the run-time class-path. Just construct an URL directly from the String.


But that is really only 'part of an answer'. Here's why.

In this context, there are basically two types of resources. Application resources and (for want of a better word) User resources.

Application resources.

These resources might consist of things like frame icons, button and menu icons (Action icons), icons for tabs. Help files (and associated images), splash images..

They should be added to a separate Jar (most often) and added to the run-time class-path of the application (either using the manifest or other means like applet element or JNLP file).

Application resources should be accessed by URL, which can be obtained using getResource():

URL iconUrl = this.getClass().getResource("/icons/copy.jpg");

User resources.

The user wants to open (edit/print) an existing text document, make an animated GIF from image frames, edit an image on their file system..

For these types of resources (and presuming the app. is trusted or has no security manager), offer the user a JFileChooser. It will return a specific, existing File or more (depending on how configured and used).

In that case, never convert the File to anything else, just use the instance(s) directly.


Most methods that take input from resources (worth mentioning), will accept a File URL or InputStream.

The last is useful for something generated in memory, or obtained from sources such as sockets or a JNLP API FileContents object. The latter is of special interest to sand-boxed apps. launched or embedded using Java Web Start.

like image 124
Andrew Thompson Avatar answered Jan 10 '23 00:01

Andrew Thompson