⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 screenmanager.java

📁 goldminer游戏
💻 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 类描述了可以在特殊图形环境中使用的图形设备。这些设备包括屏幕和打印机设备。
    //GraphicsDevice 对象可以是屏幕、打印机或图像缓冲区,并且都是 Graphics2D 绘图方法的目标。
    private GraphicsDevice device;
    private JFrame frame = null;

    /**
     * 构造函数。
     */
    public ScreenManager() {
        //GraphicsEnvironment 类描述了 Java(tm) 应用程序在特定平台上可用的 GraphicsDevice 对象和 Font 对象的集合。
        GraphicsEnvironment environment =
            GraphicsEnvironment.getLocalGraphicsEnvironment();
        device = environment.getDefaultScreenDevice();//获取默认显示设备
    }

    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;
    }


    /**
     * 当前的显示模式。
     * @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;
    }

    /**
     * 设置为全屏显示模式。
     * @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() {
                    //为此组件上的多缓冲创建一个新策略。多缓冲对于呈现性能非常有用。
                    //此方法试图根据提供的缓冲数创建可用的最佳策略。它将始终根据该缓冲数创建 BufferStrategy。
                    frame.createBufferStrategy(2);
                }
            });
        }
        catch (InterruptedException ex) {
            // ignore
        }
        catch (InvocationTargetException  ex) {
            // ignore
        }
    }


    /**
     * 取当前设备的图形上下文.
     * @return Graphics2D
     */
    public Graphics2D getGraphics() {
        //Window window = device.getFullScreenWindow();
        if (frame != null) {
            BufferStrategy strategy = frame.getBufferStrategy();
            return (Graphics2D)strategy.getDrawGraphics();
        }
        else {
            return null;
        }
    }


    /**
     * 提交结果,通过复制内存(位图传输)或改变显示指针(翻转)使下一个可用缓冲区变为可见。
     */
    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();
    }


    /**
     * 如果设备处于全屏模式,则返回表示全屏窗口的 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、其子组件及其拥有的所有子组件所使用的所有本机屏幕资源。
     */
    public void restoreScreen() {
        //Window window = device.getFullScreenWindow();
        if (frame != null) {
            frame.dispose();
        }
        device.setFullScreenWindow(null);
    }

   /**
    * 创建一个适应当前显示模式的图象缓冲区。
    * @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;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -