//Author: ^-^Veerle^-^ import java.awt.Toolkit; import java.awt.Image; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.File; import java.io.IOException; import java.io.FileNotFoundException; import javax.swing.ImageIcon; public abstract class dataManager { public static ImageIcon createImageIcon (Object parentClass, String strFile, String strDes) throws Exception { return new ImageIcon(Toolkit.getDefaultToolkit().createImage(getFileByteArray(parentClass, strFile)), strDes); } public static ImageIcon createImageIcon (File imageFile, String strDes) throws Exception { return new ImageIcon(Toolkit.getDefaultToolkit().createImage(getFileByteArray(imageFile)), strDes); } public static ImageIcon createImageIcon (byte[] imageData, String strDes) { return new ImageIcon(Toolkit.getDefaultToolkit().createImage(imageData), strDes); } public static Image createImage (Object parentClass, String strFile) throws Exception { return Toolkit.getDefaultToolkit().createImage(getFileByteArray(parentClass, strFile)); } public static Image createImage (File imageFile) throws Exception { return Toolkit.getDefaultToolkit().createImage(getFileByteArray(imageFile)); } public static Image createImage (byte[] imageData) { return Toolkit.getDefaultToolkit().createImage(imageData); } private static byte[] readInStream (BufferedInputStream inStream) throws Exception { byte[] buff = null; try { buff = new byte[inStream.available()]; inStream.read(buff); inStream.close(); } catch (IOException ex) { throw new Exception("Error when trying to read the file into a byte array\n" + ex.getMessage()); } return buff; } public static byte[] getFileByteArray (Object parentClass, String strFile) throws Exception { BufferedInputStream inStream = null; try { inStream = new BufferedInputStream(parentClass.getClass().getResourceAsStream(strFile)); } catch (NullPointerException ex) { throw new Exception("Could not open file\n" + strFile + "\nBecause the file either does not exist, or can not be read"); } return readInStream(inStream); } public static byte[] getFileByteArray (File dataFile) throws Exception { BufferedInputStream inStream = null; try { inStream = new BufferedInputStream(new FileInputStream(dataFile)); } catch (SecurityException ex) { throw new Exception("Could not open file\n" + dataFile.getPath() + "\nBecause the security manager denied access"); } catch (FileNotFoundException ex) { throw new Exception("Could not open file\n" + dataFile.getPath() + "\nBecause the file either does not exist, or can not be read"); } return readInStream(inStream); } }