|
1 /* |
|
2 * |
|
3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * |
|
9 * - Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * - Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * - Neither the name of Oracle nor the names of its |
|
17 * contributors may be used to endorse or promote products derived |
|
18 * from this software without specific prior written permission. |
|
19 * |
|
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
31 */ |
|
32 package java2d.demos.Fonts; |
|
33 |
|
34 |
|
35 import static java.awt.Color.BLACK; |
|
36 import static java.awt.Color.BLUE; |
|
37 import static java.awt.Color.GREEN; |
|
38 import static java.awt.Color.MAGENTA; |
|
39 import static java.awt.Color.RED; |
|
40 import static java.awt.Color.WHITE; |
|
41 import java.awt.BasicStroke; |
|
42 import java.awt.Font; |
|
43 import java.awt.Graphics2D; |
|
44 import java.awt.Shape; |
|
45 import java.awt.font.FontRenderContext; |
|
46 import java.awt.font.TextAttribute; |
|
47 import java.awt.font.TextLayout; |
|
48 import java.awt.geom.AffineTransform; |
|
49 import java.text.AttributedCharacterIterator; |
|
50 import java.text.AttributedString; |
|
51 import java2d.Surface; |
|
52 |
|
53 |
|
54 /** |
|
55 * Rendering text as an outline shape. |
|
56 */ |
|
57 @SuppressWarnings("serial") |
|
58 public class Outline extends Surface { |
|
59 |
|
60 public Outline() { |
|
61 setBackground(WHITE); |
|
62 } |
|
63 |
|
64 @Override |
|
65 public void render(int w, int h, Graphics2D g2) { |
|
66 |
|
67 FontRenderContext frc = g2.getFontRenderContext(); |
|
68 Font f = new Font(Font.SANS_SERIF, Font.PLAIN, w / 8); |
|
69 Font f1 = new Font(Font.SANS_SERIF, Font.ITALIC, w / 8); |
|
70 String s = "AttributedString"; |
|
71 AttributedString as = new AttributedString(s); |
|
72 as.addAttribute(TextAttribute.FONT, f, 0, 10); |
|
73 as.addAttribute(TextAttribute.FONT, f1, 10, s.length()); |
|
74 AttributedCharacterIterator aci = as.getIterator(); |
|
75 TextLayout tl = new TextLayout(aci, frc); |
|
76 float sw = (float) tl.getBounds().getWidth(); |
|
77 float sh = (float) tl.getBounds().getHeight(); |
|
78 Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw |
|
79 / 2, h * 0.2 + sh / 2)); |
|
80 g2.setColor(BLUE); |
|
81 g2.setStroke(new BasicStroke(1.5f)); |
|
82 g2.draw(sha); |
|
83 g2.setColor(MAGENTA); |
|
84 g2.fill(sha); |
|
85 |
|
86 f = new Font(Font.SERIF, Font.BOLD, w / 6); |
|
87 tl = new TextLayout("Outline", f, frc); |
|
88 sw = (float) tl.getBounds().getWidth(); |
|
89 sh = (float) tl.getBounds().getHeight(); |
|
90 sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw / 2, h |
|
91 * 0.5 + sh / 2)); |
|
92 g2.setColor(BLACK); |
|
93 g2.draw(sha); |
|
94 g2.setColor(RED); |
|
95 g2.fill(sha); |
|
96 |
|
97 f = new Font(Font.SANS_SERIF, Font.ITALIC, w / 8); |
|
98 AffineTransform fontAT = new AffineTransform(); |
|
99 fontAT.shear(-0.2, 0.0); |
|
100 Font derivedFont = f.deriveFont(fontAT); |
|
101 tl = new TextLayout("Italic-Shear", derivedFont, frc); |
|
102 sw = (float) tl.getBounds().getWidth(); |
|
103 sh = (float) tl.getBounds().getHeight(); |
|
104 sha = tl.getOutline(AffineTransform.getTranslateInstance(w / 2 - sw / 2, h |
|
105 * 0.80f + sh / 2)); |
|
106 g2.setColor(GREEN); |
|
107 g2.draw(sha); |
|
108 g2.setColor(BLACK); |
|
109 g2.fill(sha); |
|
110 } |
|
111 |
|
112 public static void main(String s[]) { |
|
113 createDemoFrame(new Outline()); |
|
114 } |
|
115 } |