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