- Tour the Icon Resource Format
- Accessing .ICO Files with the Ico Library
- Viewing Windows Icons with IcoViewer
- Conclusion
Viewing Windows Icons with IcoViewer
After creating the Ico library, I created an IcoViewer Swing application to test this library. This application subsequently evolved into a useful tool for viewing any of the icon images in the .ICO file whose path and name combination is specified as the application’s single command-line argument. Figure 2 shows IcoViewer’s GUI.
Figure 2 IcoViewer lets you view any .ICO image. (Icon image courtesy of VistaICO.com.)
The GUI presents a scrollable area that displays an icon image. Scrollbars appear only if you encounter an icon image whose dimensions exceed 256×256 pixels (which is probably rare). The GUI also presents a combo box that identifies the various images via their dimensions and number of colors. Select a combo box item, and the corresponding image appears.
IcoViewer’s IcoViewer.java source code (in this article’s code archive) illustrates how easy it is to work with the Ico library. The main() method executes ico = new Ico (args [0]); to create an Ico object; IcoViewer’s constructor uses this object to create the contents of the combo box, and (from within the combo box’s item listener) to specify a new image for display:
String [] resolutions = new String [ico.getNumImages ()]; for (int i = 0; i < resolutions.length; i++) resolutions [i] = ico.getImage (i).getWidth ()+"x"+ ico.getImage (i).getHeight ()+": "+ (ico.getNumColors (i) == 0 ? "32-bit color" : ico.getNumColors (i)+" colors"); final JComboBox cbResolutions = new JComboBox (resolutions); // ... ItemListener il; il = new ItemListener () { public void itemStateChanged (ItemEvent ie) { if (ie.getStateChange () == ItemEvent.SELECTED) { JComboBox cb = (JComboBox) ie.getSource (); int index = cb.getSelectedIndex (); lblImage.setIcon (new ImageIcon (ico.getImage (index))); } } }; cbResolutions.addItemListener (il);
To build the IcoViewer application and Ico library, follow these steps:
- Unzip this article’s code archive.
- Make the unarchived IcoViewer directory current.
- Invoke the following command (assuming Windows) to compile all source code:
javac -cp ..\ IcoViewer.java
- If compilation succeeds, invoke the following command to run IcoViewer (where x identifies some .ICO file’s path and name):
java -cp ..\;. IcoViewer x.ico