Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/demos/Composite/ACrules.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.Composite;
33
34
35
import static java.awt.AlphaComposite.Clear;
36
import static java.awt.AlphaComposite.Dst;
37
import static java.awt.AlphaComposite.DstAtop;
38
import static java.awt.AlphaComposite.DstIn;
39
import static java.awt.AlphaComposite.DstOut;
40
import static java.awt.AlphaComposite.DstOver;
41
import static java.awt.AlphaComposite.Src;
42
import static java.awt.AlphaComposite.SrcAtop;
43
import static java.awt.AlphaComposite.SrcIn;
44
import static java.awt.AlphaComposite.SrcOut;
45
import static java.awt.AlphaComposite.SrcOver;
46
import static java.awt.AlphaComposite.Xor;
47
import java.awt.AlphaComposite;
48
import java.awt.Color;
49
import java.awt.Font;
50
import java.awt.GradientPaint;
51
import java.awt.Graphics2D;
52
import java.awt.RenderingHints;
53
import java.awt.font.FontRenderContext;
54
import java.awt.font.LineMetrics;
55
import java.awt.font.TextLayout;
56
import java.awt.geom.GeneralPath;
57
import java.awt.image.BufferedImage;
58
import java2d.AnimatingSurface;
59
60
61
/**
62
* All the AlphaCompositing Rules demonstrated.
63
*/
64
@SuppressWarnings("serial")
65
public class ACrules extends AnimatingSurface {
66
67
private static String[] compNames = {
68
"Src",
69
"SrcOver",
70
"SrcIn",
71
"SrcOut",
72
"SrcAtop",
73
"Clear",
74
"Dst",
75
"DstOver",
76
"DstIn",
77
"DstOut",
78
"DstAtop",
79
"Xor", };
80
private static final AlphaComposite[] compObjs = {
81
Src, SrcOver, SrcIn, SrcOut, SrcAtop, Clear,
82
Dst, DstOver, DstIn, DstOut, DstAtop, Xor, };
83
private static final int NUM_RULES = compObjs.length;
84
private static final int HALF_NUM_RULES = NUM_RULES / 2;
85
private int fadeIndex;
86
private static float[][] fadeValues = {
87
{ 1.0f, -0.1f, 0.0f, 1.0f, 0.0f, 1.0f },
88
{ 0.0f, 0.1f, 1.0f, 1.0f, -0.1f, 0.0f },
89
{ 1.0f, 0.0f, 1.0f, 0.0f, 0.1f, 1.0f }, };
90
private static String[] fadeNames = {
91
"Src => transparent, Dest opaque",
92
"Src => opaque, Dest => transparent",
93
"Src opaque, Dest => opaque", };
94
private static Font f = new Font("serif", Font.PLAIN, 10);
95
private float srca = fadeValues[fadeIndex][0];
96
private float dsta = fadeValues[fadeIndex][3];
97
private String fadeLabel = fadeNames[0];
98
private BufferedImage statBI, animBI;
99
private int PADLEFT, PADRIGHT, HPAD;
100
private int PADABOVE, PADBELOW, VPAD;
101
private int RECTWIDTH, RECTHEIGHT;
102
private int PADDEDHEIGHT;
103
private GeneralPath srcpath = new GeneralPath();
104
private GeneralPath dstpath = new GeneralPath();
105
private LineMetrics lm;
106
private BufferedImage dBI, sBI;
107
private GradientPaint gradientDst, gradientSrc;
108
109
public ACrules() {
110
setBackground(Color.white);
111
}
112
113
@Override
114
public void reset(int w, int h) {
115
setSleepAmount(400);
116
FontRenderContext frc = new FontRenderContext(null, false, false);
117
lm = f.getLineMetrics(compNames[0], frc);
118
119
PADLEFT = (w < 150) ? 10 : 15;
120
PADRIGHT = (w < 150) ? 10 : 15;
121
HPAD = (PADLEFT + PADRIGHT);
122
PADBELOW = (h < 250) ? 1 : 2;
123
PADABOVE = PADBELOW + (int) lm.getHeight();
124
VPAD = (PADABOVE + PADBELOW);
125
RECTWIDTH = w / 4 - HPAD;
126
RECTWIDTH = (RECTWIDTH < 6) ? 6 : RECTWIDTH;
127
RECTHEIGHT = (h - VPAD) / HALF_NUM_RULES - VPAD;
128
RECTHEIGHT = (RECTHEIGHT < 6) ? 6 : RECTHEIGHT;
129
PADDEDHEIGHT = (RECTHEIGHT + VPAD);
130
131
srcpath.reset();
132
srcpath.moveTo(0, 0);
133
srcpath.lineTo(RECTWIDTH, 0);
134
srcpath.lineTo(0, RECTHEIGHT);
135
srcpath.closePath();
136
137
dstpath.reset();
138
dstpath.moveTo(0, 0);
139
dstpath.lineTo(RECTWIDTH, RECTHEIGHT);
140
dstpath.lineTo(RECTWIDTH, 0);
141
dstpath.closePath();
142
143
dBI = new BufferedImage(RECTWIDTH, RECTHEIGHT,
144
BufferedImage.TYPE_INT_ARGB);
145
sBI = new BufferedImage(RECTWIDTH, RECTHEIGHT,
146
BufferedImage.TYPE_INT_ARGB);
147
gradientDst = new GradientPaint(0, 0,
148
new Color(1.0f, 0.0f, 0.0f, 1.0f),
149
0, RECTHEIGHT,
150
new Color(1.0f, 0.0f, 0.0f, 0.0f));
151
gradientSrc = new GradientPaint(0, 0,
152
new Color(0.0f, 0.0f, 1.0f, 1.0f),
153
RECTWIDTH, 0,
154
new Color(0.0f, 0.0f, 1.0f, 0.0f));
155
statBI = new BufferedImage(w / 2, h, BufferedImage.TYPE_INT_RGB);
156
statBI = drawCompBI(statBI, true);
157
animBI = new BufferedImage(w / 2, h, BufferedImage.TYPE_INT_RGB);
158
}
159
160
@Override
161
public void step(int w, int h) {
162
if (getSleepAmount() == 5000) {
163
setSleepAmount(200);
164
}
165
166
srca = srca + fadeValues[fadeIndex][1];
167
dsta = dsta + fadeValues[fadeIndex][4];
168
fadeLabel = fadeNames[fadeIndex];
169
if (srca < 0 || srca > 1.0 || dsta < 0 || dsta > 1.0) {
170
setSleepAmount(5000);
171
srca = fadeValues[fadeIndex][2];
172
dsta = fadeValues[fadeIndex][5];
173
if (fadeIndex++ == fadeValues.length - 1) {
174
fadeIndex = 0;
175
}
176
}
177
}
178
179
@Override
180
public void render(int w, int h, Graphics2D g2) {
181
182
if (statBI == null || animBI == null) {
183
return;
184
}
185
g2.drawImage(statBI, 0, 0, null);
186
g2.drawImage(drawCompBI(animBI, false), w / 2, 0, null);
187
188
g2.setColor(Color.black);
189
FontRenderContext frc = g2.getFontRenderContext();
190
TextLayout tl = new TextLayout("AC Rules", g2.getFont(), frc);
191
tl.draw(g2, 15.0f, (float) tl.getBounds().getHeight() + 3.0f);
192
193
tl = new TextLayout(fadeLabel, f, frc);
194
float x = (float) (w * 0.75 - tl.getBounds().getWidth() / 2);
195
if ((x + tl.getBounds().getWidth()) > w) {
196
x = (float) (w - tl.getBounds().getWidth());
197
}
198
tl.draw(g2, x, (float) tl.getBounds().getHeight() + 3.0f);
199
}
200
201
private BufferedImage drawCompBI(BufferedImage bi, boolean doGradient) {
202
Graphics2D big = bi.createGraphics();
203
big.setColor(getBackground());
204
big.fillRect(0, 0, bi.getWidth(), bi.getHeight());
205
big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
206
big.setFont(f);
207
208
Graphics2D gD = dBI.createGraphics();
209
gD.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
210
Graphics2D gS = sBI.createGraphics();
211
gS.setRenderingHint(RenderingHints.KEY_ANTIALIASING, AntiAlias);
212
213
int x = 0, y = 0;
214
int yy = (int) lm.getHeight() + VPAD;
215
216
for (int i = 0; i < compNames.length; i++) {
217
y = (i == 0 || i == HALF_NUM_RULES) ? yy : y + PADDEDHEIGHT;
218
x = (i >= HALF_NUM_RULES) ? bi.getWidth() / 2 + PADLEFT : PADLEFT;
219
big.translate(x, y);
220
221
gD.setComposite(Clear);
222
gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
223
gD.setComposite(Src);
224
if (doGradient) {
225
gD.setPaint(gradientDst);
226
gD.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
227
} else {
228
gD.setPaint(new Color(1.0f, 0.0f, 0.0f, dsta));
229
gD.fill(dstpath);
230
}
231
232
gS.setComposite(Clear);
233
gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
234
gS.setComposite(Src);
235
if (doGradient) {
236
gS.setPaint(gradientSrc);
237
gS.fillRect(0, 0, RECTWIDTH, RECTHEIGHT);
238
} else {
239
gS.setPaint(new Color(0.0f, 0.0f, 1.0f, srca));
240
gS.fill(srcpath);
241
}
242
243
gD.setComposite(compObjs[i]);
244
gD.drawImage(sBI, 0, 0, null);
245
246
big.drawImage(dBI, 0, 0, null);
247
big.setColor(Color.black);
248
big.drawString(compNames[i], 0, -lm.getDescent());
249
big.drawRect(0, 0, RECTWIDTH, RECTHEIGHT);
250
big.translate(-x, -y);
251
}
252
253
gD.dispose();
254
gS.dispose();
255
big.dispose();
256
257
return bi;
258
}
259
260
public static void main(String[] argv) {
261
createDemoFrame(new ACrules());
262
}
263
}
264
265