Here you can find the source of getImageIcon(final Class baseClass, final String image)
public static ImageIcon getImageIcon(final Class baseClass, final String image)
//package com.java2s; /*/*from w w w . ja va2s . c o m*/ * @(#)SwingHelpUtilities.java 1.10 06/10/30 * * Copyright (c) 2006 Sun Microsystems, Inc. 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. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; import javax.swing.ImageIcon; public class Main { /** * Create an Icon from a given resource. * * This works uisng getResourceAsStream() because several browsers do not * correctly implement getResource(). * * This method may change... */ public static ImageIcon getImageIcon(final Class baseClass, final String image) { if (image == null) { return null; } final byte[][] buffer = new byte[1][]; try { InputStream resource = baseClass.getResourceAsStream(image); if (resource == null) { return null; } BufferedInputStream in = new BufferedInputStream(resource); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); buffer[0] = new byte[1024]; int n; while ((n = in.read(buffer[0])) > 0) { out.write(buffer[0], 0, n); } in.close(); out.flush(); buffer[0] = out.toByteArray(); } catch (IOException ioe) { System.err.println(ioe.toString()); return null; } if (buffer[0] == null) { System.err.println(baseClass.getName() + "/" + image + " not found."); return null; } if (buffer[0].length == 0) { System.err.println("warning: " + image + " is zero-length"); return null; } return new ImageIcon(buffer[0]); } }