Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Giving an application a window icon in a sensible way

Reply
Thread Tools

Giving an application a window icon in a sensible way

 
 
Twisted
Guest
Posts: n/a
 
      11-20-2006
Hrm. How to go about doing this?

I want to give a Java application a window icon in a manner that is
independent of how it is installed, etc.

The trick is obtaining an Image object for myJFrame.setIconImage().
Loading it from a URL means it won't work without a working network
connection, and I need to host the image somewhere. Every copy of the
app running anywhere in the world will, on startup, hit that host with
a request for the file(!). Loading it from a file path requires a
separate installer that sets the image into a specific directory.
Putting it in a JAR file with the app means learning a big new chunk of
API, plus it won't work when running in the development environment
rather than as a standalone executable JAR.

This suggests doing the ListMessageBundle sort of thing, and somehow
packaging it as a class -- an Image subclass, presumably. Is there a
tool for turning a jpeg, gif, or png into Java source code for an Image
subclass that will, when instantiated, behave as the appropriate jpeg?

I don't have the google-fu to find this -- searches for queries like
"image resource class java" didn't do much for me. Damn, we need *real*
natural language search.

 
Reply With Quote
 
 
 
 
Larry Barowski
Guest
Posts: n/a
 
      11-20-2006

"Twisted" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hrm. How to go about doing this?
>
> I want to give a Java application a window icon in a manner that is
> independent of how it is installed, etc.


The usual method is to use Class.getResource(), which will work
whether the class and icon are in a jar file or not.


 
Reply With Quote
 
 
 
 
Mark Rafn
Guest
Posts: n/a
 
      11-20-2006
Twisted <(E-Mail Removed)> wrote:
>I want to give a Java application a window icon in a manner that is
>independent of how it is installed, etc.
>The trick is obtaining an Image object for myJFrame.setIconImage().
>Loading it from a URL means it won't work without a working network
>connection, and I need to host the image somewhere.


Not at all. A URL doesn't always mean http. It could be a file URL, or a
resource URL inside your jarfile.

>Putting it in a JAR file with the app means learning a big new chunk of
>API, plus it won't work when running in the development environment
>rather than as a standalone executable JAR.


URL imgURL = getClass().getClassLoader().getResource("img/path");

Works if the image is in a jarfile OR a directory on the classpath.
--
Mark Rafn http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      11-20-2006
Larry Barowski wrote:
> "Twisted" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > Hrm. How to go about doing this?
> >
> > I want to give a Java application a window icon in a manner that is
> > independent of how it is installed, etc.

>
> The usual method is to use Class.getResource(), which will work
> whether the class and icon are in a jar file or not.


Where does it look, if the class isn't in a jar file? The directory
with the .class file?

 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      11-20-2006
Mark Rafn wrote:
> Twisted <(E-Mail Removed)> wrote:
> >I want to give a Java application a window icon in a manner that is
> >independent of how it is installed, etc.
> >The trick is obtaining an Image object for myJFrame.setIconImage().
> >Loading it from a URL means it won't work without a working network
> >connection, and I need to host the image somewhere.

>
> Not at all. A URL doesn't always mean http. It could be a file URL, or a
> resource URL inside your jarfile.


File URL and jar I was considering separately.

> URL imgURL = getClass().getClassLoader().getResource("img/path");
>
> Works if the image is in a jarfile OR a directory on the classpath.


Hrm.

Anyway I found something interesting. My google-fu isn't as weak as I
thought -- I was eventually able to dredge up a way to encode icons
into a class file.

