Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Fonts/AttributedStr.java
41175 views
1
/*
2
*
3
* Copyright (c) 2007, 2018, 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.Font.BOLD;
36
import static java.awt.Font.ITALIC;
37
import static java.awt.Font.PLAIN;
38
import static java.awt.font.TextAttribute.BACKGROUND;
39
import static java.awt.font.TextAttribute.CHAR_REPLACEMENT;
40
import static java.awt.font.TextAttribute.FONT;
41
import static java.awt.font.TextAttribute.FOREGROUND;
42
import static java.awt.font.TextAttribute.UNDERLINE;
43
import static java.awt.font.TextAttribute.UNDERLINE_ON;
44
import java.awt.Color;
45
import java.awt.Font;
46
import java.awt.GradientPaint;
47
import java.awt.Graphics2D;
48
import java.awt.Rectangle;
49
import java.awt.Shape;
50
import java.awt.TexturePaint;
51
import java.awt.font.FontRenderContext;
52
import java.awt.font.GraphicAttribute;
53
import java.awt.font.ImageGraphicAttribute;
54
import java.awt.font.LineBreakMeasurer;
55
import java.awt.font.ShapeGraphicAttribute;
56
import java.awt.font.TextLayout;
57
import java.awt.geom.AffineTransform;
58
import java.awt.geom.Ellipse2D;
59
import java.awt.image.BufferedImage;
60
import java.text.AttributedCharacterIterator;
61
import java.text.AttributedString;
62
import java2d.Surface;
63
64
65
/**
66
* Demonstrates how to build an AttributedString and then render the
67
* string broken over lines.
68
*/
69
@SuppressWarnings("serial")
70
public class AttributedStr extends Surface {
71
72
static final Color black = new Color(20, 20, 20);
73
static final Color blue = new Color(94, 105, 176);
74
static final Color yellow = new Color(255, 255, 140);
75
static final Color red = new Color(149, 43, 42);
76
static final Color white = new Color(240, 240, 255);
77
static final String text =
78
" A quick brown fox jumped over the lazy duke ";
79
static final AttributedString as = new AttributedString(text);
80
static AttributedCharacterIterator aci;
81
82
static {
83
Shape shape = new Ellipse2D.Double(0, 25, 12, 12);
84
ShapeGraphicAttribute sga = new ShapeGraphicAttribute(shape,
85
GraphicAttribute.TOP_ALIGNMENT, false);
86
as.addAttribute(CHAR_REPLACEMENT, sga, 0, 1);
87
88
89
Font font = new Font("sanserif", BOLD | ITALIC, 20);
90
int index = text.indexOf("quick");
91
as.addAttribute(FONT, font, index, index + 5);
92
93
index = text.indexOf("brown");
94
font = new Font(Font.SERIF, BOLD, 20);
95
as.addAttribute(FONT, font, index, index + 5);
96
as.addAttribute(FOREGROUND, red, index, index + 5);
97
98
index = text.indexOf("fox");
99
AffineTransform fontAT = new AffineTransform();
100
fontAT.rotate(Math.toRadians(10));
101
Font fx = new Font(Font.SERIF, BOLD, 30).deriveFont(fontAT);
102
as.addAttribute(FONT, fx, index, index + 1);
103
as.addAttribute(FONT, fx, index + 1, index + 2);
104
as.addAttribute(FONT, fx, index + 2, index + 3);
105
106
fontAT.setToRotation(Math.toRadians(-4));
107
fx = font.deriveFont(fontAT);
108
index = text.indexOf("jumped");
109
as.addAttribute(FONT, fx, index, index + 6);
110
111
font = new Font(Font.SERIF, BOLD | ITALIC, 30);
112
index = text.indexOf("over");
113
as.addAttribute(UNDERLINE, UNDERLINE_ON, index, index + 4);
114
as.addAttribute(FOREGROUND, white, index, index + 4);
115
as.addAttribute(FONT, font, index, text.length());
116
117
font = new Font(Font.DIALOG, PLAIN, 20);
118
int i = text.indexOf("duke");
119
as.addAttribute(FONT, font, index, i - 1);
120
121
BufferedImage bi = new BufferedImage(4, 4, BufferedImage.TYPE_INT_ARGB);
122
bi.setRGB(0, 0, 0xffffffff);
123
TexturePaint tp = new TexturePaint(bi, new Rectangle(0, 0, 4, 4));
124
as.addAttribute(BACKGROUND, tp, i, i + 4);
125
font = new Font(Font.SERIF, BOLD, 40);
126
as.addAttribute(FONT, font, i, i + 4);
127
}
128
129
public AttributedStr() {
130
setBackground(Color.white);
131
132
Font font = getFont("A.ttf");
133
if (font != null) {
134
font = font.deriveFont(PLAIN, 70);
135
} else {
136
font = new Font(Font.SERIF, PLAIN, 50);
137
}
138
int index = text.indexOf("A") + 1;
139
as.addAttribute(FONT, font, 0, index);
140
as.addAttribute(FOREGROUND, white, 0, index);
141
142
font = new Font(Font.DIALOG, PLAIN, 40);
143
int size = getFontMetrics(font).getHeight();
144
BufferedImage bi =
145
new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
146
Graphics2D big = bi.createGraphics();
147
big.drawImage(getImage("snooze.png"), 0, 0, size, size, null);
148
ImageGraphicAttribute iga =
149
new ImageGraphicAttribute(bi, GraphicAttribute.TOP_ALIGNMENT);
150
as.addAttribute(CHAR_REPLACEMENT, iga, text.length() - 1, text.length());
151
152
aci = as.getIterator();
153
}
154
155
@Override
156
public void render(int w, int h, Graphics2D g2) {
157
158
float x = 5, y = 0;
159
FontRenderContext frc = g2.getFontRenderContext();
160
LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
161
162
g2.setPaint(new GradientPaint(0, h, blue, w, 0, black));
163
g2.fillRect(0, 0, w, h);
164
165
g2.setColor(white);
166
String s = "AttributedString LineBreakMeasurer";
167
Font font = new Font(Font.SERIF, PLAIN, 12);
168
TextLayout tl = new TextLayout(s, font, frc);
169
170
tl.draw(g2, 5, y += (float) tl.getBounds().getHeight());
171
172
g2.setColor(yellow);
173
174
while (y < h - tl.getAscent()) {
175
lbm.setPosition(0);
176
while (lbm.getPosition() < text.length()) {
177
tl = lbm.nextLayout(w - x);
178
if (!tl.isLeftToRight()) {
179
x = w - tl.getAdvance();
180
}
181
tl.draw(g2, x, y += tl.getAscent());
182
y += tl.getDescent() + tl.getLeading();
183
}
184
}
185
}
186
187
public static void main(String[] s) {
188
createDemoFrame(new AttributedStr());
189
}
190
}
191
192