package org.freehep.util.images;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

import javax.swing.Icon;
import javax.swing.ImageIcon;

public class TempImageHandler {

    private static Image brokenImage;
    private static Cursor brokenCursor;
    private static String[] imageExtensions = { ".gif", ".png" };
    private static Hashtable imageCache = new Hashtable();
    private static Hashtable cursorCache = new Hashtable();
    private static Hashtable iconCache = new Hashtable();
    
    // only static methods
    protected TempImageHandler() {
    }

    static {
        Toolkit toolkit = Toolkit.getDefaultToolkit(); 
        
        byte[] brokenImageBytes = getImageBytes("BrokenImage.gif",
                                                TempImageHandler.class);
        if (brokenImageBytes != null) {
            brokenImage = toolkit.createImage(brokenImageBytes);
        }
        
        if (brokenImage == null) {
            throw new RuntimeException("Could not load BrokenIcon");
        }
        
        byte[] brokenCursorBytes =  getImageBytes("BrokenCursor.gif", 
                                                  TempImageHandler.class);
        if (brokenCursorBytes != null) {
            Image brokenCursorImage = toolkit.createImage(brokenCursorBytes);
            if (brokenCursorImage != null) {
                brokenCursor = toolkit.createCustomCursor(brokenCursorImage, 
                                                          new Point(0,0), 
                                                          "BrokenCursor");
            }
        }
        
        if (brokenCursor == null) {
            throw new RuntimeException("Could not load BrokenCursor");
        }
    }
    
    public static Image getImage(String name, Class clazz) {
        Image image = (Image)imageCache.get(name);
        if (image == null) {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            int i = 0;
            while ((image == null) && (i < imageExtensions.length)) {
                byte[] imageBytes = getImageBytes(name+imageExtensions[i], 
                                                  clazz);
                if (imageBytes != null) {
                    image = toolkit.createImage(imageBytes);
                }
                i++;
            }
            if (image != null) {
                imageCache.put(name, image);
            } else {
                image = brokenImage;
            }
        }
        return image;
    }

    public static Cursor getCursor(String name, Class clazz) {
        return getCursor(name, clazz, 0, 0);
    }    
   
    public static Cursor getCursor(String name, Class clazz, int x, int y) {
        Cursor cursor = (Cursor)cursorCache.get(name);

        if (cursor == null) {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Image image = getImage(name, clazz);

            // Make this into an icon image, so that we don't have to deal
            // with image observers.
            ImageIcon icon = new ImageIcon(image);
            int iconWidth = icon.getIconWidth();
            int iconHeight = icon.getIconHeight();

            // Get the best size for the cursor.
            Dimension bestSize = 
                toolkit.getBestCursorSize(icon.getIconWidth(),
                                          icon.getIconHeight());
            int cursorWidth = bestSize.width;
            int cursorHeight = bestSize.height;

            if (cursorWidth0) {
                ostream.write(buffer,0,bytesRead);
                bytesRead = stream.read(buffer);
            }
            stream.close();

            // Convert to a byte array and return it.
            return ostream.toByteArray();
        } catch (IOException x) {

            return null;
        }
    }
}

 

Ads help maintain this website.