![]()
And have: Download icons as .ico files for coding
| Technical difficulties free download | 816 |
| Foxit pdf printer download -reader | 64 |
| Wii mod games download | 853 |
How to Use Icons
Many Swing components, such as labels, buttons, and tabbed panes, can be decorated with an icon a fixed-sized picture. An icon is an object that adheres to the interface. Swing provides a particularly useful implementation of the interface: , which paints an icon from a GIF, JPEG, or PNG image.
Here's a snapshot of an application with three labels, two decorated with an icon:
The program uses one image icon to contain and paint the yellow splats. One statement creates the image icon and two more statements include the image icon on each of the two labels:
The method (used in the preceding snippet) is one we use in many of our code samples. It finds the specified file and returns an for that file, or if that file couldn't be found. Here is a typical implementation:
In the preceding snippet, the first argument to the constructor is relative to the location of the current class, and will be resolved to an absolute URL. The argument is a string that allows assistive technologies to help a visually impaired user understand what information the icon conveys.
Generally, applications provide their own set of images used as part of the application, as is the case with the images used by many of our demos. You should use the method to obtain the path to the image. This allows the application to verify that the image is available and to provide sensible error handling if it is not. When the image is not part of the application, should not be used and the constructor is used directly. For example:
When you specify a filename or URL to an constructor, processing is blocked until after the image data is completely loaded or the data location has proven to be invalid. If the data location is invalid (but non-null), an is still successfully created; it just has no size and, therefore, paints nothing. As shown in the method, it is advisable to first verify that the URL points to an existing file before passing it to the constructor. This allows graceful error handling when the file isn't present. If you want more information while the image is loading, you can register an observer on an image icon by calling its method.
Under the covers, each image icon uses an object to hold the image data.
The rest of this section covers the following topics:
A More Complex Image Icon Example
Here's an application that uses six image icons. Five of them display thumbnail images and the sixth diplays the full size the photograph.
Try this:
Click the Launch button to run IconDemo using Java™ Web Start (download JDK 7 or later). Or, to compile and run the example yourself, consult the example index.
Click any of the thumbnail images to view the full size photographs.
- Hold the mouse over a photograph. A tool tip appears that displays the photograph caption.
IconDemoApp demonstrates icons used in the following ways:
- As a GUI element attached to a button (the thumbnail images on the buttons).
- To display an image (the five photographs).
The photographs are loaded in a separate thread by . The code is shown a little later in this section.
The class, an inner class in , is a descendant of that manages our full size image icon, a thumbnail version, and its description. When the method is called the full size image is loaded into the main display area. Each button has its own instance of which specifies a different image to show.
Loading Images Using getResource
Most often, an image icon's data comes from an image file. There are a number of valid ways that your application's class and image files may be configured on your file server. You might have your class files in a JAR file, or your image files in a JAR file; they might be in the same JAR file, or they might be in different JAR files. The following figures illustrate a few of the ways these files can be configured:
If you are writing a real-world application, it is likely (and recommended) that you put your files into a package. For more information on packages, see Creating and Using Packages in the Learning the Java Language trail. Here are some possible configurations using a package named "omega":
| Class file in directory named . Image in directory. | Class file in directory. Image in JAR file not inside of directory, but created with hierarchy. |
| One big JAR file with class files under directory and image files under directory. |
All seven configurations shown are valid, and the same code reads the image:
The method causes the class loader to look through the directories and JAR files in the program's class path, returning a URL as soon as it finds the desired file. In the example the MyDemo program attempts to load the file from the class. The class loader looks through the directories and JAR files in the program's class path for . If the class loader finds the file, it returns the URL of the JAR file or directory that contained the file. If another JAR file or directory in the class path contains the file, the class loader returns the first instance that contains the file.
Here are three ways to specify the class path:
Using the or command-line argument. For example, in the case where the images are in a JAR file named and the class file is in the current directory:
java -cp .;www.cronistalascolonias.com.ar MyDemo [Microsoft Windows] java -cp ".;www.cronistalascolonias.com.ar" MyDemo [UNIX-emulating shell on Microsoft Windows you must quote the path] java -cp .:www.cronistalascolonias.com.ar MyDemo [UNIX]If your image and class files are in separate JAR files, your command line will look something like:
java -cp .;www.cronistalascolonias.com.ar;www.cronistalascolonias.com.ar MyDemo [Microsoft Windows]In the situation where all the files are in one JAR file, you can use either of the following commands:
java -jar www.cronistalascolonias.com.ar java -cp .;www.cronistalascolonias.com.ar MyApp [Microsoft Windows]For more information, see the JAR Files trail.
In the program's JNLP file (used by Java Web Start). For example, here is the JNLP file used by :
<?xml version="" encoding="utf-8"?> <!-- JNLP File for DragPictureDemo --> <jnlp spec="+" codebase="www.cronistalascolonias.com.ar" href="www.cronistalascolonias.com.ar"> <information> <title>DragPictureDemo</title> <vendor>The Java(tm) Tutorial: Sun Microsystems, Inc.</vendor> <homepage href="www.cronistalascolonias.com.ar#DragPictureDemo"/> <description>DragPictureDemo</description> <description kind="short">A demo showing how to install data transfer on a custom component.</description> <offline-allowed/> </information> <resources> <j2se version="+"/> <jar href="www.cronistalascolonias.com.ar"/><jar href="www.cronistalascolonias.com.ar"/> </resources> <application-desc main-class="DragPictureDemo"/> </jnlp>In this example, the class files and the images files are in separate JAR files. The JAR files are specified using the XML tag.
Setting the environment variable. This last approach is not recommended. If is not set, the current directory (".") followed by the location of the system classes shipped with the JRE are used by default.
Most of the Swing Tutorial examples put the images in an directory under the directory that contains the examples' class files. When we create JAR files for the examples, we keep the same relative locations, although often we put the class files in a different JAR file than the image JAR file. No matter where the class and image files are in the file system in one JAR file, or in multiple JAR files, in a named package, or in the default package the same code finds the image files using .
For more information, see Accessing Resources in a Location-Independent Manner and the Application Development Considerations.
Loading Images Into Applets
Applets generally load image data from the computer that served up the applet. The tag is where you specify information about the images used in the applet. For more information on the tag see Using the APPLET Tag
Improving Perceived Performance When Loading Image Icons
Because the photograph images can be slow to access, uses a to improve the performance of the program as perceived by the user.
Background image loading the program uses a www.cronistalascolonias.com.arorker object to load each photograph image and compute it's thumbnail in a background thread. Using a prevents the program from appearing to freeze up while loading and scaling the images.
Here's the code to process each image:
SwingWorker invokes the method in a background thread. The method places a full size image, thumbnail size image and caption into a object. The SwingWorker then delivers the to the method. The method executes on the event dispatch thread and updates the GUI by adding a button to the toolbar. has a constructor that takes an action object. The action object determines a number of the button's properties. In our case the button icon, the caption and the action to be performed when the button is pressed is all determined by the .
Overhead this program eventually loads all the source images into memory. This may not be desirable in all situations. Loading a number of very large files could cause the program to allocate a very large amount or memory. Care should be taken to manage the number and size of images that are loaded.
As with all performance-related issues, this technique is applicable in some situations and not others. Also the technique described here is designed to improve the program's perceived performance, but does not necessarily impact its real performance.
Creating a Custom Icon Implementation
The method returns null when it cannot find an image, but what should the program do then? One possibility would be to ignore that image and move on. Another option would be to provide some sort of default icon to display when the real one cannot be loaded. Making another call to might result in another null so using that is not a good idea. Instead lets create a custom implementation.
You can find the implementation of the custom icon class in . Here are the interesting parts of its code:
The method is passed a object. The object gives the method access to the entire Java2D API. For more information about painting and Java2D, see Performing Custom Painting.
The following code demonstrates how the class is used in the method.
Using a custom icon has a few implications:
Because the icon's appearance is determined dynamically, the icon painting code can use any information component and application state, for example to determine what to paint.
- Depending on the platform and the type of image, you may get a performance boost with custom icons, since painting simple shapes can sometimes be faster than copying images.
Because does not do any file I/O there is no need for separate threads to load the image.
The Image Icon API
The following tables list the commonly used constructors and methods. Note that is not a descendent of or even of .
The API for using image icons falls into these categories:
| Method or Constructor | Purpose |
|---|---|
| ImageIcon() ImageIcon(byte[]) ImageIcon(byte[], String) ImageIcon(Image) ImageIcon(Image, String) ImageIcon(String) ImageIcon(String, String) ImageIcon(URL) ImageIcon(URL, String) | Create an instance, initializing it to contain the specified image. The first argument indicates the source image, byte array, filename, or URL from which the image icon's image should be loaded. The source must be in a format supported by the class: namely GIF, JPEG, or PNG. The second argument, when present, provides a description for the image. The description may also be set via and provides useful textual information for assistive technologies. |
| void setImage(Image) Image getImage() | Set or get the image displayed by the image icon. |
| void paintIcon(Component, Graphics, int, int) | Paint the image icon's image in the specified graphics context. You would override this only if you're implementing a custom icon that performs its own painting. The object is used as an image observer. You can rely on the default behavior provided by class, and pass in any component. The two arguments specify the top-left corner where the icon is painted. |
| URL getResource(String) in (www.cronistalascolonias.com.aroader) | Find the resource with the given name. For more information, see Loading Images Using getResource. |
| InputStream getResourceAsStream(String) in (www.cronistalascolonias.com.aroader) | Find the resource with the given name and return an input stream for reading the resource. For more information, see the Loading Images Into Applets discussion. |
Examples that Use Icons
The following table lists just a few of the many examples that use .
-
-
-