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.app;
26
27 import java.awt.Font;
28 import java.awt.FontMetrics;
29 import java.awt.Graphics2D;
30 import java.awt.image.BufferedImage;
31 import java.io.IOException;
32
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 import org.utgenome.gwt.utgb.server.RequestHandlerBase;
38 import org.utgenome.gwt.utgb.server.util.graphic.GraphicUtil;
39
40
41
42
43
44
45
46 public class TextLabel extends RequestHandlerBase {
47 private static final long serialVersionUID = 1L;
48
49 private String text = "";
50 private int width = 300;
51 private int height = 200;
52 private int size = 12;
53 private String color = "000000";
54
55 @Override
56 public void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57
58 if (size <= 0)
59 size = 1;
60 if (width <= 0)
61 width = 50;
62 if (height <= 0)
63 height = 12;
64
65 BufferedImage image = GraphicUtil.getTransparentBufferedImage(width, height);
66 Graphics2D g = GraphicUtil.getGraphics(image);
67
68
69 g.setFont(new Font("SansSerif", Font.PLAIN, size));
70 FontMetrics fontMetrics = g.getFontMetrics();
71 int fontHeight = fontMetrics.getHeight();
72 int fontWidth = fontMetrics.stringWidth(text);
73 g.setColor(GraphicUtil.parseColor(color));
74 g.drawString(text, 0, fontHeight);
75
76 BufferedImage clippedText = image.getSubimage(0, 0, fontWidth, fontHeight);
77 GraphicUtil.writeImage(clippedText, "png", response);
78 }
79
80 public String getText() {
81 return text;
82 }
83
84 public void setText(String text) {
85 this.text = text;
86 }
87
88 public int getWidth() {
89 return width;
90 }
91
92 public void setWidth(int width) {
93 this.width = width;
94 }
95
96 public int getHeight() {
97 return height;
98 }
99
100 public void setHeight(int height) {
101 this.height = height;
102 }
103
104 public int getSize() {
105 return size;
106 }
107
108 public void setSize(int size) {
109 this.size = size;
110 }
111
112 }