Debian Codegraph @

netx/net/sourceforge/jnlp/splashscreen/impls/defaultsplashscreen2012/TextOutlineRenderer.java

raw
1 /* TextOutlineRenderer.java
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 IcedTea is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with IcedTea; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37 package net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012;
38
39 import java.awt.Color;
40 import java.awt.Font;
41 import java.awt.Graphics2D;
42 import java.awt.Image;
43 import java.awt.Rectangle;
44 import java.awt.RenderingHints;
45 import java.awt.Shape;
46 import java.awt.font.FontRenderContext;
47 import java.awt.font.TextLayout;
48 import java.awt.geom.AffineTransform;
49
50 public class TextOutlineRenderer {
51
52 private Image img;
53 private Font font;
54 private Color outlineColor;
55 private final String text;
56
57 public TextOutlineRenderer(Font f, String s) {
58 this.font = f;
59 outlineColor = Color.black;
60 this.text = s;
61
62 }
63
64 public TextOutlineRenderer(Font f, String s, Color textOutline) {
65 this(f, s);
66 this.outlineColor = textOutline;
67 }
68
69 public int getWidth() {
70 if (img == null) {
71 return -1;
72 }
73 return img.getWidth(null);
74 }
75
76 public int getHeight() {
77 if (img == null) {
78 return -1;
79 }
80 return img.getHeight(null);
81 }
82
83 public Rectangle cutTo(Graphics2D g2, int x, int y) {
84 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
85 RenderingHints.VALUE_ANTIALIAS_ON);
86
87 g2.setRenderingHint(RenderingHints.KEY_RENDERING,
88 RenderingHints.VALUE_RENDER_QUALITY);
89
90 FontRenderContext frc = g2.getFontRenderContext();
91 TextLayout tl = new TextLayout(getText(), getFont(), frc);
92 float sw = (float) tl.getBounds().getWidth();
93 AffineTransform transform = new AffineTransform();
94 transform.setToTranslation(x, y);
95 Shape shape = tl.getOutline(transform);
96 Rectangle r = shape.getBounds();
97 g2.setColor(getTextOutline());
98 g2.draw(shape);
99 g2.setClip(shape);
100 g2.drawImage(getImg(), r.x, r.y, r.width, r.height, null);
101 return r;
102
103 }
104
105 /**
106 * @return the img
107 */
108 public Image getImg() {
109 return img;
110 }
111
112 /**
113 * @param img the img to set
114 */
115 public void setImg(Image img) {
116 this.img = img;
117 }
118
119 /**
120 * @return the font
121 */
122 public Font getFont() {
123 return font;
124 }
125
126 /**
127 * @param font the font to set
128 */
129 public void setFont(Font font) {
130 this.font = font;
131 }
132
133 /**
134 * @return the color of outline
135 */
136 public Color getTextOutline() {
137 return outlineColor;
138 }
139
140 /**
141 * @param textOutline the color of outline
142 */
143 public void setTextOutline(Color textOutline) {
144 this.outlineColor = textOutline;
145 }
146
147 /**
148 * @return the text
149 */
150 public String getText() {
151 return text;
152 }
153 }