1 /* 2 * VectorGraphics2D: Vector export for Java(R) Graphics2D 3 * 4 * (C) Copyright 2010 Erich Seifert <dev[at]erichseifert.de> 5 * 6 * This file is part of VectorGraphics2D. 7 * 8 * VectorGraphics2D is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU Lesser General Public License as published by 10 * the Free Software Foundation, either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * VectorGraphics2D is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public License 19 * along with VectorGraphics2D. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 package de.erichseifert.vectorgraphics2d; 23 24 import java.awt.Graphics; 25 import java.awt.GraphicsConfiguration; 26 import java.awt.GraphicsDevice; 27 import java.awt.GraphicsEnvironment; 28 import java.awt.HeadlessException; 29 import java.awt.Image; 30 import java.awt.Transparency; 31 import java.awt.image.BufferedImage; 32 import java.awt.image.ColorModel; 33 import java.awt.image.PixelGrabber; 34 35 import javax.swing.ImageIcon; 36 37 /** 38 * Abstract class that contains utility functions for working with graphics. 39 * For example, this includes font handling. 40 */ 41 public abstract class GraphicsUtils { 42 /** 43 * This method returns <code>true</code> if the specified image 44 * has transparent pixels. 45 * Taken from http://www.exampledepot.com/egs/java.awt.image/HasAlpha.html 46 * @param image 47 * @return <code>true</code> if the specified image has transparent pixels, 48 * <code>false</code> otherwise 49 */ 50 public static boolean hasAlpha(Image image) { 51 // If buffered image, the color model is readily available 52 if (image instanceof BufferedImage) { 53 BufferedImage bimage = (BufferedImage) image; 54 return bimage.getColorModel().hasAlpha(); 55 } 56 // Use a pixel grabber to retrieve the image's color model; 57 // grabbing a single pixel is usually sufficient 58 PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); 59 try { 60 pg.grabPixels(); 61 } catch (InterruptedException e) { 62 } 63 // Get the image's color model 64 ColorModel cm = pg.getColorModel(); 65 return cm.hasAlpha(); 66 } 67 68 /** 69 * This method returns a buffered image with the contents of an image. 70 * Taken from http://www.exampledepot.com/egs/java.awt.image/Image2Buf.html 71 * @param image Image ot be converted 72 * @return a buffered image with the contents of the specified image 73 */ 74 public static BufferedImage toBufferedImage(Image image) { 75 if (image instanceof BufferedImage) { 76 return (BufferedImage)image; 77 } 78 // This code ensures that all the pixels in the image are loaded 79 image = new ImageIcon(image).getImage(); 80 // Determine if the image has transparent pixels; for this method's 81 // implementation, see Determining If an Image Has Transparent Pixels 82 boolean hasAlpha = hasAlpha(image); 83 // Create a buffered image with a format that's compatible with the screen 84 BufferedImage bimage = null; 85 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 86 try { 87 // Determine the type of transparency of the new buffered image 88 int transparency = Transparency.OPAQUE; 89 if (hasAlpha) { 90 transparency = Transparency.BITMASK; 91 } 92 // Create the buffered image 93 GraphicsDevice gs = ge.getDefaultScreenDevice(); 94 GraphicsConfiguration gc = gs.getDefaultConfiguration(); 95 bimage = gc.createCompatibleImage( 96 image.getWidth(null), image.getHeight(null), transparency); 97 } catch (HeadlessException e) { 98 // The system does not have a screen 99 } if (bimage == null) { 100 // Create a buffered image using the default color model 101 int type = BufferedImage.TYPE_INT_RGB; 102 if (hasAlpha) { 103 type = BufferedImage.TYPE_INT_ARGB; 104 } 105 bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); 106 } 107 // Copy image to buffered image 108 Graphics g = bimage.createGraphics(); 109 // Paint the image onto the buffered image 110 g.drawImage(image, 0, 0, null); 111 g.dispose(); 112 return bimage; 113 } 114 }