下面列出了怎么用java.awt.GraphicsDevice的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Finds a display mode that is different from the current display
* mode and is likely to cause a display change event.
*/
private static DisplayMode findDisplayMode(GraphicsDevice gd) {
DisplayMode dms[] = gd.getDisplayModes();
DisplayMode currentDM = gd.getDisplayMode();
for (DisplayMode dm : dms) {
if (!dm.equals(currentDM) &&
dm.getRefreshRate() == currentDM.getRefreshRate())
{
// different from the current dm and refresh rate is the same
// means that something else is different => more likely to
// cause a DM change event
return dm;
}
}
return null;
}
public static void main(String[] args) throws Exception {
DisplayChangeVITest test = new DisplayChangeVITest();
GraphicsDevice gd =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
gd.setFullScreenWindow(test);
Thread t = new Thread(test);
t.run();
synchronized (lock) {
while (!done) {
try {
lock.wait(50);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
System.err.println("Test Passed.");
} else {
System.err.println("Full screen not supported. Test passed.");
}
}
@Override
public GraphicsDevice getGraphicsDevice() {
CGraphicsEnvironment ge = (CGraphicsEnvironment)GraphicsEnvironment.
getLocalGraphicsEnvironment();
LWLightweightFramePeer peer = (LWLightweightFramePeer)getPeer();
int scale = ((LightweightFrame)peer.getTarget()).getScaleFactor();
Rectangle bounds = ((LightweightFrame)peer.getTarget()).getHostBounds();
for (GraphicsDevice d : ge.getScreenDevices()) {
if (d.getDefaultConfiguration().getBounds().intersects(bounds) &&
((CGraphicsDevice)d).getScaleFactor() == scale)
{
return d;
}
}
// We shouldn't be here...
return ge.getDefaultScreenDevice();
}
public static void printDisplayInfo() {
int g = 0;
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
out.println("graphics device #"+(++g)+": "+gd.getIDstring()+" type "+gd.getType());
out.println("\tavailable accelerated memory " + gd.getAvailableAcceleratedMemory());
int c = 0;
for (GraphicsConfiguration gc : gd.getConfigurations()) {
out.println("\tgraphics configuration #"+(++c)+":");
out.println("\t\twidth "+gc.getBounds().getWidth()+" height "+gc.getBounds().getHeight());
out.println("\t\tfull screen "+gc.getBufferCapabilities().isFullScreenRequired());
ImageCapabilities ic = gc.getImageCapabilities();
out.println("\t\tis accelerated "+ic.isAccelerated());
}
DisplayMode dm = gd.getDisplayMode();
out.println("\tdisplay mode bit width "+dm.getWidth()+" height "+dm.getHeight()+" bit depth "+dm.getBitDepth()+" refresh rate "+dm.getRefreshRate());
int m = 0;
for (DisplayMode d : gd.getDisplayModes())
out.println("\talt display mode #"+(++m)+" bit width "+d.getWidth()+" height "+d.getHeight()+" bit depth "+d.getBitDepth()+" refresh rate "+d.getRefreshRate());
}
}
static void initScreenBounds() {
GraphicsDevice[] devices = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getScreenDevices();
screenBounds = new Rectangle[devices.length];
scales = new double[devices.length][2];
for (int i = 0; i < devices.length; i++) {
GraphicsConfiguration gc = devices[i].getDefaultConfiguration();
screenBounds[i] = gc.getBounds();
AffineTransform tx = gc.getDefaultTransform();
scales[i][0] = tx.getScaleX();
scales[i][1] = tx.getScaleY();
}
maxBounds = screenBounds[0];
for (int i = 0; i < screenBounds.length; i++) {
maxBounds = maxBounds.union(screenBounds[i]);
}
}
/**
* Initialize the splash screen.
*
* @param gd The {@code GraphicsDevice} to display on.
* @param splashStream An {@code InputStream} to read content from.
*/
public SplashScreen(GraphicsDevice gd, InputStream splashStream)
throws IOException {
super(gd.getDefaultConfiguration());
BufferedImage im = ImageIO.read(splashStream);
this.getContentPane().add(new JLabel(new ImageIcon(im)));
this.pack();
this.setVisible(false);
Point start = this.getLocation();
DisplayMode dm = gd.getDisplayMode();
int x = start.x + dm.getWidth()/2 - this.getWidth() / 2;
int y = start.y + dm.getHeight()/2 - this.getHeight() / 2;
this.setLocation(x, y);
}
public static void main(String[] args) throws Exception {
DisplayChangeVITest test = new DisplayChangeVITest();
GraphicsDevice gd =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
gd.setFullScreenWindow(test);
Thread t = new Thread(test);
t.run();
synchronized (lock) {
while (!done) {
try {
lock.wait(50);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
System.err.println("Test Passed.");
} else {
System.err.println("Full screen not supported. Test passed.");
}
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
/**
* Finds a display mode that is different from the current display
* mode and is likely to cause a display change event.
*/
private static DisplayMode findDisplayMode(GraphicsDevice gd) {
DisplayMode dms[] = gd.getDisplayModes();
DisplayMode currentDM = gd.getDisplayMode();
for (DisplayMode dm : dms) {
if (!dm.equals(currentDM) &&
dm.getRefreshRate() == currentDM.getRefreshRate())
{
// different from the current dm and refresh rate is the same
// means that something else is different => more likely to
// cause a DM change event
return dm;
}
}
return null;
}
/**
* Finds a display mode that is different from the current display
* mode and is likely to cause a display change event.
*/
private static DisplayMode findDisplayMode(GraphicsDevice gd) {
DisplayMode dms[] = gd.getDisplayModes();
DisplayMode currentDM = gd.getDisplayMode();
for (DisplayMode dm : dms) {
if (!dm.equals(currentDM) &&
dm.getRefreshRate() == currentDM.getRefreshRate())
{
// different from the current dm and refresh rate is the same
// means that something else is different => more likely to
// cause a DM change event
return dm;
}
}
return null;
}
private static GraphicsDevice checkConfigs(GraphicsDevice[] devices) {
GraphicsDevice correctDevice = null;
if (devices.length < 2) {
return correctDevice;
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenBounds = new Rectangle(toolkit.getScreenSize());
int halfScreen = screenBounds.height/2;
for(int i = 0; i < devices.length; i++) {
if(devices[i].getType() == GraphicsDevice.TYPE_RASTER_SCREEN) {
GraphicsConfiguration conf =
devices[i].getDefaultConfiguration();
Rectangle bounds = conf.getBounds();
if (bounds.y >= halfScreen) {
// found
correctDevice = devices[i];
break;
}
}
}
return correctDevice;
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicDevice = ge.getDefaultScreenDevice();
if (!graphicDevice.isDisplayChangeSupported()) {
System.err.println("Display mode change is not supported on this host. Test is considered passed.");
return;
}
DisplayMode defaultDisplayMode = graphicDevice.getDisplayMode();
checkDisplayMode(defaultDisplayMode);
graphicDevice.setDisplayMode(defaultDisplayMode);
DisplayMode[] displayModes = graphicDevice.getDisplayModes();
boolean isDefaultDisplayModeIncluded = false;
for (DisplayMode displayMode : displayModes) {
checkDisplayMode(displayMode);
graphicDevice.setDisplayMode(displayMode);
if (defaultDisplayMode.equals(displayMode)) {
isDefaultDisplayModeIncluded = true;
}
}
if (!isDefaultDisplayModeIncluded) {
throw new RuntimeException("Default display mode is not included");
}
}
/**
* Determine whether to use full screen or windowed mode.
*
* @param gd The {@code GraphicsDevice} to display to.
* @param desiredSize An optional window {@code Dimension}.
* @return Null if full screen is to be used, otherwise a window
* bounds {@code Rectangle}
*/
private static boolean checkWindowed(GraphicsDevice gd,
Dimension desiredSize) {
boolean ret;
if (desiredSize == null) {
if (gd.isFullScreenSupported()) {
logger.info("Full screen mode used.");
ret = false;
} else {
logger.warning("Full screen mode not supported.");
System.err.println(Messages.message("client.fullScreen"));
ret = true;
}
} else {
logger.info("Windowed mode used.");
ret = true;
}
return ret;
}
public static void main(final String[] args)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> {
final GraphicsDevice[] devices =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getScreenDevices();
final Frame frame = new Frame();
frame.setBackground(Color.GREEN);
frame.setUndecorated(true);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
sleep();
for (final GraphicsDevice gd : devices) {
try {
gd.setFullScreenWindow(frame);
if (gd.getFullScreenWindow() != frame) {
throw new RuntimeException("Wrong window");
}
if (frame.getGraphicsConfiguration().getDevice() != gd) {
throw new RuntimeException("Wrong new GraphicsDevice");
}
} finally {
// cleaning up
gd.setFullScreenWindow(null);
}
}
frame.dispose();
});
}
public static X11GraphicsConfig getGC(X11ComponentPeer peer) {
if (peer != null) {
return (X11GraphicsConfig) peer.getGraphicsConfiguration();
} else {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
return (X11GraphicsConfig)gd.getDefaultConfiguration();
}
}
/**
* Sets the window to fullscreen.
*/
private void setFullscreen() {
final GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice[] devices = env.getScreenDevices();
final GraphicsDevice device = devices[0];
device.setFullScreenWindow(frame);
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(16, 16);
vi.validate(gc);
BufferedImage bi =
new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
data[0] = 0x0000007f;
data[1] = 0x0000007f;
data[2] = 0xff00007f;
data[3] = 0xff00007f;
Graphics2D g = vi.createGraphics();
g.setComposite(AlphaComposite.SrcOver.derive(0.999f));
g.drawImage(bi, 0, 0, null);
bi = vi.getSnapshot();
if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) {
throw new RuntimeException("Test FAILED: color at 0x0 ="+
Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+
Integer.toHexString(bi.getRGB(1,1)));
}
System.out.println("Test PASSED.");
}
private void checkEffects() {
GraphicsDevice gd = getGraphicsConfiguration().getDevice();
if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
shapedCb.setEnabled(false);
}
if (!gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) {
transparencySld.setEnabled(false);
}
if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
nonOpaqueChb.setEnabled(false);
}
}
private static D3DGraphicsConfig getGC(WComponentPeer peer) {
GraphicsConfiguration gc;
if (peer != null) {
gc = peer.getGraphicsConfiguration();
} else {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
gc = gd.getDefaultConfiguration();
}
return (gc instanceof D3DGraphicsConfig) ? (D3DGraphicsConfig)gc : null;
}
public static void main(final String[] args)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> {
final GraphicsDevice[] devices =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getScreenDevices();
final Frame frame = new Frame();
frame.setBackground(Color.GREEN);
frame.setUndecorated(true);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
sleep();
for (final GraphicsDevice gd : devices) {
try {
gd.setFullScreenWindow(frame);
if (gd.getFullScreenWindow() != frame) {
throw new RuntimeException("Wrong window");
}
if (frame.getGraphicsConfiguration().getDevice() != gd) {
throw new RuntimeException("Wrong new GraphicsDevice");
}
} finally {
// cleaning up
gd.setFullScreenWindow(null);
}
}
frame.dispose();
});
}
/**
* Return the bounds of a GraphicsDevice, less its screen insets.
* See also java.awt.GraphicsEnvironment.getUsableBounds();
*/
public static Rectangle getUsableBounds(GraphicsDevice gd) {
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
Rectangle usableBounds = gc.getBounds();
usableBounds.x += insets.left;
usableBounds.y += insets.top;
usableBounds.width -= (insets.left + insets.right);
usableBounds.height -= (insets.top + insets.bottom);
return usableBounds;
}
private void checkEffects() {
GraphicsDevice gd = getGraphicsConfiguration().getDevice();
if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSPARENT)) {
shapedCb.setEnabled(false);
}
if (!gd.isWindowTranslucencySupported(WindowTranslucency.TRANSLUCENT)) {
transparencySld.setEnabled(false);
}
if (!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT)) {
nonOpaqueChb.setEnabled(false);
}
}
public static GLXGraphicsConfig getGC(X11ComponentPeer peer) {
if (peer != null) {
return (GLXGraphicsConfig)peer.getGraphicsConfiguration();
} else {
// REMIND: this should rarely (never?) happen, but what if
// default config is not GLX?
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
return (GLXGraphicsConfig)gd.getDefaultConfiguration();
}
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(16, 16);
vi.validate(gc);
BufferedImage bi =
new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
data[0] = 0x0000007f;
data[1] = 0x0000007f;
data[2] = 0xff00007f;
data[3] = 0xff00007f;
Graphics2D g = vi.createGraphics();
g.setComposite(AlphaComposite.SrcOver.derive(0.999f));
g.drawImage(bi, 0, 0, null);
bi = vi.getSnapshot();
if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) {
throw new RuntimeException("Test FAILED: color at 0x0 ="+
Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+
Integer.toHexString(bi.getRGB(1,1)));
}
System.out.println("Test PASSED.");
}
/**
* Returns the graphics device that this configuration is associated with.
*
* @return The graphics device (never {@code null}).
*/
@Override
public GraphicsDevice getDevice() {
if (this.device == null) {
this.device = new SVGGraphicsDevice("JFreeSVG-GraphicsDevice", this);
}
return this.device;
}
/**
* Returns the default screen graphics device.
*/
public GraphicsDevice getDefaultScreenDevice() {
GraphicsDevice[] screens = getScreenDevices();
if (screens.length == 0) {
throw new AWTError("no screen devices");
}
return screens[0];
}
public static void main(String[] args) {
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(16, 16);
vi.validate(gc);
BufferedImage bi =
new BufferedImage(2, 2, BufferedImage.TYPE_INT_RGB);
int data[] = ((DataBufferInt)bi.getRaster().getDataBuffer()).getData();
data[0] = 0x0000007f;
data[1] = 0x0000007f;
data[2] = 0xff00007f;
data[3] = 0xff00007f;
Graphics2D g = vi.createGraphics();
g.setComposite(AlphaComposite.SrcOver.derive(0.999f));
g.drawImage(bi, 0, 0, null);
bi = vi.getSnapshot();
if (bi.getRGB(0, 0) != bi.getRGB(1, 1)) {
throw new RuntimeException("Test FAILED: color at 0x0 ="+
Integer.toHexString(bi.getRGB(0, 0))+" differs from 1x1 ="+
Integer.toHexString(bi.getRGB(1,1)));
}
System.out.println("Test PASSED.");
}