Here you can find the source of getIcon(String f, Class> c)
Parameter | Description |
---|---|
f | The name of the icon file. |
c | The class where it is to be found. |
public static Icon getIcon(String f, Class<?> c)
//package com.java2s; /*//from w ww.ja v a2 s. c o m * Copyright (C) 2015 University of Oregon * * You may distribute under the terms of either the GNU General Public * License or the Apache License, as specified in the LICENSE file. * * For more information, see the LICENSE file. */ import java.io.*; import java.net.*; import javax.swing.Icon; import javax.swing.ImageIcon; import java.awt.Image; import java.awt.Toolkit; public class Main { /** * Get an Icon from the JAR file. * @param f The name of the icon file. * @param c The class where it is to be found. * @return The Icon, or null. */ public static Icon getIcon(String f, Class<?> c) { Icon imageIcon = null; try { URL imageURL = c.getResource(f); if (imageURL == null) { return null; } java.awt.image.ImageProducer I_P; I_P = (java.awt.image.ImageProducer) imageURL.getContent(); Toolkit tk = Toolkit.getDefaultToolkit(); Image img = tk.createImage(I_P); if (img == null) { return null; } imageIcon = new ImageIcon(img); } catch (IOException e) { return null; } return imageIcon; } }