Utility class for managing resources such as colors, fonts, images, etc. : Color « 2D Graphics GUI « Java






Utility class for managing resources such as colors, fonts, images, etc.

   
//package com.swtdesigner;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.ImageIcon;

/**
 * Utility class for managing resources such as colors, fonts, images, etc.
 * 
 * This class may be freely distributed as part of any application or plugin.
 * <p>
 * Copyright (c) 2003 - 2004, Instantiations, Inc. <br>All Rights Reserved
 * 
 * @author scheglov_ke
 */
public class SwingResourceManager {
  
  /**
   * Maps image names to images
   */
  private static HashMap<String, Image> m_ClassImageMap = new HashMap<String, Image>();
  
    /**
     * Returns an image encoded by the specified input stream
     * @param is InputStream The input stream encoding the image data
     * @return Image The image encoded by the specified input stream
     */
  private static Image getImage(InputStream is) {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte buf[] = new byte[1024 * 4];
      while (true) {
        int n = is.read(buf);
        if (n == -1)
          break;
        baos.write(buf, 0, n);
      }
      baos.close();
      return Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
    } catch (Throwable e) {
      return null;
    }
  }
  
    /**
     * Returns an image stored in the file at the specified path relative to the specified class
     * @param clazz Class The class relative to which to find the image
     * @param path String The path to the image file
     * @return Image The image stored in the file at the specified path
     */
  public static Image getImage(Class<?> clazz, String path) {
    String key = clazz.getName() + '|' + path;
    Image image = m_ClassImageMap.get(key);
    if (image == null) {
      if ((path.length() > 0) && (path.charAt(0) == '/')) {
        String newPath = path.substring(1, path.length());
        image = getImage(new BufferedInputStream(clazz.getClassLoader().getResourceAsStream(newPath)));
      } else {
        image = getImage(clazz.getResourceAsStream(path));
      }
      m_ClassImageMap.put(key, image);
    }
    return image;
  }
  
    /**
     * Returns an image stored in the file at the specified path
     * @param path String The path to the image file
     * @return Image The image stored in the file at the specified path
     */
  public static Image getImage(String path) {
    return getImage("default", path); //$NON-NLS-1$
  }
  
    /**
     * Returns an image stored in the file at the specified path
     * @param section String The storage section in the cache
     * @param path String The path to the image file
     * @return Image The image stored in the file at the specified path
     */
  public static Image getImage(String section, String path) {
    String key = section + '|' + SwingResourceManager.class.getName() + '|' + path;
    Image image = m_ClassImageMap.get(key);
    if (image == null) {
      try {
        FileInputStream fis = new FileInputStream(path);
        image = getImage(fis);
        m_ClassImageMap.put(key, image);
        fis.close();
      } catch (IOException e) {
        return null;
      }
    }
    return image;
  }
  
    /**
   * Clear cached images in specified section
   * @param section the section do clear
   */
  public static void clearImages(String section) {
    for (Iterator<String> I = m_ClassImageMap.keySet().iterator(); I.hasNext();) {
      String key = I.next();
      if (!key.startsWith(section + '|'))
        continue;
      Image image = m_ClassImageMap.get(key);
      image.flush();
      I.remove();
    }
  }
  
    /**
     * Returns an icon stored in the file at the specified path relative to the specified class
     * @param clazz Class The class relative to which to find the icon
     * @param path String The path to the icon file
     * @return Icon The icon stored in the file at the specified path
     */
  public static ImageIcon getIcon(Class<?> clazz, String path) {
    return getIcon(getImage(clazz, path));
  }
  
    /**
     * Returns an icon stored in the file at the specified path
     * @param path String The path to the icon file
     * @return Icon The icon stored in the file at the specified path
     */
  public static ImageIcon getIcon(String path) {
    return getIcon("default", path); //$NON-NLS-1$
  }
  
    /**
     * Returns an icon stored in the file at the specified path
     * @param section String The storage section in the cache
     * @param path String The path to the icon file
     * @return Icon The icon stored in the file at the specified path
     */
  public static ImageIcon getIcon(String section, String path) {
    return getIcon(getImage(section, path));
  }

    /**
     * Returns an icon based on the specified image
     * @param image Image The original image
     * @return Icon The icon based on the image
     */
  public static ImageIcon getIcon(Image image) {
    if (image == null)
      return null;
    return new ImageIcon(image);
  }
}

   
    
    
  








Related examples in the same category

1.Color class is used to work with colors in Java 2D
2.Color Utilities: common color operations
3.Color Difference
4.Rainbow ColorRainbow Color
5.XOR colorXOR color
6.Color Gradient
7.Common color utilities
8.Drawing with Color
9.Color fading animation
10.140 colors - defined for X Window System listed in O'Reilly html pocket reference 87pp
11.Color Util
12.Color Factory
13.An efficient color quantization algorithm
14.Utility for checking colors given either hexa or natural language string descriptions.
15.Derives a color by adding the specified offsets to the base color's hue, saturation, and brightness values
16.Map colors into names and vice versa.
17.Converts a given string into a color.
18.If the color is equal to one of the defined constant colors, that name is returned instead.
19.Converts the String representation of a color to an actual Color object.
20.Returns blue-yellow-red color scale
21.Returns green-yellow-red-black color scale
22.Returns black-red-yellow-green color scale
23.Returns color based on 0-9 scale ranging from green to yellow
24.Returns color based on 0-9 scale ranging from yellow to red
25.Returns color based on 0-9 scale ranging from black to green
26.Returns n-dimensional array of colors for given nx3 integer array of RGB values
27.Web color enum
28.Make a color transparent
29.Return a Color object given a string representation of it
30.Return a string representation of a color
31.Serializes a color to its HTML markup (e.g. "#ff0000" for red)
32.Parses a java.awt.Color from an HTML color string in the form '#RRGGBB' where RR, GG, and BB are the red, green, and blue bytes in hexadecimal form
33.Performs a somewhat subjective analysis of a color to determine how dark it looks to a user
34.Lightens a color by a given amount
35.Darkens a color by a given amount
36.Blend two colors
37.Utility for working with natively-ordered integer-packed RGBA-format colours.
38.HSV to RGB
39.A widget to manipulate an RGBA colour.