| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivegui.component; |
| 16 | |
| 17 | import java.awt.Graphics; |
| 18 | import java.awt.Image; |
| 19 | import java.awt.image.BufferedImage; |
| 20 | import java.net.URL; |
| 21 | |
| 22 | import javax.swing.Icon; |
| 23 | import javax.swing.ImageIcon; |
| 24 | |
| 25 | import org.apache.hivemind.Resource; |
| 26 | |
| 27 | /** |
| 28 | * Provides miscellaneous utility functions for handling icons. |
| 29 | * There is no need to instantiate this class since it only provides |
| 30 | * static methods. |
| 31 | * @author Jean-Francois Poilpret |
| 32 | */ |
| 33 | public class IconTools |
| 34 | { |
| 35 | protected IconTools() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Loads an icon from a HiveMind Resource. |
| 41 | * @param resource HiveMind resource referring to the icon file |
| 42 | * @return the image that was loaded from <code>resource</code> |
| 43 | */ |
| 44 | static public ImageIcon loadIcon(Resource resource) |
| 45 | { |
| 46 | URL url = resource.getResourceURL(); |
| 47 | if (url != null) |
| 48 | { |
| 49 | return new ImageIcon(url); |
| 50 | } |
| 51 | String file = resource.getPath(); |
| 52 | if (file != null) |
| 53 | { |
| 54 | return new ImageIcon(file); |
| 55 | } |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Loads an icon from a file name. |
| 61 | * Works even for files that are located in a JAR file. |
| 62 | * <p> |
| 63 | * It is strongly recommended to always use lowercase names for image |
| 64 | * files (both in the OS and in the paths used inside the code). |
| 65 | * File paths must be relatives (else this will not work from within |
| 66 | * a JAR file). |
| 67 | * @param file relative path (all in lowercase) to the image file |
| 68 | * @return the image that was loaded from <code>file</code> |
| 69 | */ |
| 70 | public static ImageIcon loadIcon(String file) |
| 71 | { |
| 72 | ImageIcon icon = null; |
| 73 | if (file != null) |
| 74 | { |
| 75 | // Use the System ClassLoader to load the file (to ensure that this |
| 76 | // will work even for a file in a JAR file) |
| 77 | // URL url = ClassLoader.getSystemResource(file); |
| 78 | URL url = Thread.currentThread().getContextClassLoader().getResource(file); |
| 79 | if (url != null) |
| 80 | { |
| 81 | icon = new ImageIcon(url); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | icon = new ImageIcon(file); |
| 86 | } |
| 87 | } |
| 88 | return icon; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Scale down an Icon to a new size. This method is very useful to build a |
| 93 | * thumbnail for an Icon. The method keeps the aspect ratio of the original |
| 94 | * Icon. |
| 95 | * @param source original icon to scale down |
| 96 | * @param newSize new size to which the icon should be scaled |
| 97 | * @return a new icon which is a scaled copy of the source icon |
| 98 | */ |
| 99 | public static Icon scaleIcon(Icon source, int newSize) |
| 100 | { |
| 101 | if (source == null) |
| 102 | { |
| 103 | return null; |
| 104 | } |
| 105 | if ( (source.getIconWidth() <= newSize) |
| 106 | && (source.getIconHeight() <= newSize)) |
| 107 | { |
| 108 | return source; |
| 109 | } |
| 110 | |
| 111 | // Prepare an offscreen pixmap |
| 112 | Image image = new BufferedImage(source.getIconWidth(), |
| 113 | source.getIconHeight(), |
| 114 | BufferedImage.TYPE_4BYTE_ABGR); |
| 115 | // Draw the icon inside it |
| 116 | Graphics g = image.getGraphics(); |
| 117 | source.paintIcon(null, g, 0, 0); |
| 118 | return scaleIcon(image, newSize); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Scale down an Image to a new size. This method is very useful to build a |
| 123 | * thumbnail for an Icon. The method keeps the aspect ratio of the original |
| 124 | * Icon. |
| 125 | * @param source original image to scale down |
| 126 | * @param newSize new size to which the image should be scaled |
| 127 | * @return a new icon which is a scaled copy of the source image |
| 128 | */ |
| 129 | public static Icon scaleIcon(Image source, int newSize) |
| 130 | { |
| 131 | if (source == null) |
| 132 | { |
| 133 | return null; |
| 134 | } |
| 135 | if ( (source.getWidth(null) <= newSize) |
| 136 | && (source.getHeight(null) <= newSize)) |
| 137 | { |
| 138 | return new ImageIcon(source); |
| 139 | } |
| 140 | |
| 141 | int width = -1; |
| 142 | int height = -1; |
| 143 | if (source.getWidth(null) <= source.getHeight(null)) |
| 144 | { |
| 145 | height = newSize; |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | width = newSize; |
| 150 | } |
| 151 | Image target = source.getScaledInstance(width, height, Image.SCALE_DEFAULT); |
| 152 | return new ImageIcon(target); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Scale an Image to a new size. The method keeps the aspect ratio of the |
| 157 | * original Image. |
| 158 | * @param source original image to scale down |
| 159 | * @param newSize new size to which the image should be scaled |
| 160 | * @return a new image which is a scaled copy of the source image |
| 161 | */ |
| 162 | public static Image scaleImage(Image source, int newSize) |
| 163 | { |
| 164 | if (source == null) |
| 165 | { |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | int width = -1; |
| 170 | int height = -1; |
| 171 | if (source.getWidth(null) <= source.getHeight(null)) |
| 172 | { |
| 173 | height = newSize; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | width = newSize; |
| 178 | } |
| 179 | return source.getScaledInstance(width, height, Image.SCALE_DEFAULT); |
| 180 | } |
| 181 | |
| 182 | static public ImageIcon centerIcon(Icon source, int newWidth, int newHeight) |
| 183 | { |
| 184 | Image image = new BufferedImage(newWidth, |
| 185 | newHeight, |
| 186 | BufferedImage.TYPE_4BYTE_ABGR); |
| 187 | Graphics g = image.getGraphics(); |
| 188 | source.paintIcon( null, |
| 189 | g, |
| 190 | (newWidth - source.getIconWidth()) / 2, |
| 191 | (newHeight - source.getIconHeight()) / 2); |
| 192 | return new ImageIcon(image); |
| 193 | } |
| 194 | } |