下面列出了java.awt.image.BaseMultiResolutionImage#java.awt.GraphicsEnvironment 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convert the image into a compatible format for local rendering.
* @param img the bufferedImage
* @return the converted image
*/
BufferedImage optimizeImage(BufferedImage img) {
BufferedImage img2 = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration()
.createCompatibleImage(
img.getWidth(),
img.getHeight(),
img.getColorModel().getTransparency()
// img.getColorModel().hasAlpha() ? Transparency.BITMASK
// : Transparency.OPAQUE
);
Graphics2D g = img2.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return img2;
}
/**
* Returns the <code>Desktop</code> instance of the current
* browser context. On some platforms the Desktop API may not be
* supported; use the {@link #isDesktopSupported} method to
* determine if the current desktop is supported.
* @return the Desktop instance of the current browser context
* @throws HeadlessException if {@link
* GraphicsEnvironment#isHeadless()} returns {@code true}
* @throws UnsupportedOperationException if this class is not
* supported on the current platform
* @see #isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static synchronized Desktop getDesktop(){
if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
if (!Desktop.isDesktopSupported()) {
throw new UnsupportedOperationException("Desktop API is not " +
"supported on the current platform");
}
sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
Desktop desktop = (Desktop)context.get(Desktop.class);
if (desktop == null) {
desktop = new Desktop();
context.put(Desktop.class, desktop);
}
return desktop;
}
/**
* Computes the maximum bounds of the current screen device. If this method is called on JDK 1.4, Xinerama-aware
* results are returned. (See Sun-Bug-ID 4463949 for details).
*
* @return the maximum bounds of the current screen.
*/
private static Rectangle getMaximumWindowBounds()
{
try
{
final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
return localGraphicsEnvironment.getMaximumWindowBounds();
}
catch (Exception e)
{
// ignore ... will fail if this is not a JDK 1.4 ..
}
final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
return new Rectangle(0, 0, s.width, s.height);
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicDevice = ge.getDefaultScreenDevice();
if (!graphicDevice.isDisplayChangeSupported()) {
System.err.println("Display mode change is not supported on this host. Test is considered passed.");
return;
}
DisplayMode defaultDisplayMode = graphicDevice.getDisplayMode();
checkDisplayMode(defaultDisplayMode);
graphicDevice.setDisplayMode(defaultDisplayMode);
DisplayMode[] displayModes = graphicDevice.getDisplayModes();
boolean isDefaultDisplayModeIncluded = false;
for (DisplayMode displayMode : displayModes) {
checkDisplayMode(displayMode);
graphicDevice.setDisplayMode(displayMode);
if (defaultDisplayMode.equals(displayMode)) {
isDefaultDisplayModeIncluded = true;
}
}
if (!isDefaultDisplayModeIncluded) {
throw new RuntimeException("Default display mode is not included");
}
}
private void runTest() {
GraphicsConfiguration gc = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().
getDefaultConfiguration();
if (gc.getColorModel().getPixelSize() < 16) {
System.out.println("8-bit desktop depth found, test passed");
return;
}
VolatileImage vi = gc.createCompatibleVolatileImage(R_WIDTH, R_HEIGHT);
BufferedImage bi = null;
do {
vi.validate(gc);
Graphics2D g = vi.createGraphics();
render(g, vi.getWidth(), vi.getHeight());
bi = vi.getSnapshot();
} while (vi.contentsLost());
checkBI(bi);
System.out.println("Test PASSED.");
}
/**
* Computes the maximum bounds of the current screen device. If this method is called on JDK 1.4, Xinerama-aware
* results are returned. (See Sun-Bug-ID 4463949 for details).
*
* @return the maximum bounds of the current screen.
* @deprecated this method is not useful in multi-screen environments.
*/
public static Rectangle getMaximumWindowBounds ()
{
final GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
try
{
final Method method = GraphicsEnvironment.class.getMethod("getMaximumWindowBounds", (Class[]) null);
return (Rectangle) method.invoke(localGraphicsEnvironment, (Object[]) null);
}
catch(Exception e)
{
// ignore ... will fail if this is not a JDK 1.4 ..
}
final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
return new Rectangle (0, 0, s.width, s.height);
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicDevice = ge.getDefaultScreenDevice();
if (!graphicDevice.isDisplayChangeSupported()) {
System.err.println("Display mode change is not supported on this host. Test is considered passed.");
return;
}
DisplayMode defaultDisplayMode = graphicDevice.getDisplayMode();
checkDisplayMode(defaultDisplayMode);
graphicDevice.setDisplayMode(defaultDisplayMode);
DisplayMode[] displayModes = graphicDevice.getDisplayModes();
boolean isDefaultDisplayModeIncluded = false;
for (DisplayMode displayMode : displayModes) {
checkDisplayMode(displayMode);
graphicDevice.setDisplayMode(displayMode);
if (defaultDisplayMode.equals(displayMode)) {
isDefaultDisplayModeIncluded = true;
}
}
if (!isDefaultDisplayModeIncluded) {
throw new RuntimeException("Default display mode is not included");
}
}
public static void main(String[] args) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicDevice = ge.getDefaultScreenDevice();
if (!graphicDevice.isDisplayChangeSupported()) {
System.err.println("Display mode change is not supported on this host. Test is considered passed.");
return;
}
DisplayMode defaultDisplayMode = graphicDevice.getDisplayMode();
checkDisplayMode(defaultDisplayMode);
graphicDevice.setDisplayMode(defaultDisplayMode);
DisplayMode[] displayModes = graphicDevice.getDisplayModes();
boolean isDefaultDisplayModeIncluded = false;
for (DisplayMode displayMode : displayModes) {
checkDisplayMode(displayMode);
graphicDevice.setDisplayMode(displayMode);
if (defaultDisplayMode.equals(displayMode)) {
isDefaultDisplayModeIncluded = true;
}
}
if (!isDefaultDisplayModeIncluded) {
throw new RuntimeException("Default display mode is not included");
}
}
/**
* Center a frame on the main display
*
* @param frame
* The frame to center
*/
public static void centerFrameOnMainDisplay(final JFrame frame) {
final GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
final GraphicsDevice[] screens = ge.getScreenDevices();
if (screens.length < 1) {
return; // Silently fail.
}
final Rectangle screenBounds = screens[0].getDefaultConfiguration()
.getBounds();
final int x = (int) ((screenBounds.getWidth() - frame.getWidth()) / 2);
final int y = (int) ((screenBounds.getHeight() - frame.getHeight()) / 2);
frame.setLocation(x, y);
}
public static void main(String[] args) throws InterruptedException {
if (Beans.isGuiAvailable() == GraphicsEnvironment.isHeadless()) {
throw new Error("unexpected GuiAvailable property");
}
Beans.setGuiAvailable(!Beans.isGuiAvailable());
ThreadGroup group = new ThreadGroup("$$$");
Thread thread = new Thread(group, new TestGuiAvailable());
thread.start();
thread.join();
}
protected String[] getFontFamilies() {
if (fontFamilyNames == null) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
fontFamilyNames = env.getAvailableFontFamilyNames();
}
return fontFamilyNames;
}
public static CGLGraphicsConfig getGC(CPlatformView pView) {
if (pView != null) {
return (CGLGraphicsConfig)pView.getGraphicsConfiguration();
} else {
// REMIND: this should rarely (never?) happen, but what if
// default config is not CGL?
GraphicsEnvironment env = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
return (CGLGraphicsConfig) gd.getDefaultConfiguration();
}
}
static boolean checkGraphicsEnvironment() {
if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) {
System.err.println("Cannot run test in a headless environment");
}
DisplayMode dm =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
int i = dm.getBitDepth();
if (i == dm.BIT_DEPTH_MULTI || i >= 16) {
return true;
}
return false;
}
private void test() {
GraphicsConfiguration gc =
GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice().getDefaultConfiguration();
if (gc.getColorModel().getPixelSize() < 16) {
System.out.println("<16 bit depth detected, test passed");
return;
}
VolatileImage vi =
gc.createCompatibleVolatileImage(250, 4*120, Transparency.OPAQUE);
BufferedImage res;
do {
vi.validate(gc);
Graphics2D g2d = vi.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, vi.getWidth(), vi.getHeight());
render(g2d);
res = vi.getSnapshot();
} while (vi.contentsLost());
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
if (res.getRGB(x, y) == Color.black.getRGB()) {
System.err.printf("Test FAILED: found black at %d,%d\n",
x, y);
try {
String fileName = "AccelPaintsTest.png";
ImageIO.write(res, "png", new File(fileName));
System.err.println("Dumped rendering to " + fileName);
} catch (IOException e) {}
throw new RuntimeException("Test FAILED: found black");
}
}
}
}
public static void main(final String[] args)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> {
final GraphicsDevice[] devices =
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getScreenDevices();
final Frame frame = new Frame();
frame.setBackground(Color.GREEN);
frame.setUndecorated(true);
frame.setSize(100, 100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
sleep();
for (final GraphicsDevice gd : devices) {
try {
gd.setFullScreenWindow(frame);
if (gd.getFullScreenWindow() != frame) {
throw new RuntimeException("Wrong window");
}
if (frame.getGraphicsConfiguration().getDevice() != gd) {
throw new RuntimeException("Wrong new GraphicsDevice");
}
} finally {
// cleaning up
gd.setFullScreenWindow(null);
}
}
frame.dispose();
});
}
/**
* Starts a recording of the screen
*
* @param testFileName
* The name of the video file to create
* @throws Exception
*/
private void startRecording(String testFileName) throws Exception {
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration();
this.screenRecorder = new SpecializedScreenRecorder(gc, testFileName,
config);
this.screenRecorder.start();
}
public static void main(String[] args) throws InterruptedException {
if (Beans.isGuiAvailable() == GraphicsEnvironment.isHeadless()) {
throw new Error("unexpected GuiAvailable property");
}
Beans.setGuiAvailable(!Beans.isGuiAvailable());
ThreadGroup group = new ThreadGroup("$$$");
Thread thread = new Thread(group, new TestGuiAvailable());
thread.start();
thread.join();
}
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice graphicsDevice =
graphicsEnvironment.getDefaultScreenDevice();
final Dimension screenSize = toolkit.getScreenSize();
final Insets screenInsets = toolkit.getScreenInsets(
graphicsDevice.getDefaultConfiguration());
final Rectangle availableScreenBounds = new Rectangle(screenSize);
availableScreenBounds.x += screenInsets.left;
availableScreenBounds.y += screenInsets.top;
availableScreenBounds.width -= (screenInsets.left + screenInsets.right);
availableScreenBounds.height -= (screenInsets.top + screenInsets.bottom);
frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
availableScreenBounds.width, availableScreenBounds.height);
frame.setVisible(true);
Rectangle frameBounds = frame.getBounds();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
((SunToolkit) toolkit).realSync();
Rectangle maximizedFrameBounds = frame.getBounds();
if (maximizedFrameBounds.width < frameBounds.width
|| maximizedFrameBounds.height < frameBounds.height) {
throw new RuntimeException("Maximized frame is smaller than non maximized");
}
}
public static WGLGraphicsConfig getGC(WComponentPeer peer) {
if (peer != null) {
return (WGLGraphicsConfig)peer.getGraphicsConfiguration();
} else {
// REMIND: this should rarely (never?) happen, but what if
// default config is not WGL?
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
return (WGLGraphicsConfig)gd.getDefaultConfiguration();
}
}
public void testActiveEditorDropAnnotated() throws IOException {
clearWorkDir();
assertTrue("Headless run", GraphicsEnvironment.isHeadless());
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "test.B",
"import org.netbeans.spi.palette.PaletteItemRegistration;\n"
+ "import org.openide.text.ActiveEditorDrop;\n;import javax.swing.text.JTextComponent;"
+ VALID_CLASS_ANNOTATION
+ "public class B implements ActiveEditorDrop {\n"
+ " public boolean handleTransfer(JTextComponent targetComponent) {return true; }"
+ "}\n");
ByteArrayOutputStream os = new ByteArrayOutputStream();
boolean r = AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, os);
assertTrue("Compilation has to be successfull:\n" + os, r);
}
public static Vector<String> getSystemFonts() {
GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] tempFonts = gEnv.getAllFonts();
char dot = '.';
int dotIndex = 0;
char[] fontNameChars = null;
String fontName = null;
Vector<String> fontNames = new Vector<String>();
for (int i = 0; i < tempFonts.length; i++) {
fontName = tempFonts[i].getFontName();
dotIndex = fontName.indexOf(dot);
if (dotIndex == -1) {
fontNames.add(fontName);
} else {
fontNameChars = fontName.substring(0, dotIndex).toCharArray();
fontNameChars[0] = Character.toUpperCase(fontNameChars[0]);
fontName = new String(fontNameChars);
if (!fontNames.contains(fontName)) {
fontNames.add(fontName);
}
}
}
Collections.sort(fontNames);
return fontNames;
}
private static VolatileImage makeVI(final int type) {
final GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
final GraphicsDevice gd = ge.getDefaultScreenDevice();
final GraphicsConfiguration gc = gd.getDefaultConfiguration();
return gc.createCompatibleVolatileImage(SIZE, SIZE, type);
}
/**
* Get all available fonts (system fonts, weather font and custom fonts)
*
* @return Font list
*/
public static List<Font> getAllFonts() {
List<Font> fontList = new ArrayList<>();
//Weather font
Font weatherFont = getWeatherFont();
if (weatherFont != null) {
fontList.add(weatherFont);
}
//System fonts
GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = gEnv.getAllFonts();
for (Font font : fonts) {
fontList.add(font);
}
//Custom fonts
String fn = GlobalUtil.getAppPath(MapFrame.class);
fn = fn.substring(0, fn.lastIndexOf("/"));
String path = fn + File.separator + "font";
File pathDir = new File(path);
if (pathDir.isDirectory()) {
}
return fontList;
}
public static void main(String args[]) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = ge.getAllFonts();
for (int i=0;i<fonts.length;i++) {
System.out.println("Fam="+fonts[i].getFamily()+
", Full="+ fonts[i].getFontName()+
", ps="+ fonts[i].getPSName()+", "+fonts[i]);
}
}
private static D3DGraphicsConfig getGC(WComponentPeer peer) {
GraphicsConfiguration gc;
if (peer != null) {
gc = peer.getGraphicsConfiguration();
} else {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
gc = gd.getDefaultConfiguration();
}
return (gc instanceof D3DGraphicsConfig) ? (D3DGraphicsConfig)gc : null;
}
@Override
public boolean isDisplayChangeSupported() {
return (isFullScreenSupported()
&& (getFullScreenWindow() != null)
&& !((X11GraphicsEnvironment) GraphicsEnvironment
.getLocalGraphicsEnvironment()).runningXinerama());
}
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
final Toolkit toolkit = Toolkit.getDefaultToolkit();
final GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
final GraphicsDevice graphicsDevice =
graphicsEnvironment.getDefaultScreenDevice();
final Dimension screenSize = toolkit.getScreenSize();
final Insets screenInsets = toolkit.getScreenInsets(
graphicsDevice.getDefaultConfiguration());
final Rectangle availableScreenBounds = new Rectangle(screenSize);
availableScreenBounds.x += screenInsets.left;
availableScreenBounds.y += screenInsets.top;
availableScreenBounds.width -= (screenInsets.left + screenInsets.right);
availableScreenBounds.height -= (screenInsets.top + screenInsets.bottom);
frame.setBounds(availableScreenBounds.x, availableScreenBounds.y,
availableScreenBounds.width, availableScreenBounds.height);
frame.setVisible(true);
Rectangle frameBounds = frame.getBounds();
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
((SunToolkit) toolkit).realSync();
Rectangle maximizedFrameBounds = frame.getBounds();
if (maximizedFrameBounds.width < frameBounds.width
|| maximizedFrameBounds.height < frameBounds.height) {
throw new RuntimeException("Maximized frame is smaller than non maximized");
}
}
public static CGLGraphicsConfig getGC(CPlatformView pView) {
if (pView != null) {
return (CGLGraphicsConfig)pView.getGraphicsConfiguration();
} else {
// REMIND: this should rarely (never?) happen, but what if
// default config is not CGL?
GraphicsEnvironment env = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
return (CGLGraphicsConfig) gd.getDefaultConfiguration();
}
}
private static void test(final BufferedImage bi) throws IOException {
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice()
.getDefaultConfiguration();
VolatileImage vi = gc.createCompatibleVolatileImage(500, 200,
TRANSLUCENT);
BufferedImage gold = gc.createCompatibleImage(500, 200, TRANSLUCENT);
// draw to compatible Image
draw(bi, gold);
// draw to volatile image
int attempt = 0;
BufferedImage snapshot;
while (true) {
if (++attempt > 10) {
throw new RuntimeException("Too many attempts: " + attempt);
}
vi.validate(gc);
if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
continue;
}
draw(bi, vi);
snapshot = vi.getSnapshot();
if (vi.contentsLost()) {
continue;
}
break;
}
// validate images
for (int x = 0; x < gold.getWidth(); ++x) {
for (int y = 0; y < gold.getHeight(); ++y) {
if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
ImageIO.write(gold, "png", new File("gold.png"));
ImageIO.write(snapshot, "png", new File("bi.png"));
throw new RuntimeException("Test failed.");
}
}
}
}
@Test
public void testCompatibleFonts() {
String s = "\u23EF";
Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
System.out.println("Total fonts: \t" + fonts.length);
int count = 0;
for (Font font : fonts) {
if (font.canDisplayUpTo(s) < 0) {
count++;
System.out.println(font.getName());
}
}
System.out.println("Compatible fonts: \t" + count);
}