1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
41
42
43
44
45 public class GraphicUtil {
46
47 private static Logger _logger = Logger.getLogger(GraphicUtil.class);
48
49
50
51
52
53
54
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
72
73
74
75
76
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 }