| 1 |
/* |
| 2 |
* ImageUtilities.java |
| 3 |
* |
| 4 |
* Created on March 16, 2007, 4:34 PM |
| 5 |
* |
| 6 |
* Copyright 2006-2007 Nigel Hughes |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 9 |
* in compliance with the License. You may obtain a copy of the License at http://www.apache.org/ |
| 10 |
* licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 |
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language |
| 13 |
* governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
package org.pushingpixels.lafwidget.contrib.blogofbug.utility; |
| 17 |
|
| 18 |
import java.awt.Color; |
| 19 |
import java.awt.Font; |
| 20 |
import java.awt.Graphics; |
| 21 |
import java.awt.Graphics2D; |
| 22 |
import java.awt.GraphicsConfiguration; |
| 23 |
import java.awt.GraphicsEnvironment; |
| 24 |
import java.awt.Image; |
| 25 |
import java.awt.RenderingHints; |
| 26 |
import java.awt.Transparency; |
| 27 |
import java.awt.font.FontRenderContext; |
| 28 |
import java.awt.font.LineBreakMeasurer; |
| 29 |
import java.awt.font.TextAttribute; |
| 30 |
import java.awt.font.TextLayout; |
| 31 |
import java.awt.image.BufferedImage; |
| 32 |
import java.io.IOException; |
| 33 |
import java.net.MalformedURLException; |
| 34 |
import java.net.URL; |
| 35 |
import java.text.AttributedCharacterIterator; |
| 36 |
import java.text.AttributedString; |
| 37 |
import java.util.Hashtable; |
| 38 |
import java.util.LinkedList; |
| 39 |
import javax.imageio.ImageIO; |
| 40 |
import javax.swing.JComponent; |
| 41 |
|
| 42 |
/** |
| 43 |
* Static class with utility methods for images |
| 44 |
* |
| 45 |
* @author nigel |
| 46 |
*/ |
| 47 |
public class ImageUtilities { |
| 48 |
|
| 49 |
/** |
| 50 |
* Creates an image compatible with the current display |
| 51 |
* |
| 52 |
* @return A BufferedImage with the appropriate color model |
| 53 |
*/ |
| 54 |
public static BufferedImage createCompatibleImage(int width, int height){ |
| 55 |
GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); |
| 56 |
|
| 57 |
//Create a buffered image which is the right (translucent) format for the current graphics device, this |
| 58 |
//should ensure the fastest possible performance. Adding on some extra height to make room for the reflection |
| 59 |
return configuration.createCompatibleImage(width,height, Transparency.TRANSLUCENT); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Loads an image in a format compatible with the current display |
| 64 |
* |
| 65 |
* |
| 66 |
* @return A BufferedImage with the appropriate color model |
| 67 |
*/ |
| 68 |
public static BufferedImage loadCompatibleImage(String imageURL){ |
| 69 |
Image image = null; |
| 70 |
try { |
| 71 |
image = ImageIO.read(new URL(imageURL)); |
| 72 |
} catch (MalformedURLException ex) { |
| 73 |
ex.printStackTrace(); |
| 74 |
return null; |
| 75 |
} catch (IOException ex) { |
| 76 |
ex.printStackTrace(); |
| 77 |
return null; |
| 78 |
} |
| 79 |
if (image==null){ |
| 80 |
return null; |
| 81 |
} |
| 82 |
|
| 83 |
GraphicsConfiguration configuration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); |
| 84 |
|
| 85 |
//Create a buffered image which is the right (translucent) format for the current graphics device, this |
| 86 |
//should ensure the fastest possible performance. Adding on some extra height to make room for the reflection |
| 87 |
BufferedImage originalImage = configuration.createCompatibleImage(image.getWidth(null),image.getHeight(null), Transparency.TRANSLUCENT); |
| 88 |
|
| 89 |
//Blit the loaded image onto the optimized surface by creating a graphics context for the new image |
| 90 |
Graphics2D g = originalImage.createGraphics(); |
| 91 |
//Draw the original image |
| 92 |
g.drawImage(image, 0, 0, null); |
| 93 |
g.dispose(); |
| 94 |
return originalImage; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Produces a resized image that is of the given dimensions |
| 99 |
* |
| 100 |
* @param image The original image |
| 101 |
* @param width The desired width |
| 102 |
* @param height The desired height |
| 103 |
* @return The new BufferedImage |
| 104 |
*/ |
| 105 |
public static BufferedImage scaledImage(BufferedImage image, int width, int height){ |
| 106 |
BufferedImage newImage = createCompatibleImage(width,height); |
| 107 |
Graphics graphics = newImage.createGraphics(); |
| 108 |
|
| 109 |
graphics.drawImage(image,0,0,width,height,null); |
| 110 |
|
| 111 |
graphics.dispose(); |
| 112 |
return newImage; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Produces a copy of the supplied image |
| 117 |
* |
| 118 |
* @param image The original image |
| 119 |
* @return The new BufferedImage |
| 120 |
*/ |
| 121 |
public static BufferedImage copyImage(BufferedImage image){ |
| 122 |
return scaledImage(image,image.getWidth(),image.getHeight()); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Renders a paragraph of text (line breaks ignored) to an image (created and returned). |
| 127 |
* |
| 128 |
* @param font The font to use |
| 129 |
* @param textColor The color of the text |
| 130 |
* @param text The message |
| 131 |
* @param width The width the text should be limited to |
| 132 |
* @return An image with the text rendered into it |
| 133 |
*/ |
| 134 |
public static BufferedImage renderTextToImage(Font font, Color textColor, String text, int width){ |
| 135 |
Hashtable map = new Hashtable(); |
| 136 |
map.put(TextAttribute.FONT, font); |
| 137 |
AttributedString attributedString = new AttributedString(text,map); |
| 138 |
AttributedCharacterIterator paragraph = attributedString.getIterator(); |
| 139 |
|
| 140 |
FontRenderContext frc = new FontRenderContext(null, false, false); |
| 141 |
int paragraphStart = paragraph.getBeginIndex(); |
| 142 |
int paragraphEnd = paragraph.getEndIndex(); |
| 143 |
LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc); |
| 144 |
|
| 145 |
float drawPosY=0; |
| 146 |
|
| 147 |
//First time around, just determine the height |
| 148 |
while (lineMeasurer.getPosition() < paragraphEnd) { |
| 149 |
TextLayout layout = lineMeasurer.nextLayout(width); |
| 150 |
|
| 151 |
// Move it down |
| 152 |
drawPosY += layout.getAscent() + layout.getDescent() + layout.getLeading(); |
| 153 |
} |
| 154 |
|
| 155 |
BufferedImage image = ImageUtilities.createCompatibleImage(width,(int) drawPosY); |
| 156 |
Graphics2D graphics = (Graphics2D) image.getGraphics(); |
| 157 |
graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
| 158 |
|
| 159 |
drawPosY=0; |
| 160 |
lineMeasurer.setPosition(paragraphStart); |
| 161 |
while (lineMeasurer.getPosition() < paragraphEnd) { |
| 162 |
TextLayout layout = lineMeasurer.nextLayout(width); |
| 163 |
|
| 164 |
// Move y-coordinate by the ascent of the layout. |
| 165 |
drawPosY += layout.getAscent(); |
| 166 |
|
| 167 |
/* Compute pen x position. If the paragraph is |
| 168 |
right-to-left, we want to align the TextLayouts |
| 169 |
to the right edge of the panel. |
| 170 |
*/ |
| 171 |
float drawPosX; |
| 172 |
if (layout.isLeftToRight()) { |
| 173 |
drawPosX = 0; |
| 174 |
} else { |
| 175 |
drawPosX = width - layout.getAdvance(); |
| 176 |
} |
| 177 |
|
| 178 |
// Draw the TextLayout at (drawPosX, drawPosY). |
| 179 |
layout.draw(graphics, drawPosX, drawPosY); |
| 180 |
|
| 181 |
// Move y-coordinate in preparation for next layout. |
| 182 |
drawPosY += layout.getDescent() + layout.getLeading(); |
| 183 |
} |
| 184 |
|
| 185 |
graphics.dispose(); |
| 186 |
return image; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
/** |
| 191 |
* Renders multiple paragraphs of text in an array to an image (created and returned). |
| 192 |
* |
| 193 |
* @param font The font to use |
| 194 |
* @param textColor The color of the text |
| 195 |
* @param text The message in an array of strings (one paragraph in each |
| 196 |
* @param width The width the text should be limited to |
| 197 |
* @return An image with the text rendered into it |
| 198 |
*/ |
| 199 |
public static BufferedImage renderTextToImage(Font font, Color textColor, String text[], int width){ |
| 200 |
LinkedList<BufferedImage> images = new LinkedList<BufferedImage>(); |
| 201 |
|
| 202 |
int totalHeight = 0; |
| 203 |
|
| 204 |
for (String paragraph : text){ |
| 205 |
BufferedImage paraImage = renderTextToImage(font,textColor,paragraph,width); |
| 206 |
totalHeight+=paraImage.getHeight(); |
| 207 |
images.add(paraImage); |
| 208 |
} |
| 209 |
|
| 210 |
BufferedImage image = createCompatibleImage(width,totalHeight); |
| 211 |
Graphics2D graphics = (Graphics2D) image.createGraphics(); |
| 212 |
|
| 213 |
int y=0; |
| 214 |
|
| 215 |
for (BufferedImage paraImage : images){ |
| 216 |
graphics.drawImage(paraImage,0,y,null); |
| 217 |
y+=paraImage.getHeight(); |
| 218 |
} |
| 219 |
|
| 220 |
graphics.dispose(); |
| 221 |
return image; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Renders a component into an image, which is useful for playing with the component's |
| 226 |
* resultant image in special effects or transitions |
| 227 |
* |
| 228 |
* @param component The component to render |
| 229 |
* @return A buffered image with the rendered component. |
| 230 |
*/ |
| 231 |
public static BufferedImage renderComponentToImage(JComponent component){ |
| 232 |
//Create the image |
| 233 |
BufferedImage image = createCompatibleImage(component.getWidth(),component.getHeight()); |
| 234 |
|
| 235 |
//Render the component onto the image |
| 236 |
Graphics graphics = image.createGraphics(); |
| 237 |
component.paint(graphics); |
| 238 |
graphics.dispose(); |
| 239 |
return image; |
| 240 |
} |
| 241 |
} |