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

📄 fullscreen.java

📁 一个用java编的全屏显示代码
💻 JAVA
字号:
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;
/**
* 
* @author fubin www.cujava.com
* 这是全屏幕显示模块.但是这个程序消耗资源比较多。
*/
public class FullScreen {

 private static int counter = 0;

 private static final int MAX = 50;

 private static DisplayMode MODES[] = new DisplayMode[] {
     new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 16, 0),
     new DisplayMode(640, 480, 8, 0) };

 private static DisplayMode getBestDisplayMode(GraphicsDevice device) {
   for (int x = 0, xn = MODES.length; x < xn; x++) {
     DisplayMode[] modes = device.getDisplayModes();
     for (int i = 0, in = modes.length; i < in; i++) {
       if (modes[i].getWidth() == MODES[x].getWidth()
           && modes[i].getHeight() == MODES[x].getHeight()
           && modes[i].getBitDepth() == MODES[x].getBitDepth()) {
         return MODES[x];
       }
     }
   }
   return null;
 }

 public static void main(String args[]) {
   GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment
       .getLocalGraphicsEnvironment();
   GraphicsDevice graphicsDevice = graphicsEnvironment
       .getDefaultScreenDevice();
   DisplayMode originalDisplayMode = graphicsDevice.getDisplayMode();

   try {
     Frame frame = new Frame();
     frame.setUndecorated(true);
     frame.setIgnoreRepaint(true);
     graphicsDevice.setFullScreenWindow(frame);
     if (graphicsDevice.isDisplayChangeSupported()) {
       graphicsDevice
           .setDisplayMode(getBestDisplayMode(graphicsDevice));
     }
     frame.createBufferStrategy(2); // 2 buffers
     Rectangle bounds = frame.getBounds();
     BufferStrategy bufferStrategy = frame.getBufferStrategy();
     while (!done()) {
       Graphics g = null;
       try {
         g = bufferStrategy.getDrawGraphics();
         if ((counter <= 2)) { // 2 buffers
           g.setColor(Color.CYAN);
           g.fillRect(0, 0, bounds.width, bounds.height);
         }
         g.setColor(Color.RED);
         // redraw prior line, too, since 2 buffers
         if (counter != 1) {
           g.drawLine(counter - 1, (counter - 1) * 5,
               bounds.width, bounds.height);
         }
         g.drawLine(counter, counter * 5, bounds.width,
             bounds.height);
         bufferStrategy.show();
       } finally {
         if (g != null) {
           g.dispose();
         }
       }
       try {
         Thread.sleep(250);
       } catch (InterruptedException ignored) {
       }
     }
   } finally {
     graphicsDevice.setDisplayMode(originalDisplayMode);
     graphicsDevice.setFullScreenWindow(null);
   }
   System.exit(0);
 }

 private static boolean done() {
   return (counter++ == MAX);
 }
} 

⌨️ 快捷键说明

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