Java by API/java.awt/DisplayMode

Материал из Java эксперт
Перейти к: навигация, поиск

DisplayMode: getBitDepth()

  
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    DisplayMode[] dmodes = gs.getDisplayModes();
    for (int i = 0; i < dmodes.length; i++) {
      int bitDepth = dmodes[i].getBitDepth();
    }
  }
}





DisplayMode: getHeight()

  

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    DisplayMode[] dmodes = gs.getDisplayModes();
    for (int i = 0; i < dmodes.length; i++) {
      int screenWidth = dmodes[i].getWidth();
      int screenHeight = dmodes[i].getHeight();
    }
  }
}





DisplayMode: getRefreshRate()

  

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    DisplayMode[] dmodes = gs.getDisplayModes();
    for (int i = 0; i < dmodes.length; i++) {
      int refreshRate = dmodes[i].getRefreshRate();
    }
  }
}





DisplayMode: getWidth()

  

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    DisplayMode[] dmodes = gs.getDisplayModes();
    for (int i = 0; i < dmodes.length; i++) {
      int screenWidth = dmodes[i].getWidth();
      int screenHeight = dmodes[i].getHeight();
    }
  }
}





DisplayMode.REFRESH_RATE_UNKNOWN

  
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    for (int i = 0; i < gs.length; i++) {
      DisplayMode dm = gs[i].getDisplayMode();
       int refreshRate = dm.getRefreshRate();
      if (refreshRate == DisplayMode.REFRESH_RATE_UNKNOWN) {
        System.out.println("Unknown rate"); 
      }
      int bitDepth = dm.getBitDepth();
      int numColors = (int) Math.pow(2, bitDepth);
    }
  }
}





new DisplayMode(int width, int height, int bitDepth, int refreshRate)

  
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    boolean canChg = gs.isDisplayChangeSupported();
    if (canChg) {
      DisplayMode displayMode = gs.getDisplayMode();
      int screenWidth = 640;
      int screenHeight = 480;
      int bitDepth = 8;
      displayMode = new DisplayMode(screenWidth, screenHeight, bitDepth, displayMode
          .getRefreshRate());
      try {
        gs.setDisplayMode(displayMode);
      } catch (Throwable e) {
        gs.setFullScreenWindow(null);
      }
    }
  }
}