Java Icon makeIcon(final Class baseClass, final Class rootClass, final String imageFile)

Here you can find the source of makeIcon(final Class baseClass, final Class rootClass, final String imageFile)

Description

Utility method that creates a UIDefaults.LazyValue that creates an ImageIcon UIResource for the specified image file name.

License

Open Source License

Parameter

Parameter Description
baseClass the first class to use in searching for the resource
rootClass an ancestor of <code>baseClass</code> to finish the search at
imageFile the name of the file to be found

Return

a lazy value that creates the ImageIcon UIResource for the image, or null if it cannot be found

Declaration

public static Object makeIcon(final Class<?> baseClass, final Class<?> rootClass, final String imageFile) 

Method Source Code

//package com.java2s;
/*//from  w ww  . ja  v a 2  s. c om
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import javax.swing.*;

import sun.swing.ImageIconUIResource;

import java.io.*;

public class Main {
    /**
     * Utility method that creates a <code>UIDefaults.LazyValue</code> that
     * creates an <code>ImageIcon</code> <code>UIResource</code> for the
     * specified image file name. The image is loaded using
     * <code>getResourceAsStream</code>, starting with a call to that method
     * on the base class parameter. If it cannot be found, searching will
     * continue through the base class' inheritance hierarchy, up to and
     * including <code>rootClass</code>.
     *
     * @param baseClass the first class to use in searching for the resource
     * @param rootClass an ancestor of <code>baseClass</code> to finish the
     *                  search at
     * @param imageFile the name of the file to be found
     * @return a lazy value that creates the <code>ImageIcon</code>
     *         <code>UIResource</code> for the image,
     *         or null if it cannot be found
     */
    public static Object makeIcon(final Class<?> baseClass, final Class<?> rootClass, final String imageFile) {

        return new UIDefaults.LazyValue() {
            public Object createValue(UIDefaults table) {
                /* Copy resource into a byte array.  This is
                 * necessary because several browsers consider
                 * Class.getResource a security risk because it
                 * can be used to load additional classes.
                 * Class.getResourceAsStream just returns raw
                 * bytes, which we can convert to an image.
                 */
                byte[] buffer = java.security.AccessController
                        .doPrivileged(new java.security.PrivilegedAction<byte[]>() {
                            public byte[] run() {
                                try {
                                    InputStream resource = null;
                                    Class<?> srchClass = baseClass;

                                    while (srchClass != null) {
                                        resource = srchClass.getResourceAsStream(imageFile);

                                        if (resource != null || srchClass == rootClass) {
                                            break;
                                        }

                                        srchClass = srchClass.getSuperclass();
                                    }

                                    if (resource == null) {
                                        return null;
                                    }

                                    BufferedInputStream in = new BufferedInputStream(resource);
                                    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
                                    byte[] buffer = new byte[1024];
                                    int n;
                                    while ((n = in.read(buffer)) > 0) {
                                        out.write(buffer, 0, n);
                                    }
                                    in.close();
                                    out.flush();
                                    return out.toByteArray();
                                } catch (IOException ioe) {
                                    System.err.println(ioe.toString());
                                }
                                return null;
                            }
                        });

                if (buffer == null) {
                    return null;
                }
                if (buffer.length == 0) {
                    System.err.println("warning: " + imageFile + " is zero-length");
                    return null;
                }

                return new ImageIconUIResource(buffer);
            }
        };
    }
}

Related

  1. createHistImage(JComponent c, Icon hd, File dir, Color bgColor, String fname)
  2. createRotatedImage(Component c, Icon icon, double rotatedAngle)
  3. extractIconImage(Component component, Icon icon)
  4. genImageResource(Class cls, String icon)
  5. getChatIconImage()
  6. scaleIcon(Image iconImage, int size)
  7. toImage(Icon icon)
  8. toImages(List icons)