Here you can find the source of getImageFromFile(String fileName)
Parameter | Description |
---|---|
fileName | a parameter |
public static ImageIcon getImageFromFile(String fileName)
//package com.java2s; /**/* w w w . j a v a2s . co m*/ * Copyright? 2014-2016 LIST (Luxembourg Institute of Science and Technology), all right reserved. * Authorship : Olivier PARISOT, Yoanne DIDRY * Licensed under GNU General Public License version 3 */ import java.io.*; import javax.swing.ImageIcon; public class Main { /** * * @param fileName * @return */ public static ImageIcon getImageFromFile(String fileName) { try { final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int nRead; byte[] data = new byte[16384]; while ((nRead = is.read(data, 0, data.length)) != -1) { buffer.write(data, 0, nRead); } buffer.flush(); return new ImageIcon(buffer.toByteArray()); } catch (IOException e) { e.printStackTrace(); } return null; } }