View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // GraphicUtil.java
20  // Since: Nov 30, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.server.util.graphic;
26  
27  import java.awt.AlphaComposite;
28  import java.awt.Color;
29  import java.awt.Graphics2D;
30  import java.awt.geom.Rectangle2D;
31  import java.awt.image.BufferedImage;
32  import java.io.IOException;
33  
34  import javax.imageio.ImageIO;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.xerial.util.log.Logger;
38  
39  /**
40   * Utilities for manipulating Graphics in Java
41   * 
42   * @author leo
43   * 
44   */
45  public class GraphicUtil {
46  
47  	private static Logger _logger = Logger.getLogger(GraphicUtil.class);
48  
49  	/**
50  	 * Generates a BufferedImage instance whose background is transparent.
51  	 * 
52  	 * @param width
53  	 * @param height
54  	 * @return a BufferedImage
55  	 */
56  	public static BufferedImage getTransparentBufferedImage(int width, int height) {
57  		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
58  		Graphics2D g = image.createGraphics();
59  
60  		g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
61  		g.fill(new Rectangle2D.Double(0, 0, width, height));
62  		g.setComposite(AlphaComposite.SrcOver);
63  		return image;
64  	}
65  
66  	public static Graphics2D getGraphics(BufferedImage bufferedImage) {
67  		return (Graphics2D) bufferedImage.getGraphics();
68  	}
69  
70  	/**
71  	 * Output the given BufferedImage to the servlet response.
72  	 * 
73  	 * @param image
74  	 * @param imageType
75  	 * @param response
76  	 * @throws IOException
77  	 */
78  	public static void writeImage(BufferedImage image, String imageType, HttpServletResponse response) throws IOException {
79  		response.setContentType("image/" + imageType);
80  		ImageIO.write(image, imageType, response.getOutputStream());
81  	}
82  
83  	public static Color parseColor(String colorCodeStr) {
84  		if (colorCodeStr.startsWith("#"))
85  			colorCodeStr = colorCodeStr.substring(1);
86  
87  		int colorCode = 0x666666;
88  		try {
89  			colorCode = Integer.parseInt(colorCodeStr, 16);
90  		}
91  		catch (NumberFormatException e) {
92  			_logger.error("invalid color code:" + e);
93  		}
94  		return new Color((colorCode >> 16) & 0xFF, (colorCode >> 8) & 0xFF, (colorCode) & 0xFF);
95  	}
96  
97  	public static Color parseColor(String colorCodeStr, int alpha) {
98  		if (colorCodeStr.startsWith("#"))
99  			colorCodeStr = colorCodeStr.substring(1);
100 
101 		int colorCode = 0x666666;
102 		try {
103 			colorCode = Integer.parseInt(colorCodeStr, 16);
104 		}
105 		catch (NumberFormatException e) {
106 			_logger.error("invalid color code:" + e);
107 		}
108 		return new Color((colorCode >> 16) & 0xFF, (colorCode >> 8) & 0xFF, (colorCode) & 0xFF, alpha);
109 	}
110 
111 }