It involved exporting the file from photoshop as an XPM, pasting most
of the result into the declaration of a string array, and feeding it to
a class named XImageSource. Of course, this turned out not to be a
standard library class, and tracking it down posed its own challenge
(during which time Firefox crashed for the second time today -- it hit
a crapplet on a page somewhere and died the number. It actually
tottered along sort-of-working until I quit it, but wouldn't load
anything -- and after being quit I couldn't start a new instance until
I terminated a bunch of firefox-related processes that were idling in
the task manager that didn't have any UI or cpu activity!).

Naturally, the XImageSource class had dependencies to track down as
well.

Naturally, one of those dependencies had a bug -- XpmParser. It had

colors = new int[charsPerPixel*2];

where it appeared to need

colors = new int[(charsPerPixel == 2)?65536:256];

since it actually multiplies one char by 256 and adds a second in the
latter case, and was throwing ArrayOutOfBoundsExceptions like they were
going out of style.

Naturally, the author of those classes included a copyright notice and
will probably sue me for copyright infringement for fixing their bug
without permission, too, now that I've copped to this heinous act in a
public newsgroup posting.

But it actually works, and the source code is completely
self-contained, without requiring any extra files besides the .java
files. Which is what I was hoping to accomplish.

Thanks anyway.

 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      11-20-2006
Twisted wrote:
> But it actually works, and the source code is completely
> self-contained, without requiring any extra files besides the .java
> files. Which is what I was hoping to accomplish.


Update: with another hack and more copyright infringement (further
editing the XImageSource code) I now have working transparency as well
-- the first color index set to perfectly white (255, 255, 255) in the
XPM disappears. The thing looks far better in my task list now.

 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      11-20-2006
>> Twisted <(E-Mail Removed)> wrote:
>> >Loading it from a URL means it won't work without a working network
>> >connection, and I need to host the image somewhere.


>Mark Rafn wrote:
>> Not at all. A URL doesn't always mean http. It could be a file URL, or a
>> resource URL inside your jarfile.
>> URL imgURL = getClass().getClassLoader().getResource("img/path");
>> Works if the image is in a jarfile OR a directory on the classpath.


Twisted <(E-Mail Removed)> wrote:
>Anyway I found something interesting. My google-fu isn't as weak as I
>thought -- I was eventually able to dredge up a way to encode icons
>into a class file.


It doesn't need to be a class file for the classloader to find it as a
resource.

>It involved exporting the file from photoshop as an XPM, pasting most
>of the result into the declaration of a string array, and feeding it to
>a class named XImageSource.

....
>Naturally, the XImageSource class had dependencies to track down as
>well.

....
>But it actually works, and the source code is completely
>self-contained, without requiring any extra files besides the .java
>files. Which is what I was hoping to accomplish.


Wouldn't it be MUCH easier to put gif/jpg/png files directly into the jar,
then let the classloader find them using getResource or getResourceAsStream?
--
Mark Rafn (E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      11-20-2006
Twisted wrote:
> Twisted wrote:
>> But it actually works, and the source code is completely
>> self-contained, without requiring any extra files besides the .java
>> files. Which is what I was hoping to accomplish.

>
> Update: with another hack and more copyright infringement (further
> editing the XImageSource code) I now have working transparency as well
> -- the first color index set to perfectly white (255, 255, 255) in the
> XPM disappears. The thing looks far better in my task list now.
>


What's the advantage of this approach compared to using getResource?

Patricia
 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      11-20-2006
Patricia Shanahan wrote:
> What's the advantage of this approach compared to using getResource?


It's built right into the frame subclass file it applies to?
It doesn't need to be put in some jar file, then retrieved, then all
kinds of recovery code written to deal with the IOExceptions that can
result if Something Goes Wrong(tm)?
No need to fiddle with classpath?
Everything is self-contained in the source files?

 
Reply With Quote
 
Twisted
Guest
Posts: n/a
 
      11-20-2006
Mark Rafn wrote:
> Wouldn't it be MUCH easier to put gif/jpg/png files directly into the jar,
> then let the classloader find them using getResource or getResourceAsStream?


First of all, I don't *have* a jar, at least not yet.

Second of all, this is the kind of thing that should really just work
without being able to bomb with IOExceptions and suchlike, in my
opinion. Retrieving even the most basic stuff from a bunch of external
files adds unnecessary points of failure, and gobs of extra recovery
code to check for and cope with anything that goes wrong. This way, it
Just Works(tm).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The giving that keeps on giving sixteenmillion C Programming 0 11-19-2007 10:59 PM
sensible way to pass around values David C ASP .Net 3 08-07-2007 08:34 PM
Multiple image sizes in frame icon (window icon) in Swing Dan Polansky Java 3 02-12-2007 11:15 PM
Is the RequiredFieldValidator a reliable control if supported with sensible form level coding? Hazz ASP .Net 5 04-04-2005 03:16 AM
Reality check: Is it sensible to link XML nodes to other XML nodes in the same file? gavnosis XML 0 08-02-2003 08:22 AM



Advertisments