package com.drawbridge.utils; import java.awt.Color; import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.HashSet; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import com.drawbridge.Activity; import com.drawbridge.correspondance.Spotlight; import com.drawbridge.correspondance.Spotlightable; public class GraphicUtils { public static Color DIM_COLOR = Color.decode("#D5D5D5"); public static void dimBackground(Spotlightable component) { Color c = Color.decode("#D5D5D5"); component.setBackground(c); } public static void addTimedSpotlight(final Spotlightable s, final HashSet<Spotlight> spotlights) { new Thread(new Runnable() { @Override public void run() { Utils.out.println(this.getClass(), "started at :" + System.currentTimeMillis()); s.setDimBackground(true); s.setSpotlights(spotlights); s.repaint(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } s.setDimBackground(false); s.repaint(); Utils.out.println(this.getClass(), "stopped at :" + System.currentTimeMillis()); } }).start(); } public static BufferedImage loadImageFromResource(String name) { InputStream is = Activity.class.getResourceAsStream(name); BufferedImage returnImage = null; try { returnImage = ImageIO.read(is); is.close(); } catch (IOException e) { Utils.out.println(GraphicUtils.class, "Exception in loadIconFromResource"); e.printStackTrace(); } return returnImage; } public static BufferedImage loadImageFromFile(File file) { BufferedImage returnImage = null; try { returnImage = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); } return returnImage; } public static ImageIcon loadIconFromResource(String name) { InputStream is = Activity.class.getResourceAsStream(name); ImageIcon returnImageIcon = null; try { returnImageIcon = new ImageIcon(ImageIO.read(is)); is.close(); } catch (IOException e) { Utils.out.println(GraphicUtils.class, "Exception in loadIconFromResource"); e.printStackTrace(); } return returnImageIcon; } public static Dimension getScaledDimFitInside(Dimension size, Dimension maxSize) { if (size.getWidth() > 0 && size.getHeight() > 0) { double imageRatio = (double) size.getWidth() / (double) size.getHeight(); double boundsRatio = (double) maxSize.getWidth() / (double) maxSize.getHeight(); if (boundsRatio < imageRatio) { if ((int) imageRatio != 0) { int newHeight = maxSize.width / (int) imageRatio;//user.dir return new Dimension(maxSize.width, newHeight); } else { Utils.err.println(GraphicUtils.class, "Could not get correct size of image so settled for bounds size!"); return new Dimension(maxSize.width, maxSize.height); } } else { int newWidth = (int) (imageRatio * maxSize.height); return new Dimension(newWidth, maxSize.height); } } else return maxSize; } public static Dimension getScaledDimFill(Dimension imageSize, Dimension bounds) { if (imageSize.getWidth() > 0 && imageSize.getHeight() > 0) { //We just need to cover the dimension with the smallest ratio double widthRatio = (double) imageSize.width / (double) bounds.width; double heightRatio = (double) imageSize.height / (double) bounds.height; if(widthRatio > heightRatio){ Utils.out.println(GraphicUtils.class, "height was smaller"); return new Dimension((int)(imageSize.width * ((double) bounds.height / (double) bounds.width)), bounds.height); } else{ Utils.out.println(GraphicUtils.class, "width was smaller"); return new Dimension(bounds.width, (int)(imageSize.height * ((double) bounds.width / (double) bounds.height))); } } else return bounds; } }