?? screenmanager.java
字號:
package com.hbwhzdg.goldminer.gamecore;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
/**
*
* <p>Title: 管理屏幕初始化,顯示為全屏模式</p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ScreenManager {
private static final ScreenManager instance = new ScreenManager();
//GraphicsDevice 類描述了可以在特殊圖形環(huán)境中使用的圖形設(shè)備。這些設(shè)備包括屏幕和打印機(jī)設(shè)備。
//GraphicsDevice 對象可以是屏幕、打印機(jī)或圖像緩沖區(qū),并且都是 Graphics2D 繪圖方法的目標(biāo)。
private GraphicsDevice device;
private JFrame frame = null;
/**
* 構(gòu)造函數(shù)。
*/
public ScreenManager() {
//GraphicsEnvironment 類描述了 Java(tm) 應(yīng)用程序在特定平臺上可用的 GraphicsDevice 對象和 Font 對象的集合。
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();//獲取默認(rèn)顯示設(shè)備
}
public static ScreenManager getInstance() {
return instance;
}
/**
* 返回此 GraphicsDevice 可用的所有顯示模式。
* @return DisplayMode[]
*/
public DisplayMode[] getCompatibleDisplayModes() {
return device.getDisplayModes();
}
/**
* 返回指定顯示模式中第一個適配的模式。
* @param modes DisplayMode[]
* @return DisplayMode
*/
public DisplayMode findFirstCompatibleMode(
DisplayMode modes[])
{
DisplayMode goodModes[] = device.getDisplayModes();
for (int i = 0; i < modes.length; i++) {
for (int j = 0; j < goodModes.length; j++) {
if (displayModesMatch(modes[i], goodModes[j])) {
return modes[i];
}
}
}
return null;
}
/**
* 當(dāng)前的顯示模式。
* @return DisplayMode
*/
public DisplayMode getCurrentDisplayMode() {
return device.getDisplayMode();
}
/**
* 比較指定的兩種模式是否一樣。
* @param mode1 DisplayMode
* @param mode2 DisplayMode
* @return boolean
*/
public boolean displayModesMatch(DisplayMode mode1,
DisplayMode mode2)
{
if (mode1.getWidth() != mode2.getWidth() ||
mode1.getHeight() != mode2.getHeight())
{
return false;
}
if (mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
mode1.getBitDepth() != mode2.getBitDepth())
{
return false;
}
if (mode1.getRefreshRate() !=
DisplayMode.REFRESH_RATE_UNKNOWN &&
mode2.getRefreshRate() !=
DisplayMode.REFRESH_RATE_UNKNOWN &&
mode1.getRefreshRate() != mode2.getRefreshRate())
{
return false;
}
return true;
}
/**
* 設(shè)置為全屏顯示模式。
* @param displayMode DisplayMode
*/
public void setFullScreen(DisplayMode displayMode) {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
frame.setResizable(false);
device.setFullScreenWindow(frame);
if (displayMode != null &&
device.isDisplayChangeSupported())
{
try {
device.setDisplayMode(displayMode);
}
catch (IllegalArgumentException ex) { }
// fix for mac os x
frame.setSize(displayMode.getWidth(),
displayMode.getHeight());
}
// avoid potential deadlock in 1.4.1_02
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
//為此組件上的多緩沖創(chuàng)建一個新策略。多緩沖對于呈現(xiàn)性能非常有用。
//此方法試圖根據(jù)提供的緩沖數(shù)創(chuàng)建可用的最佳策略。它將始終根據(jù)該緩沖數(shù)創(chuàng)建 BufferStrategy。
frame.createBufferStrategy(2);
}
});
}
catch (InterruptedException ex) {
// ignore
}
catch (InvocationTargetException ex) {
// ignore
}
}
/**
* 取當(dāng)前設(shè)備的圖形上下文.
* @return Graphics2D
*/
public Graphics2D getGraphics() {
//Window window = device.getFullScreenWindow();
if (frame != null) {
BufferStrategy strategy = frame.getBufferStrategy();
return (Graphics2D)strategy.getDrawGraphics();
}
else {
return null;
}
}
/**
* 提交結(jié)果,通過復(fù)制內(nèi)存(位圖傳輸)或改變顯示指針(翻轉(zhuǎn))使下一個可用緩沖區(qū)變?yōu)榭梢姟? */
public void update() {
//Window window = device.getFullScreenWindow();
if (frame != null) {
BufferStrategy strategy = frame.getBufferStrategy();
if (!strategy.contentsLost()) {
strategy.show();
}
}
// Sync the display on some systems.
// (on Linux, this fixes event queue problems)
Toolkit.getDefaultToolkit().sync();
}
/**
* 如果設(shè)備處于全屏模式,則返回表示全屏窗口的 Window 對象。
* @return JFrame
*/
public JFrame getFullScreenWindow() {
//return (JFrame)device.getFullScreenWindow();
return frame;
}
/**
* 返回屏幕寬度。
* @return int
*/
public int getWidth() {
//Window window = device.getFullScreenWindow();
if (frame != null) {
return frame.getWidth();
}
else {
return 0;
}
}
/**
* 返回屏幕高度。
* @return int
*/
public int getHeight() {
//Window window = device.getFullScreenWindow();
if (frame != null) {
return frame.getHeight();
}
else {
return 0;
}
}
/**
* 釋放由此 Window、其子組件及其擁有的所有子組件所使用的所有本機(jī)屏幕資源。
*/
public void restoreScreen() {
//Window window = device.getFullScreenWindow();
if (frame != null) {
frame.dispose();
}
device.setFullScreenWindow(null);
}
/**
* 創(chuàng)建一個適應(yīng)當(dāng)前顯示模式的圖象緩沖區(qū)。
* @param w int
* @param h int
* @param transparancy int
* @return BufferedImage
*/
public BufferedImage createCompatibleImage(int w, int h,
int transparancy)
{
//Window window = device.getFullScreenWindow();
if (frame != null) {
GraphicsConfiguration gc =
frame.getGraphicsConfiguration();
return gc.createCompatibleImage(w, h, transparancy);
}
return null;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -