2648
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
|
2648
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
21596
|
7 |
* published by the Free Software Foundation.
|
2648
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2648
|
22 |
*/
|
|
23 |
|
|
24 |
import java.awt.BorderLayout;
|
|
25 |
import java.awt.Color;
|
|
26 |
import java.awt.Dimension;
|
|
27 |
import java.awt.Frame;
|
|
28 |
import java.awt.Graphics;
|
|
29 |
import java.awt.GraphicsConfiguration;
|
|
30 |
import java.awt.GraphicsDevice;
|
|
31 |
import java.awt.GraphicsDevice.WindowTranslucency;
|
|
32 |
import java.awt.GraphicsEnvironment;
|
|
33 |
import java.awt.RenderingHints;
|
|
34 |
import java.awt.event.MouseAdapter;
|
|
35 |
import java.awt.event.MouseEvent;
|
|
36 |
import java.awt.event.WindowAdapter;
|
|
37 |
import java.awt.event.WindowEvent;
|
|
38 |
import java.awt.Canvas;
|
|
39 |
import java.awt.Component;
|
|
40 |
import java.awt.GradientPaint;
|
|
41 |
import java.awt.Graphics2D;
|
|
42 |
import java.awt.Paint;
|
|
43 |
import java.util.Random;
|
|
44 |
import java.awt.geom.Ellipse2D;
|
|
45 |
import javax.swing.JApplet;
|
|
46 |
import javax.swing.JButton;
|
|
47 |
import javax.swing.JComponent;
|
|
48 |
import javax.swing.JFrame;
|
|
49 |
import javax.swing.JPanel;
|
|
50 |
import javax.swing.SwingUtilities;
|
|
51 |
|
|
52 |
public class TSFrame {
|
|
53 |
|
|
54 |
static volatile boolean done = false;
|
|
55 |
|
|
56 |
static final boolean useSwing = System.getProperty("useswing") != null;
|
|
57 |
static final boolean useShape = System.getProperty("useshape") != null;
|
|
58 |
static final boolean useTransl = System.getProperty("usetransl") != null;
|
|
59 |
static final boolean useNonOpaque = System.getProperty("usenonop") != null;
|
|
60 |
|
|
61 |
static final Random rnd = new Random();
|
|
62 |
private static void render(Graphics g, int w, int h, boolean useNonOpaque) {
|
|
63 |
if (useNonOpaque) {
|
|
64 |
Graphics2D g2d = (Graphics2D)g;
|
|
65 |
GradientPaint p =
|
|
66 |
new GradientPaint(0.0f, 0.0f,
|
|
67 |
new Color(rnd.nextInt(0xffffff)),
|
|
68 |
w, h,
|
|
69 |
new Color(rnd.nextInt(0xff),
|
|
70 |
rnd.nextInt(0xff),
|
|
71 |
rnd.nextInt(0xff), 0),
|
|
72 |
true);
|
|
73 |
g2d.setPaint(p);
|
|
74 |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
|
75 |
RenderingHints.VALUE_ANTIALIAS_ON);
|
|
76 |
g2d.fillOval(0, 0, w, h);
|
|
77 |
} else {
|
|
78 |
g.setColor(new Color(rnd.nextInt(0xffffff)));
|
|
79 |
g.fillRect(0, 0, w, h);
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
private static class MyCanvas extends Canvas {
|
|
84 |
@Override
|
|
85 |
public void paint(Graphics g) {
|
|
86 |
render(g, getWidth(), getHeight(), false);
|
|
87 |
}
|
|
88 |
@Override
|
|
89 |
public Dimension getPreferredSize() {
|
|
90 |
return new Dimension(200, 100);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
private static class NonOpaqueJFrame extends JFrame {
|
|
94 |
NonOpaqueJFrame() {
|
|
95 |
super("NonOpaque Swing JFrame");
|
|
96 |
JPanel p = new JPanel() {
|
|
97 |
public void paintComponent(Graphics g) {
|
|
98 |
super.paintComponent(g);
|
|
99 |
render(g, getWidth(), getHeight(), true);
|
|
100 |
g.setColor(Color.red);
|
|
101 |
g.drawString("Non-Opaque Swing JFrame", 10, 15);
|
|
102 |
}
|
|
103 |
};
|
|
104 |
p.setDoubleBuffered(false);
|
|
105 |
p.setOpaque(false);
|
|
106 |
add(p);
|
|
107 |
setUndecorated(true);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
private static class NonOpaqueJAppletFrame extends JFrame {
|
|
111 |
JPanel p;
|
|
112 |
NonOpaqueJAppletFrame() {
|
|
113 |
super("NonOpaque Swing JAppletFrame");
|
|
114 |
JApplet ja = new JApplet() {
|
|
115 |
public void paint(Graphics g) {
|
|
116 |
super.paint(g);
|
|
117 |
System.err.println("JAppletFrame paint called");
|
|
118 |
}
|
|
119 |
};
|
|
120 |
p = new JPanel() {
|
|
121 |
public void paintComponent(Graphics g) {
|
|
122 |
super.paintComponent(g);
|
|
123 |
render(g, getWidth(), getHeight(), true);
|
|
124 |
g.setColor(Color.red);
|
|
125 |
g.drawString("Non-Opaque Swing JFrame", 10, 15);
|
|
126 |
}
|
|
127 |
};
|
|
128 |
p.setDoubleBuffered(false);
|
|
129 |
p.setOpaque(false);
|
|
130 |
ja.add(p);
|
|
131 |
add(ja);
|
|
132 |
setUndecorated(true);
|
|
133 |
}
|
|
134 |
}
|
|
135 |
private static class NonOpaqueFrame extends Frame {
|
|
136 |
NonOpaqueFrame() {
|
|
137 |
super("NonOpaque AWT Frame");
|
|
138 |
// uncomment to test with hw child
|
|
139 |
// setLayout(null);
|
|
140 |
// Component c = new Panel() {
|
|
141 |
// public void paint(Graphics g) {
|
|
142 |
// g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f));
|
|
143 |
// g.fillRect(0, 0, getWidth(), getHeight());
|
|
144 |
// }
|
|
145 |
// };
|
|
146 |
// c.setSize(100, 100);
|
|
147 |
// c.setBackground(Color.red);
|
|
148 |
// c.setForeground(Color.red);
|
|
149 |
// add(c);
|
|
150 |
// c.setLocation(130, 130);
|
|
151 |
}
|
|
152 |
@Override
|
|
153 |
public void paint(Graphics g) {
|
|
154 |
render(g, getWidth(), getHeight(), true);
|
|
155 |
g.setColor(Color.red);
|
|
156 |
g.drawString("Non-Opaque AWT Frame", 10, 15);
|
|
157 |
}
|
|
158 |
}
|
|
159 |
|
|
160 |
private static class MyJPanel extends JPanel {
|
|
161 |
@Override
|
|
162 |
public void paintComponent(Graphics g) {
|
|
163 |
render(g, getWidth(), getHeight(), false);
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
public static Frame createGui(
|
|
168 |
final boolean useSwing,
|
|
169 |
final boolean useShape,
|
|
170 |
final boolean useTransl,
|
|
171 |
final boolean useNonOpaque,
|
|
172 |
final float factor)
|
|
173 |
{
|
|
174 |
Frame frame;
|
|
175 |
done = false;
|
|
176 |
|
|
177 |
if (useNonOpaque) {
|
|
178 |
if (useSwing) {
|
|
179 |
frame = new NonOpaqueJFrame();
|
|
180 |
// frame = new NonOpaqueJAppletFrame(gc);
|
|
181 |
} else {
|
|
182 |
frame = new NonOpaqueFrame();
|
|
183 |
}
|
|
184 |
animateComponent(frame);
|
|
185 |
} else if (useSwing) {
|
|
186 |
frame = new JFrame("Swing Frame");
|
|
187 |
JComponent p = new JButton("Swing!");
|
|
188 |
p.setPreferredSize(new Dimension(200, 100));
|
|
189 |
frame.add("North", p);
|
|
190 |
p = new MyJPanel();
|
|
191 |
animateComponent(p);
|
|
192 |
frame.add("Center", p);
|
|
193 |
} else {
|
|
194 |
frame = new Frame("AWT Frame") {
|
|
195 |
public void paint(Graphics g) {
|
|
196 |
g.setColor(Color.red);
|
|
197 |
g.fillRect(0, 0, 100, 100);
|
|
198 |
}
|
|
199 |
};
|
|
200 |
frame.setLayout(new BorderLayout());
|
|
201 |
Canvas c = new MyCanvas();
|
|
202 |
frame.add("North", c);
|
|
203 |
animateComponent(c);
|
|
204 |
c = new MyCanvas();
|
|
205 |
frame.add("Center", c);
|
|
206 |
animateComponent(c);
|
|
207 |
c = new MyCanvas();
|
|
208 |
frame.add("South", c);
|
|
209 |
animateComponent(c);
|
|
210 |
}
|
|
211 |
final Frame finalFrame = frame;
|
|
212 |
frame.addWindowListener(new WindowAdapter() {
|
|
213 |
@Override
|
|
214 |
public void windowClosing(WindowEvent e) {
|
|
215 |
finalFrame.dispose();
|
|
216 |
done = true;
|
|
217 |
}
|
|
218 |
});
|
|
219 |
frame.addMouseListener(new MouseAdapter() {
|
|
220 |
@Override
|
|
221 |
public void mouseClicked(MouseEvent e) {
|
|
222 |
finalFrame.dispose();
|
|
223 |
done = true;
|
|
224 |
}
|
|
225 |
});
|
|
226 |
frame.setPreferredSize(new Dimension(800, 600));
|
|
227 |
|
|
228 |
if (useShape) {
|
|
229 |
frame.setUndecorated(true);
|
|
230 |
}
|
|
231 |
|
|
232 |
frame.setLocation(450, 10);
|
|
233 |
frame.pack();
|
|
234 |
|
|
235 |
GraphicsDevice gd = frame.getGraphicsConfiguration().getDevice();
|
|
236 |
if (useShape) {
|
|
237 |
if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
|
|
238 |
System.out.println("applying PERPIXEL_TRANSPARENT");
|
|
239 |
frame.setShape(new Ellipse2D.Double(0, 0, frame.getWidth(),
|
|
240 |
frame.getHeight()/3));
|
|
241 |
frame.setTitle("PERPIXEL_TRANSPARENT");
|
|
242 |
} else {
|
|
243 |
System.out.println("Passed: PERPIXEL_TRANSPARENT unsupported");
|
|
244 |
}
|
|
245 |
}
|
|
246 |
if (useTransl) {
|
|
247 |
if (gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) {
|
|
248 |
System.out.println("applying TRANSLUCENT");
|
|
249 |
frame.setOpacity(factor);
|
|
250 |
frame.setTitle("TRANSLUCENT");
|
|
251 |
} else {
|
|
252 |
System.out.println("Passed: TRANSLUCENT unsupported");
|
|
253 |
}
|
|
254 |
}
|
|
255 |
if (useNonOpaque) {
|
|
256 |
if (gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
|
|
257 |
System.out.println("applying PERPIXEL_TRANSLUCENT");
|
|
258 |
frame.setBackground(new Color(0, 0, 0, 0));
|
|
259 |
frame.setTitle("PERPIXEL_TRANSLUCENT");
|
|
260 |
} else {
|
|
261 |
System.out.println("Passed: PERPIXEL_TRANSLUCENT unsupported");
|
|
262 |
}
|
|
263 |
}
|
|
264 |
frame.setVisible(true);
|
|
265 |
return frame;
|
|
266 |
}
|
|
267 |
|
|
268 |
public static void stopThreads() {
|
|
269 |
done = true;
|
|
270 |
}
|
|
271 |
|
|
272 |
private static void animateComponent(final Component comp) {
|
|
273 |
Thread t = new Thread(new Runnable() {
|
|
274 |
public void run() {
|
|
275 |
do {
|
|
276 |
try {
|
|
277 |
Thread.sleep(50);
|
|
278 |
} catch (InterruptedException ex) {}
|
|
279 |
comp.repaint();
|
|
280 |
} while (!done);
|
|
281 |
}
|
|
282 |
});
|
|
283 |
t.start();
|
|
284 |
}
|
|
285 |
|
|
286 |
public static void main(String[] args) throws Exception {
|
|
287 |
SwingUtilities.invokeLater(new Runnable() {
|
|
288 |
public void run() {
|
|
289 |
TSFrame.createGui(useSwing,
|
|
290 |
useShape,
|
|
291 |
useTransl,
|
|
292 |
useNonOpaque,
|
|
293 |
0.7f);
|
|
294 |
}
|
|
295 |
});
|
|
296 |
}
|
|
297 |
}
|