1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.Shape;
import java.awt.Window;
import java.lang.reflect.Method;
/**
*
* @author user
*/
public class AWTUtilities {
private static Class AWTUTILITIES_DELEGATED_CLASS = null;
private static Class TRANSLUCENCY_DELEGATED_CLASS = null;
private static boolean SUPPORTS_AWTUTILITIES = false;
private static boolean SUPPORTS_JNA = false;
static {
String version = System.getProperty("java.version");
String[] tokens = version.split("\\.");
int major = Integer.parseInt(tokens[0]);
int minor = Integer.parseInt(tokens[1]);
String awtUtilitiesClassname = null;
if (major == 1) {
if (minor == 6) {
awtUtilitiesClassname = "com.sun.awt.AWTUtilities";
} else if (minor > 6) {
}
} else {
}
if (awtUtilitiesClassname != null) {
String translucencyUtilitiesClassname = awtUtilitiesClassname + "$Translucency";
try {
AWTUTILITIES_DELEGATED_CLASS = Class.forName(awtUtilitiesClassname);
TRANSLUCENCY_DELEGATED_CLASS = Class.forName(translucencyUtilitiesClassname);
} // Silently consume exception.
catch (Exception e) {
}
}
SUPPORTS_AWTUTILITIES = (AWTUTILITIES_DELEGATED_CLASS != null);
}
public enum Translucency {
PERPIXEL_TRANSPARENT, TRANSLUCENT, PERPIXEL_TRANSLUCENT;
public Object toAWT() {
Object result = null;
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = TRANSLUCENCY_DELEGATED_CLASS.getMethod("valueOf", String.class);
result = method.invoke(null, this.toString());
} // Silently consume exception.
catch (Exception e) {
}
}
return result;
}
}
public static boolean isTranslucencySupported(Translucency translucency) {
boolean result = false;
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("isTranslucencySupported", TRANSLUCENCY_DELEGATED_CLASS);
result = (Boolean) method.invoke(null, translucency.toAWT());
} // Silently consume exception.
catch (Exception e) {
}
}
return result;
}
public static void setWindowOpacity(Window window, float opacity) {
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("setWindowOpacity", Window.class, float.class);
method.invoke(null, window, opacity);
} // Silently consume exception.
catch (Exception e) {
}
}
}
public static float getWindowOpacity(Window window) {
float result = 1f;
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("getWindowOpacity", Window.class);
result = (Float) method.invoke(null, window);
} // Silently consume exception.
catch (Exception e) {
}
}
return result;
}
public static Shape getWindowShape(Window window) {
Shape result = null;
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("getWindowShape", Window.class);
result = (Shape) method.invoke(null, window);
} // Silently consume exception.
catch (Exception e) {
}
}
return result;
}
public static void setWindowShape(Window window, Shape shape) {
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("setWindowShape", Window.class, Shape.class);
method.invoke(null, window, shape);
} // Silently consume exception.
catch (Exception e) {
}
}
}
public static void setWindowOpaque(Window window, boolean opaque) {
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, window, opaque);
} // Silently consume exception.
catch (Exception e) {
}
}
}
public static boolean isWindowOpaque(Window window) {
boolean result = false;
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("isWindowOpaque", Window.class);
result = (Boolean) method.invoke(null, window);
} // Silently consume exception.
catch (Exception e) {
}
}
return result;
}
public static boolean isTranslucencyCapable(GraphicsConfiguration gc) {
boolean result = false;
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("isTranslucencyCapable", GraphicsConfiguration.class);
result = (Boolean) method.invoke(null, gc);
} // Silently consume exception.
catch (Exception e) {
}
}
return result;
}
public static void setComponentMixingCutoutShape(Component component, Shape shape) {
if (SUPPORTS_AWTUTILITIES) {
try {
Method method = AWTUTILITIES_DELEGATED_CLASS.getMethod("setComponentMixingCutoutShape", Component.class, Shape.class);
method.invoke(null, component, shape);
} // Silently consume exception.
catch (Exception e) {
}
}
}
} |
Partager