2
|
1 |
/*
|
887
|
2 |
* Copyright 1997-2008 Sun Microsystems Microsystems, Inc. All Rights Reserved.
|
2
|
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 |
|
|
27 |
package java.awt;
|
|
28 |
|
|
29 |
import java.awt.image.ColorModel;
|
|
30 |
import sun.awt.AppContext;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* The <code>GraphicsDevice</code> class describes the graphics devices
|
|
34 |
* that might be available in a particular graphics environment. These
|
|
35 |
* include screen and printer devices. Note that there can be many screens
|
|
36 |
* and many printers in an instance of {@link GraphicsEnvironment}. Each
|
|
37 |
* graphics device has one or more {@link GraphicsConfiguration} objects
|
|
38 |
* associated with it. These objects specify the different configurations
|
|
39 |
* in which the <code>GraphicsDevice</code> can be used.
|
|
40 |
* <p>
|
|
41 |
* In a multi-screen environment, the <code>GraphicsConfiguration</code>
|
|
42 |
* objects can be used to render components on multiple screens. The
|
|
43 |
* following code sample demonstrates how to create a <code>JFrame</code>
|
|
44 |
* object for each <code>GraphicsConfiguration</code> on each screen
|
|
45 |
* device in the <code>GraphicsEnvironment</code>:
|
|
46 |
* <pre>
|
|
47 |
* GraphicsEnvironment ge = GraphicsEnvironment.
|
|
48 |
* getLocalGraphicsEnvironment();
|
|
49 |
* GraphicsDevice[] gs = ge.getScreenDevices();
|
|
50 |
* for (int j = 0; j < gs.length; j++) {
|
|
51 |
* GraphicsDevice gd = gs[j];
|
|
52 |
* GraphicsConfiguration[] gc =
|
|
53 |
* gd.getConfigurations();
|
|
54 |
* for (int i=0; i < gc.length; i++) {
|
|
55 |
* JFrame f = new
|
|
56 |
* JFrame(gs[j].getDefaultConfiguration());
|
|
57 |
* Canvas c = new Canvas(gc[i]);
|
|
58 |
* Rectangle gcBounds = gc[i].getBounds();
|
|
59 |
* int xoffs = gcBounds.x;
|
|
60 |
* int yoffs = gcBounds.y;
|
|
61 |
* f.getContentPane().add(c);
|
|
62 |
* f.setLocation((i*50)+xoffs, (i*60)+yoffs);
|
|
63 |
* f.show();
|
|
64 |
* }
|
|
65 |
* }
|
|
66 |
* </pre>
|
|
67 |
* <p>
|
|
68 |
* For more information on full-screen exclusive mode API, see the
|
|
69 |
* <a href="http://java.sun.com/docs/books/tutorial/extra/fullscreen/index.html">
|
|
70 |
* Full-Screen Exclusive Mode API Tutorial</a>.
|
|
71 |
*
|
|
72 |
* @see GraphicsEnvironment
|
|
73 |
* @see GraphicsConfiguration
|
|
74 |
*/
|
|
75 |
public abstract class GraphicsDevice {
|
|
76 |
|
|
77 |
private Window fullScreenWindow;
|
|
78 |
private AppContext fullScreenAppContext; // tracks which AppContext
|
|
79 |
// created the FS window
|
|
80 |
// this lock is used for making synchronous changes to the AppContext's
|
|
81 |
// current full screen window
|
|
82 |
private final Object fsAppContextLock = new Object();
|
|
83 |
|
|
84 |
private Rectangle windowedModeBounds;
|
|
85 |
|
|
86 |
/**
|
|
87 |
* This is an abstract class that cannot be instantiated directly.
|
|
88 |
* Instances must be obtained from a suitable factory or query method.
|
|
89 |
* @see GraphicsEnvironment#getScreenDevices
|
|
90 |
* @see GraphicsEnvironment#getDefaultScreenDevice
|
|
91 |
* @see GraphicsConfiguration#getDevice
|
|
92 |
*/
|
|
93 |
protected GraphicsDevice() {
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Device is a raster screen.
|
|
98 |
*/
|
|
99 |
public final static int TYPE_RASTER_SCREEN = 0;
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Device is a printer.
|
|
103 |
*/
|
|
104 |
public final static int TYPE_PRINTER = 1;
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Device is an image buffer. This buffer can reside in device
|
|
108 |
* or system memory but it is not physically viewable by the user.
|
|
109 |
*/
|
|
110 |
public final static int TYPE_IMAGE_BUFFER = 2;
|
|
111 |
|
|
112 |
/**
|
|
113 |
* Returns the type of this <code>GraphicsDevice</code>.
|
|
114 |
* @return the type of this <code>GraphicsDevice</code>, which can
|
|
115 |
* either be TYPE_RASTER_SCREEN, TYPE_PRINTER or TYPE_IMAGE_BUFFER.
|
|
116 |
* @see #TYPE_RASTER_SCREEN
|
|
117 |
* @see #TYPE_PRINTER
|
|
118 |
* @see #TYPE_IMAGE_BUFFER
|
|
119 |
*/
|
|
120 |
public abstract int getType();
|
|
121 |
|
|
122 |
/**
|
|
123 |
* Returns the identification string associated with this
|
|
124 |
* <code>GraphicsDevice</code>.
|
|
125 |
* <p>
|
|
126 |
* A particular program might use more than one
|
|
127 |
* <code>GraphicsDevice</code> in a <code>GraphicsEnvironment</code>.
|
|
128 |
* This method returns a <code>String</code> identifying a
|
|
129 |
* particular <code>GraphicsDevice</code> in the local
|
|
130 |
* <code>GraphicsEnvironment</code>. Although there is
|
|
131 |
* no public method to set this <code>String</code>, a programmer can
|
|
132 |
* use the <code>String</code> for debugging purposes. Vendors of
|
|
133 |
* the Java<sup><font size=-2>TM</font></sup> Runtime Environment can
|
|
134 |
* format the return value of the <code>String</code>. To determine
|
|
135 |
* how to interpret the value of the <code>String</code>, contact the
|
|
136 |
* vendor of your Java Runtime. To find out who the vendor is, from
|
|
137 |
* your program, call the
|
|
138 |
* {@link System#getProperty(String) getProperty} method of the
|
|
139 |
* System class with "java.vendor".
|
|
140 |
* @return a <code>String</code> that is the identification
|
|
141 |
* of this <code>GraphicsDevice</code>.
|
|
142 |
*/
|
|
143 |
public abstract String getIDstring();
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Returns all of the <code>GraphicsConfiguration</code>
|
|
147 |
* objects associated with this <code>GraphicsDevice</code>.
|
|
148 |
* @return an array of <code>GraphicsConfiguration</code>
|
|
149 |
* objects that are associated with this
|
|
150 |
* <code>GraphicsDevice</code>.
|
|
151 |
*/
|
|
152 |
public abstract GraphicsConfiguration[] getConfigurations();
|
|
153 |
|
|
154 |
/**
|
|
155 |
* Returns the default <code>GraphicsConfiguration</code>
|
|
156 |
* associated with this <code>GraphicsDevice</code>.
|
|
157 |
* @return the default <code>GraphicsConfiguration</code>
|
|
158 |
* of this <code>GraphicsDevice</code>.
|
|
159 |
*/
|
|
160 |
public abstract GraphicsConfiguration getDefaultConfiguration();
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Returns the "best" configuration possible that passes the
|
|
164 |
* criteria defined in the {@link GraphicsConfigTemplate}.
|
|
165 |
* @param gct the <code>GraphicsConfigTemplate</code> object
|
|
166 |
* used to obtain a valid <code>GraphicsConfiguration</code>
|
|
167 |
* @return a <code>GraphicsConfiguration</code> that passes
|
|
168 |
* the criteria defined in the specified
|
|
169 |
* <code>GraphicsConfigTemplate</code>.
|
|
170 |
* @see GraphicsConfigTemplate
|
|
171 |
*/
|
|
172 |
public GraphicsConfiguration
|
|
173 |
getBestConfiguration(GraphicsConfigTemplate gct) {
|
|
174 |
GraphicsConfiguration[] configs = getConfigurations();
|
|
175 |
return gct.getBestConfiguration(configs);
|
|
176 |
}
|
|
177 |
|
|
178 |
/**
|
|
179 |
* Returns <code>true</code> if this <code>GraphicsDevice</code>
|
|
180 |
* supports full-screen exclusive mode.
|
|
181 |
* If a SecurityManager is installed, its
|
|
182 |
* <code>checkPermission</code> method will be called
|
|
183 |
* with <code>AWTPermission("fullScreenExclusive")</code>.
|
|
184 |
* <code>isFullScreenSupported</code> returns true only if
|
|
185 |
* that permission is granted.
|
|
186 |
* @return whether full-screen exclusive mode is available for
|
|
187 |
* this graphics device
|
|
188 |
* @see java.awt.AWTPermission
|
|
189 |
* @since 1.4
|
|
190 |
*/
|
|
191 |
public boolean isFullScreenSupported() {
|
|
192 |
return false;
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Enter full-screen mode, or return to windowed mode. The entered
|
|
197 |
* full-screen mode may be either exclusive or simulated. Exclusive
|
|
198 |
* mode is only available if <code>isFullScreenSupported</code>
|
|
199 |
* returns <code>true</code>.
|
|
200 |
* <p>
|
|
201 |
* Exclusive mode implies:
|
|
202 |
* <ul>
|
|
203 |
* <li>Windows cannot overlap the full-screen window. All other application
|
|
204 |
* windows will always appear beneath the full-screen window in the Z-order.
|
|
205 |
* <li>There can be only one full-screen window on a device at any time,
|
|
206 |
* so calling this method while there is an existing full-screen Window
|
|
207 |
* will cause the existing full-screen window to
|
|
208 |
* return to windowed mode.
|
|
209 |
* <li>Input method windows are disabled. It is advisable to call
|
|
210 |
* <code>Component.enableInputMethods(false)</code> to make a component
|
|
211 |
* a non-client of the input method framework.
|
|
212 |
* </ul>
|
|
213 |
* <p>
|
|
214 |
* Simulated full-screen mode resizes
|
|
215 |
* the window to the size of the screen and positions it at (0,0).
|
|
216 |
* <p>
|
|
217 |
* When entering full-screen mode, if the window to be used as the
|
|
218 |
* full-screen window is not visible, this method will make it visible.
|
|
219 |
* It will remain visible when returning to windowed mode.
|
|
220 |
* <p>
|
|
221 |
* When returning to windowed mode from an exclusive full-screen window, any
|
|
222 |
* display changes made by calling <code>setDisplayMode</code> are
|
|
223 |
* automatically restored to their original state.
|
|
224 |
*
|
|
225 |
* @param w a window to use as the full-screen window; <code>null</code>
|
|
226 |
* if returning to windowed mode. Some platforms expect the
|
|
227 |
* fullscreen window to be a top-level component (i.e., a Frame);
|
|
228 |
* therefore it is preferable to use a Frame here rather than a
|
|
229 |
* Window.
|
|
230 |
* @see #isFullScreenSupported
|
|
231 |
* @see #getFullScreenWindow
|
|
232 |
* @see #setDisplayMode
|
|
233 |
* @see Component#enableInputMethods
|
|
234 |
* @see Component#setVisible
|
|
235 |
* @since 1.4
|
|
236 |
*/
|
|
237 |
public void setFullScreenWindow(Window w) {
|
|
238 |
if (fullScreenWindow != null && windowedModeBounds != null) {
|
887
|
239 |
// if the window went into fs mode before it was realized it may
|
|
240 |
// have (0,0) dimensions
|
|
241 |
if (windowedModeBounds.width == 0) windowedModeBounds.width = 1;
|
|
242 |
if (windowedModeBounds.height == 0) windowedModeBounds.height = 1;
|
2
|
243 |
fullScreenWindow.setBounds(windowedModeBounds);
|
|
244 |
}
|
|
245 |
// Set the full screen window
|
|
246 |
synchronized (fsAppContextLock) {
|
|
247 |
// Associate fullscreen window with current AppContext
|
|
248 |
if (w == null) {
|
|
249 |
fullScreenAppContext = null;
|
|
250 |
} else {
|
|
251 |
fullScreenAppContext = AppContext.getAppContext();
|
|
252 |
}
|
|
253 |
fullScreenWindow = w;
|
|
254 |
}
|
|
255 |
if (fullScreenWindow != null) {
|
|
256 |
windowedModeBounds = fullScreenWindow.getBounds();
|
|
257 |
// Note that we use the graphics configuration of the device,
|
|
258 |
// not the window's, because we're setting the fs window for
|
|
259 |
// this device.
|
|
260 |
Rectangle screenBounds = getDefaultConfiguration().getBounds();
|
|
261 |
fullScreenWindow.setBounds(screenBounds.x, screenBounds.y,
|
|
262 |
screenBounds.width, screenBounds.height);
|
|
263 |
fullScreenWindow.setVisible(true);
|
|
264 |
fullScreenWindow.toFront();
|
|
265 |
}
|
|
266 |
}
|
|
267 |
|
|
268 |
/**
|
|
269 |
* Returns the <code>Window</code> object representing the
|
|
270 |
* full-screen window if the device is in full-screen mode.
|
|
271 |
*
|
|
272 |
* @return the full-screen window, or <code>null</code> if the device is
|
|
273 |
* not in full-screen mode.
|
|
274 |
* @see #setFullScreenWindow(Window)
|
|
275 |
* @since 1.4
|
|
276 |
*/
|
|
277 |
public Window getFullScreenWindow() {
|
|
278 |
Window returnWindow = null;
|
|
279 |
synchronized (fsAppContextLock) {
|
|
280 |
// Only return a handle to the current fs window if we are in the
|
|
281 |
// same AppContext that set the fs window
|
|
282 |
if (fullScreenAppContext == AppContext.getAppContext()) {
|
|
283 |
returnWindow = fullScreenWindow;
|
|
284 |
}
|
|
285 |
}
|
|
286 |
return returnWindow;
|
|
287 |
}
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Returns <code>true</code> if this <code>GraphicsDevice</code>
|
|
291 |
* supports low-level display changes.
|
|
292 |
* On some platforms low-level display changes may only be allowed in
|
|
293 |
* full-screen exclusive mode (i.e., if {@link #isFullScreenSupported()}
|
|
294 |
* returns {@code true} and the application has already entered
|
|
295 |
* full-screen mode using {@link #setFullScreenWindow}).
|
|
296 |
* @return whether low-level display changes are supported for this
|
|
297 |
* graphics device.
|
|
298 |
* @see #isFullScreenSupported
|
|
299 |
* @see #setDisplayMode
|
|
300 |
* @see #setFullScreenWindow
|
|
301 |
* @since 1.4
|
|
302 |
*/
|
|
303 |
public boolean isDisplayChangeSupported() {
|
|
304 |
return false;
|
|
305 |
}
|
|
306 |
|
|
307 |
/**
|
|
308 |
* Sets the display mode of this graphics device. This is only allowed
|
|
309 |
* if {@link #isDisplayChangeSupported()} returns {@code true} and may
|
|
310 |
* require first entering full-screen exclusive mode using
|
|
311 |
* {@link #setFullScreenWindow} providing that full-screen exclusive mode is
|
|
312 |
* supported (i.e., {@link #isFullScreenSupported()} returns
|
|
313 |
* {@code true}).
|
|
314 |
* <p>
|
|
315 |
*
|
|
316 |
* The display mode must be one of the display modes returned by
|
|
317 |
* {@link #getDisplayModes()}, with one exception: passing a display mode
|
|
318 |
* with {@link DisplayMode#REFRESH_RATE_UNKNOWN} refresh rate will result in
|
|
319 |
* selecting a display mode from the list of available display modes with
|
|
320 |
* matching width, height and bit depth.
|
|
321 |
* However, passing a display mode with {@link DisplayMode#BIT_DEPTH_MULTI}
|
|
322 |
* for bit depth is only allowed if such mode exists in the list returned by
|
|
323 |
* {@link #getDisplayModes()}.
|
|
324 |
* <p>
|
|
325 |
* Example code:
|
|
326 |
* <pre><code>
|
|
327 |
* Frame frame;
|
|
328 |
* DisplayMode newDisplayMode;
|
|
329 |
* GraphicsDevice gd;
|
|
330 |
* // create a Frame, select desired DisplayMode from the list of modes
|
|
331 |
* // returned by gd.getDisplayModes() ...
|
|
332 |
*
|
|
333 |
* if (gd.isFullScreenSupported()) {
|
|
334 |
* gd.setFullScreenWindow(frame);
|
|
335 |
* } else {
|
|
336 |
* // proceed in non-full-screen mode
|
|
337 |
* frame.setSize(...);
|
|
338 |
* frame.setLocation(...);
|
|
339 |
* frame.setVisible(true);
|
|
340 |
* }
|
|
341 |
*
|
|
342 |
* if (gd.isDisplayChangeSupported()) {
|
|
343 |
* gd.setDisplayMode(newDisplayMode);
|
|
344 |
* }
|
|
345 |
* </code></pre>
|
|
346 |
*
|
|
347 |
* @param dm The new display mode of this graphics device.
|
|
348 |
* @exception IllegalArgumentException if the <code>DisplayMode</code>
|
|
349 |
* supplied is <code>null</code>, or is not available in the array returned
|
|
350 |
* by <code>getDisplayModes</code>
|
|
351 |
* @exception UnsupportedOperationException if
|
|
352 |
* <code>isDisplayChangeSupported</code> returns <code>false</code>
|
|
353 |
* @see #getDisplayMode
|
|
354 |
* @see #getDisplayModes
|
|
355 |
* @see #isDisplayChangeSupported
|
|
356 |
* @since 1.4
|
|
357 |
*/
|
|
358 |
public void setDisplayMode(DisplayMode dm) {
|
|
359 |
throw new UnsupportedOperationException("Cannot change display mode");
|
|
360 |
}
|
|
361 |
|
|
362 |
/**
|
|
363 |
* Returns the current display mode of this
|
|
364 |
* <code>GraphicsDevice</code>.
|
|
365 |
* The returned display mode is allowed to have a refresh rate
|
|
366 |
* {@link DisplayMode#REFRESH_RATE_UNKNOWN} if it is indeterminate.
|
|
367 |
* Likewise, the returned display mode is allowed to have a bit depth
|
|
368 |
* {@link DisplayMode#BIT_DEPTH_MULTI} if it is indeterminate or if multiple
|
|
369 |
* bit depths are supported.
|
|
370 |
* @return the current display mode of this graphics device.
|
|
371 |
* @see #setDisplayMode(DisplayMode)
|
|
372 |
* @since 1.4
|
|
373 |
*/
|
|
374 |
public DisplayMode getDisplayMode() {
|
|
375 |
GraphicsConfiguration gc = getDefaultConfiguration();
|
|
376 |
Rectangle r = gc.getBounds();
|
|
377 |
ColorModel cm = gc.getColorModel();
|
|
378 |
return new DisplayMode(r.width, r.height, cm.getPixelSize(), 0);
|
|
379 |
}
|
|
380 |
|
|
381 |
/**
|
|
382 |
* Returns all display modes available for this
|
|
383 |
* <code>GraphicsDevice</code>.
|
|
384 |
* The returned display modes are allowed to have a refresh rate
|
|
385 |
* {@link DisplayMode#REFRESH_RATE_UNKNOWN} if it is indeterminate.
|
|
386 |
* Likewise, the returned display modes are allowed to have a bit depth
|
|
387 |
* {@link DisplayMode#BIT_DEPTH_MULTI} if it is indeterminate or if multiple
|
|
388 |
* bit depths are supported.
|
|
389 |
* @return all of the display modes available for this graphics device.
|
|
390 |
* @since 1.4
|
|
391 |
*/
|
|
392 |
public DisplayMode[] getDisplayModes() {
|
|
393 |
return new DisplayMode[] { getDisplayMode() };
|
|
394 |
}
|
|
395 |
|
|
396 |
/**
|
|
397 |
* This method returns the number of bytes available in
|
|
398 |
* accelerated memory on this device.
|
|
399 |
* Some images are created or cached
|
|
400 |
* in accelerated memory on a first-come,
|
|
401 |
* first-served basis. On some operating systems,
|
|
402 |
* this memory is a finite resource. Calling this method
|
|
403 |
* and scheduling the creation and flushing of images carefully may
|
|
404 |
* enable applications to make the most efficient use of
|
|
405 |
* that finite resource.
|
|
406 |
* <br>
|
|
407 |
* Note that the number returned is a snapshot of how much
|
|
408 |
* memory is available; some images may still have problems
|
|
409 |
* being allocated into that memory. For example, depending
|
|
410 |
* on operating system, driver, memory configuration, and
|
|
411 |
* thread situations, the full extent of the size reported
|
|
412 |
* may not be available for a given image. There are further
|
|
413 |
* inquiry methods on the {@link ImageCapabilities} object
|
|
414 |
* associated with a VolatileImage that can be used to determine
|
|
415 |
* whether a particular VolatileImage has been created in accelerated
|
|
416 |
* memory.
|
|
417 |
* @return number of bytes available in accelerated memory.
|
|
418 |
* A negative return value indicates that the amount of accelerated memory
|
|
419 |
* on this GraphicsDevice is indeterminate.
|
|
420 |
* @see java.awt.image.VolatileImage#flush
|
|
421 |
* @see ImageCapabilities#isAccelerated
|
|
422 |
* @since 1.4
|
|
423 |
*/
|
|
424 |
public int getAvailableAcceleratedMemory() {
|
|
425 |
return -1;
|
|
426 |
}
|
|
427 |
}
|