Java 怎么返回基础系统是否支持给定的半透明级别

2019年8月21日22:01:15 评论 19

1.简介

Java 怎么返回基础系统是否支持给定的半透明级别?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

 
import java.awt.*;
import javax.swing.JRootPane;
import sun.awt.AWTAccessor;
import sun.awt.SunToolkit;

public class Main{
    
    public static boolean isTranslucencySupported(
            Translucency translucencyKind) {
        switch (translucencyKind) {
        case PERPIXEL_TRANSPARENT:
            return isWindowShapingSupported();
        case TRANSLUCENT:
            return isWindowOpacitySupported();
        case PERPIXEL_TRANSLUCENT:
            return isWindowTranslucencySupported();
        }
        return false;
    }
    
    public static boolean isWindowShapingSupported() {
        Toolkit curToolkit = Toolkit.getDefaultToolkit();
        if (!(curToolkit instanceof SunToolkit)) {
            return false;
        }
        return ((SunToolkit) curToolkit).isWindowShapingSupported();
    }
    
    private static boolean isWindowOpacitySupported() {
        Toolkit curToolkit = Toolkit.getDefaultToolkit();
        if (!(curToolkit instanceof SunToolkit)) {
            return false;
        }
        return ((SunToolkit) curToolkit).isWindowOpacitySupported();
    }
    private static boolean isWindowTranslucencySupported() {
        

        Toolkit curToolkit = Toolkit.getDefaultToolkit();
        if (!(curToolkit instanceof SunToolkit)) {
            return false;
        }

        if (!((SunToolkit) curToolkit).isWindowTranslucencySupported()) {
            return false;
        }

        GraphicsEnvironment env = GraphicsEnvironment
                .getLocalGraphicsEnvironment();

        
        
        
        if (isTranslucencyCapable(env.getDefaultScreenDevice()
                .getDefaultConfiguration())) {
            return true;
        }

        
        GraphicsDevice[] devices = env.getScreenDevices();

        for (int i = 0; i < devices.length; i++) {
            GraphicsConfiguration[] configs = devices[i]
                    .getConfigurations();
            for (int j = 0; j < configs.length; j++) {
                if (isTranslucencyCapable(configs[j])) {
                    return true;
                }
            }
        }

        return false;
    }
    
    public static boolean isTranslucencyCapable(GraphicsConfiguration gc) {
        if (gc == null) {
            throw new NullPointerException(
                    "The gc argument should not be null");
        }
        
        Toolkit curToolkit = Toolkit.getDefaultToolkit();
        if (!(curToolkit instanceof SunToolkit)) {
            return false;
        }
        return ((SunToolkit) curToolkit).isTranslucencyCapable(gc);
    }
}

 

3.总结

示例代码演示了 Java 怎么返回基础系统是否支持给定的半透明级别,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: