package utils; import java.awt.*; import java.awt.image.*; import java.io.*; import java.text.*; import java.util.*; import javax.imageio.*; public class Utils { private static boolean RUNNING_FROM_JAR; private static Utils classLoaderReference = null; public static GraphicsConfiguration gc; //this constructor should never be called and is only used //internally for getting a reference to classLoader public Utils() { } public static void init(GraphicsConfiguration gc, boolean jar) { Utils.gc = gc; Utils.RUNNING_FROM_JAR = jar; } public static String dateString() { return new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date()); } private static DisplayMode getBestDisplayMode(GraphicsDevice device, int xRes, int yRes) { DisplayMode[] BEST_DISPLAY_MODES = new DisplayMode[] { new DisplayMode(xRes, yRes, 32, 0), new DisplayMode(xRes, yRes, 16, 0), new DisplayMode(xRes, yRes, 8, 0) }; for (int x = 0; x < BEST_DISPLAY_MODES.length; x++) { DisplayMode[] modes = device.getDisplayModes(); for (int i = 0; i < modes.length; i++) { if(modes[i].getWidth() == BEST_DISPLAY_MODES[x].getWidth() && modes[i].getHeight() == BEST_DISPLAY_MODES[x].getHeight() && modes[i].getBitDepth() == BEST_DISPLAY_MODES[x].getBitDepth()) return BEST_DISPLAY_MODES[x]; } } return null; } public static void chooseBestDisplayMode(GraphicsDevice device, int xRes, int yRes) { DisplayMode best = getBestDisplayMode(device, xRes, yRes); if (best != null) device.setDisplayMode(best); } public static BufferedReader loadTextFile(String fileName) throws IOException { if(RUNNING_FROM_JAR) { if(classLoaderReference == null) classLoaderReference = new Utils(); return new BufferedReader(new InputStreamReader(classLoaderReference.getClass().getResourceAsStream("/"+fileName))); }else return new BufferedReader(new FileReader(fileName)); } public static Font loadFont(String fileName) { try { if(RUNNING_FROM_JAR) { if(classLoaderReference == null) classLoaderReference = new Utils(); return Font.createFont(Font.TRUETYPE_FONT, classLoaderReference.getClass().getResourceAsStream("/"+fileName)); }else return Font.createFont(Font.TRUETYPE_FONT, new File(fileName)); }catch(FontFormatException ffe) { ffe.printStackTrace(); }catch(IOException ioe) { ioe.printStackTrace(); } return null; } public static BufferedImage loadImg(String fileName) { BufferedImage init; BufferedImage accelerated; try { if(RUNNING_FROM_JAR) { if(classLoaderReference == null) classLoaderReference = new Utils(); init = ImageIO.read(classLoaderReference.getClass().getResource("/images/" + fileName)); }else init = ImageIO.read(new File("images/" + fileName)); accelerated = gc.createCompatibleImage(init.getWidth(), init.getHeight(), Transparency.TRANSLUCENT); accelerated.getGraphics().drawImage(init, 0, 0, null); return accelerated; }catch(IOException ioe) { ioe.printStackTrace(); } return null; } public static BufferedImage loadTransImg(String fileName, Color bgColor, Color shadowColor) { BufferedImage init; BufferedImage accelerated; try { if(RUNNING_FROM_JAR) init = ImageIO.read(classLoaderReference.getClass().getResource("/images/" + fileName)); else init = ImageIO.read(new File("images/" + fileName)); init = makeColorTransparent(init, bgColor, 0x0); init = makeColorTransparent(init, shadowColor, 0xA1000000); accelerated = gc.createCompatibleImage(init.getWidth(), init.getHeight(), Transparency.TRANSLUCENT); accelerated.getGraphics().drawImage(init, 0, 0, null); return accelerated; }catch(IOException ioe) { ioe.printStackTrace(); } return null; } public static BufferedImage makeColorTransparent(BufferedImage image, Color color, int newColor) { BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = dimg.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(image, null, 0, 0); g.dispose(); for(int i = 0; i < dimg.getHeight(); i++) { for(int j = 0; j < dimg.getWidth(); j++) { if(dimg.getRGB(j, i) == color.getRGB()) { dimg.setRGB(j, i, newColor); } } } return dimg; } public static BufferedImage brightenImage(BufferedImage image) { BufferedImage dimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB); Graphics2D g = dimg.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(image, null, 0, 0); g.dispose(); Color c, newC; for(int i = 0; i < dimg.getHeight(); i++) { for(int j = 0; j < dimg.getWidth(); j++) { c = new Color(dimg.getRGB(j, i), true); newC = c.brighter().brighter(); dimg.setRGB(j, i, new Color(newC.getRed(), newC.getGreen(), newC.getBlue(), c.getAlpha()).getRGB()); } } return dimg; } public static int getRack(int loc) { return (loc >> 24) & 0xFF; } public static int getSection(int loc) { return (loc >> 16) & 0xFF; } public static int getColumn(int loc) { return (loc >> 8) & 0xFF; } public static int getRow(int loc) { return loc & 0xFF; } public static int getLoc(int rack, int section, int column, int row) { return (rack << 24) + (section << 16) + (column << 8) + row; } }