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  // TextLabel.java
20  // Since: Nov 30, 2007
21  //
22  // $URL$ 
23  // $Author$
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   * Generates a TextLabel graphic
42   * 
43   * @author leo
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  		// g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
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 }