CSDN博客

img yuanqingfei

Java的秘密:使用全屏幕模式

发表于2004/7/10 17:23:00  663人阅读

发信人: ttlink (Anakin Skywalker), 信区: Java                                                                          
标  题: Java的秘密:使用全屏幕模式 (zz)                                                                                
发信站: 日月光华 (2003年04月16日09:08:24 星期三), 站内信件                                                             

http://www.csdn.net/develop/article/17/17856.shtm                                                                      
turbochen(原作)                                                                                                      
什么时候会用到全屏幕模式?                                                                                             
    也许用到的机会很少,但JDK还是为我们提供了这个的功能。像许多软件中的                                                
打印预览功能,还有某些文本编辑器中为了获得更大的编辑画面,也用到了全屏幕                                               
模式,如果你有兴趣写一个像ACDSee这样的软件,使用全屏幕模式可以让用户看到                                               
更大的图片画面。                                                                                                       

如何使用全屏幕模式?                                                                                                   
    关键是java.awt.*里面的两个与显示设备有关的类:GraphicsEnvironment和                                                
GraphicsDevice。                                                                                                       
                                                                                                                       
    GraphicsEnvironment为Java应用程序提供了特定平台的 GraphicsDevice                                                   
对象和 Font 对象集合。这些GraphicsDevice可以是各种本机和远端机器的资源,                                               
如屏幕、打印机或者是Image Buffer,甚至是Graphics2D绘图方法的目标对象。                                                  

    而GraphicsDevice就是指特定的图形环境了,如屏幕和打印设备等。这样,我                                               
们就可以用GraphicsDevice来操纵屏幕了。GraphicsDevice提供的                                                             
setFullScreenWindow()方法就是设置全屏幕用的。                                                                          
                                                                                                                       
    由于GraphicsEnvironment的构造器是受保护的(protected),我们不能直接构                                               
造一个                                                                                                                 
GraphicsEnvironment对象来获得GraphicsDevice对象。幸好它提供了                                                          
getLocalGraphicsEnvironment()方法,用来获得一个GraphicsEnvironment实例:                                               
                                                                                                                       
        GraphicsEnvironment ge = GraphicsEnvironment.                                                                  
getLocalGraphicsEnvironment();                                                                                         
    有了GraphicsEnvironment可以调用getDefaultScreenDevice方法获得当前的                                                
屏幕设备了:                                                                                                           
        GraphicsDevice gd = ge.getDefaultScreenDevice();                                                               
                                                                                                                       
自己动手体验一下                                                                                                       
    有了上面的简介,写一个实例来体验一下吧:                                                                           
                                                                                                                       
import java.awt.*;                                                                                                     
import java.awt.event.*;                                                                                               
import javax.swing.*;                                                                                                  
                                                                                                                       
public class FullScreenTest                                                                                            
{                                                                                                                      
    public static void main(String[] args)                                                                             
    {                                                                                                                  
                                                                                                                       
           GraphicsEnvironment ge =                                                                                    
                GraphicsEnvironment.getLocalGraphicsEnvironment();                                                     
           GraphicsDevice gd = ge.getDefaultScreenDevice();                                                            
           FullScreenWindow myWindow = new FullScreenWindow();                                                         
           if ( gd.isFullScreenSupported() )                                                                           
                gd.setFullScreenWindow(myWindow);                                                                      
           else                                                                                                        
                System.out.println("Unsupported full screen.");                                                        
                                                                                                                       
    }                                                                                                                  
}                                                                                                                      

class FullScreenWindow extends JWindow                                                                                 
{                                                                                                                      
    public FullScreenWindow()                                                                                          
    {                                                                                                                  
        this.addMouseListener(new MouseAdapter()                                                                       
        {                                                                                                              
            public void mousePressed(MouseEvent evt)                                                                   
            {                                                                                                          
                quit();                                                                                                
            }                                                                                                          
            });                                                                                                        
    }                                                                                                                  
                                                                                                                       
    public void quit()                                                                                                 
    {                                                                                                                  
        this.dispose();                                                                                                
    }                                                                                                                  
                                                                                                                       
    public void paint(Graphics g)                                                                                      
    {                                                                                                                  
        g.setFont(new Font("Arial",Font.BOLD,30));                                                                     
        g.setColor(Color.RED);                                                                                         
        g.drawString("这是全屏幕模式",100,100);                                                                        
                                                                                                                       
    }                                                                                                                  
                                                                                                                       
}                                                                                                                      
                                                                                                                       
                                                                                                                       
                                                                                                                       
                                                                                                                       
你也可以进入Turbo Chen的Java技术专栏, 看更多的Java的秘密系列文章                                                       
                                                                                                                       
                                                                                                                       
--                                                                                                                     
                                                                                                                       
※ 来源:·日月光华 bbs.fudan.edu.cn·[FROM:the city of Vampire]                                                        

0 0

相关博文

我的热门文章

img
取 消
img