001 /* PAVLOV -- Multiple Choice Study System
002 * Copyright (C) 2000 - 2004 T.J. Willis
003 *
004 * This program is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU General Public License
006 * as published by the Free Software Foundation; either version 2
007 * of the License, or (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, write to the Free Software
016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017 *
018 * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/randommedia/ImageIconUtilities.java,v 1.3 2004/05/10 14:58:54 tj_willis Exp $
019 */
020 package net.sourceforge.pavlov.randommedia;
021
022
023 import java.awt.Image;
024 import java.awt.*;
025 import javax.swing.*;
026 import java.net.URL;
027 import org.apache.log4j.*;
028
029 /**
030 * Assorted utilities for ImageIcons.
031 *
032 * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
033 * @version $Revision: 1.3 $
034 */
035 public final class ImageIconUtilities
036 {
037 private static Category cat
038 = Category.getInstance(ImageIconUtilities.class.getName());
039 /**
040 * Poorly named method to get an Image of the desired size from
041 * the given ImageIcon. Returns null for non-positive size.
042 *
043 * @param input an <code>ImageIcon</code> value
044 * @param size an <code>int</code> value
045 * @return an <code>Image</code> value
046 */
047 public static Image formatImageOnly(ImageIcon input, int size) {
048 Image xx = input.getImage();
049 Dimension gg = getScaledDimension(xx, size);
050
051 return xx.getScaledInstance(gg.width, gg.height, Image.SCALE_SMOOTH);
052 }
053
054 /**
055 * Get an ImageIcon of the specified size from the given ImageIcon.
056 * Returns null for non-positive size.
057 * @param input an <code>ImageIcon</code> value
058 * @param size an <code>int</code> value
059 * @return an <code>ImageIcon</code> value
060 */
061 public synchronized static ImageIcon formatImage(ImageIcon input, int size) {
062 if(size<1) return null;
063 Image y = formatImageOnly(input, size);
064 ImageIcon x = new ImageIcon();
065
066 x.setImage(y);
067 return x;
068 }
069
070 /**
071 * Returns a width/height pair that maintains big's aspectRatio, with
072 * neither width or height larger than size. One of width or height
073 * will actually equal size.
074 *
075 * @param big an <code>Image</code> value
076 * @param size an <code>int</code> value
077 * @return a <code>Dimension</code> value
078 */
079 public static Dimension getScaledDimension(Image big, int size) {
080 if(big==null) return null;
081 if(size<1) return null;
082 int x = big.getWidth(null);
083 int y = big.getHeight(null);
084 double fac;
085
086 if (x > y) fac = x / (1.0 * size);
087 else fac = y / (1.0 * size);
088
089 x /= fac;
090 y /= fac;
091 return new Dimension(x, y);
092 }
093
094 /**
095 * Attempts to return an ImageIcon of the specified size with the given URL.
096 * Returns null on failure.
097 * @param myURL a <code>String</code> value
098 * @param size an <code>int</code> value
099 * @return an <code>ImageIcon</code> value
100 */
101 public static ImageIcon getNamedImageIcon(String myURL, int size) {
102 if(size<1) return null;
103 ImageIcon current = getNamedImageIcon(myURL);
104 if(current==null) return null;
105 return formatImage(current, size);
106 }
107
108 /**
109 * Attempts to return the named URL as an ImageIcon. null on failure.
110 *
111 * @param myURL a <code>String</code> value
112 * @return an <code>ImageIcon</code> value
113 */
114 public static ImageIcon getNamedImageIcon(String myURL) {
115 URL crap1=null;
116
117 try {
118 crap1 = new URL(myURL); //crap0.toURL();
119 } catch (java.net.MalformedURLException ex) {
120 cat.warn("Cant get icon: " + myURL,ex);
121 return null;
122 }
123
124 if(crap1==null)
125 {
126 String la = "file:" + myURL;
127 try {
128 crap1 = new URL(la);
129 } catch (java.net.MalformedURLException ex) {
130 cat.warn("Cant get icon: " + la,ex);
131 return null;
132 }
133 }
134 //print("Image fname is " + myName);
135 ImageIcon current = new ImageIcon(crap1);
136 return current;
137 }
138
139 }
140