Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/demo/share/jfc/J2Ddemo/java2d/TextureChooser.java
41154 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;
33
34
35
import static java.awt.Color.GRAY;
36
import static java.awt.Color.GREEN;
37
import static java.awt.Color.LIGHT_GRAY;
38
import static java.awt.Color.WHITE;
39
import java.awt.BasicStroke;
40
import java.awt.Color;
41
import java.awt.Component;
42
import java.awt.Dimension;
43
import java.awt.Font;
44
import java.awt.Frame;
45
import java.awt.GradientPaint;
46
import java.awt.Graphics;
47
import java.awt.Graphics2D;
48
import java.awt.GridLayout;
49
import java.awt.Image;
50
import java.awt.Rectangle;
51
import java.awt.TexturePaint;
52
import java.awt.event.MouseAdapter;
53
import java.awt.event.MouseEvent;
54
import java.awt.event.WindowAdapter;
55
import java.awt.event.WindowEvent;
56
import java.awt.font.FontRenderContext;
57
import java.awt.font.TextLayout;
58
import java.awt.geom.Ellipse2D;
59
import java.awt.image.BufferedImage;
60
import javax.swing.JPanel;
61
import javax.swing.border.EtchedBorder;
62
import javax.swing.border.TitledBorder;
63
64
65
/**
66
* Four types of Paint displayed: Geometry, Text & Image Textures and
67
* a Gradient Paint. Paints can be selected with the Mouse.
68
*/
69
@SuppressWarnings("serial")
70
public final class TextureChooser extends JPanel {
71
private final DemoInstVarsAccessor demoInstVars;
72
public Object texture = getGeomTexture();
73
public int num;
74
75
public TextureChooser(int num, DemoInstVarsAccessor demoInstVars) {
76
this.num = num;
77
this.demoInstVars = demoInstVars;
78
79
setLayout(new GridLayout(0, 2, 5, 5));
80
setBorder(new TitledBorder(new EtchedBorder(), "Texture Chooser"));
81
82
add(new Surface(getGeomTexture(), this, 0));
83
add(new Surface(getImageTexture(), this, 1));
84
add(new Surface(getTextTexture(), this, 2));
85
add(new Surface(getGradientPaint(), this, 3));
86
}
87
88
public static TexturePaint getGeomTexture() {
89
BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);
90
Graphics2D tG2 = bi.createGraphics();
91
tG2.setBackground(WHITE);
92
tG2.clearRect(0, 0, 5, 5);
93
tG2.setColor(new Color(211, 211, 211, 200));
94
tG2.fill(new Ellipse2D.Float(0, 0, 5, 5));
95
Rectangle r = new Rectangle(0, 0, 5, 5);
96
return new TexturePaint(bi, r);
97
}
98
99
public TexturePaint getImageTexture() {
100
Image img = DemoImages.getImage("globe.png", this);
101
int sw = img.getWidth(this);
102
int sh = img.getHeight(this);
103
int iw = sw/5;
104
int ih = sh/5;
105
BufferedImage bi =
106
new BufferedImage(iw, ih, BufferedImage.TYPE_INT_ARGB);
107
Graphics2D tG2 = bi.createGraphics();
108
tG2.drawImage(img, 0, 0, iw, ih, 0, 0, sw, sh, this);
109
Rectangle r = new Rectangle(0, 0, iw, ih);
110
return new TexturePaint(bi, r);
111
}
112
113
public TexturePaint getTextTexture() {
114
Font f = new Font(Font.SERIF, Font.BOLD, 10);
115
TextLayout tl = new TextLayout("OpenJDK", f, new FontRenderContext(null,
116
false, false));
117
int sw = (int) tl.getBounds().getWidth();
118
int sh = (int) (tl.getAscent() + tl.getDescent());
119
BufferedImage bi = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
120
Graphics2D tG2 = bi.createGraphics();
121
tG2.setBackground(WHITE);
122
tG2.clearRect(0, 0, sw, sh);
123
tG2.setColor(LIGHT_GRAY);
124
tl.draw(tG2, 0, tl.getAscent());
125
Rectangle r = new Rectangle(0, 0, sw, sh);
126
return new TexturePaint(bi, r);
127
}
128
129
public GradientPaint getGradientPaint() {
130
return new GradientPaint(0, 0, WHITE, 80, 0, GREEN);
131
}
132
133
134
public class Surface extends JPanel {
135
136
public boolean clickedFrame;
137
private int num;
138
private TextureChooser tc;
139
private boolean enterExitFrame = false;
140
private Object t;
141
142
public Surface(final Object t, final TextureChooser tc, int num) {
143
setBackground(WHITE);
144
this.t = t;
145
this.tc = tc;
146
this.clickedFrame = (num == tc.num);
147
this.num = num;
148
if (num == tc.num) {
149
tc.texture = t;
150
}
151
addMouseListener(new MouseAdapter() {
152
153
@Override
154
public void mouseClicked(MouseEvent e) {
155
tc.texture = t;
156
clickedFrame = true;
157
158
for (Component comp : tc.getComponents()) {
159
if (comp instanceof Surface) {
160
Surface surf = (Surface) comp;
161
if (!surf.equals(Surface.this) && surf.clickedFrame) {
162
surf.clickedFrame = false;
163
surf.repaint();
164
}
165
}
166
}
167
168
// ABP
169
if (demoInstVars.getControls().textureCB.isSelected()) {
170
demoInstVars.getControls().textureCB.doClick();
171
demoInstVars.getControls().textureCB.doClick();
172
}
173
}
174
175
@Override
176
public void mouseEntered(MouseEvent e) {
177
enterExitFrame = true;
178
repaint();
179
}
180
181
@Override
182
public void mouseExited(MouseEvent e) {
183
enterExitFrame = false;
184
repaint();
185
}
186
});
187
}
188
189
@Override
190
public void paintComponent(Graphics g) {
191
super.paintComponent(g);
192
Graphics2D g2 = (Graphics2D) g;
193
int w = getSize().width;
194
int h = getSize().height;
195
if (t instanceof TexturePaint) {
196
g2.setPaint((TexturePaint) t);
197
} else {
198
g2.setPaint((GradientPaint) t);
199
}
200
g2.fill(new Rectangle(0, 0, w, h));
201
if (clickedFrame || enterExitFrame) {
202
g2.setColor(GRAY);
203
BasicStroke bs = new BasicStroke(3, BasicStroke.CAP_BUTT,
204
BasicStroke.JOIN_MITER);
205
g2.setStroke(bs);
206
g2.drawRect(0, 0, w - 1, h - 1);
207
tc.num = num;
208
}
209
}
210
211
@Override
212
public Dimension getMinimumSize() {
213
return getPreferredSize();
214
}
215
216
@Override
217
public Dimension getMaximumSize() {
218
return getPreferredSize();
219
}
220
221
@Override
222
public Dimension getPreferredSize() {
223
return new Dimension(30, 30);
224
}
225
}
226
227
public static void main(String[] s) {
228
Frame f = new Frame("J2D Demo - TextureChooser");
229
f.addWindowListener(new WindowAdapter() {
230
231
@Override
232
public void windowClosing(WindowEvent e) {
233
System.exit(0);
234
}
235
});
236
f.add("Center", new TextureChooser(0, new DemoInstVarsAccessorImplBase()));
237
f.pack();
238
f.setSize(new Dimension(400, 400));
239
f.setVisible(true);
240
}
241
}
242
243