Java/2D Graphics GUI/Graphic Environment

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

A quick utility to print out graphic device information

 
/*
Java Swing, 2nd Edition
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole
ISBN: 0-596-00408-7
Publisher: O"Reilly 
*/
// GuiScreens.java
//A quick utility to print out graphic device information. Will work on
//systems with multiple monitors.
//
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class GuiScreens {
  public static void main(String[] args) {
    Rectangle virtualBounds = new Rectangle();
    GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    JFrame frame[][] = new JFrame[gs.length][];
    for (int j = 0; j < gs.length; j++) {
      GraphicsDevice gd = gs[j];
      System.out.println("Device " + j + ": " + gd);
      GraphicsConfiguration[] gc = gd.getConfigurations();
      frame[j] = new JFrame[gc.length];
      for (int i = 0; i < gc.length; i++) {
        System.out.println("  Configuration " + i + ": " + gc[i]);
        System.out.println("    Bounds: " + gc[i].getBounds());
        virtualBounds = virtualBounds.union(gc[i].getBounds());
        frame[j][i] = new JFrame("Config: " + i, gc[i]);
        frame[j][i].setBounds(50, 50, 400, 100);
        frame[j][i].setLocation((int) gc[i].getBounds().getX() + 50,
            (int) gc[i].getBounds().getY() + 50);
        frame[j][i].getContentPane().add(
            new JTextArea("Config:\n" + gc[i]));
        frame[j][i].setVisible(true);
      }
      System.out.println("Overall bounds: " + virtualBounds);
    }
  }
}



Create an image that does not support transparency from GraphicsConfiguration

 
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();

    BufferedImage bimage = gc.createCompatibleImage(100, 100, Transparency.OPAQUE);
  }
}



Create an image that supports arbitrary levels of transparency from GraphicsConfiguration

 
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    BufferedImage bimage = gc.createCompatibleImage(100, 100, Transparency.TRANSLUCENT);
  }
}



Create an image that supports transparent pixels from GraphicsConfiguration

 
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    BufferedImage bimage = gc.createCompatibleImage(100, 100, Transparency.BITMASK);
  }
}



Create buffered images that are compatible with the screen

 

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    // Create an image that does not support transparency
    BufferedImage bimage = gc.createCompatibleImage(100, 100, Transparency.OPAQUE);
  }
}



Determine if full-screen mode is supported directly

 
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    if (gs.isFullScreenSupported()) {
      // Full-screen mode is supported
    } else {
      // Full-screen mode will be simulated
    }
  }
}



Enter full screen mode

 
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    Button btn = new Button("OK");
    btn.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        gs.setFullScreenWindow(null);
      }
    });
    Frame frame = new Frame(gs.getDefaultConfiguration());
    Window win = new Window(frame);
    win.add(btn, BorderLayout.CENTER);
    try {
      gs.setFullScreenWindow(win);
      win.validate();
    } finally {
      gs.setFullScreenWindow(null);
    }
  }
}



Get the available font family names

 
 
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String familyNames[] = ge.getAvailableFontFamilyNames();
    for (String familyName : familyNames) {
      System.out.println("Family names: " + familyName);
    }
  }
}



Get the available font names

 
 
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = ge.getAllFonts();
    for (Font font : fonts) {
      String fontName = font.getName();
      String familyName = font.getFamily();
      System.out.println("Font: " + fontName + "; family: " + familyName);
    }
  }
}



Get the GraphicsEnvironment and GraphicsDevice

 
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class ShowConfigurations {
  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
    GraphicsDevice defaultScreen = ge.getDefaultScreenDevice();
    GraphicsConfiguration[] configurations = defaultScreen
        .getConfigurations();
    System.out.println("Default screen device: "
        + defaultScreen.getIDstring());
    for (int i = 0; i < configurations.length; i++) {
      System.out.println("  Configuration " + (i + 1));
      System.out.println("  " + configurations[i].getColorModel());
    }
    System.exit(0);
  }
}



Getting Amount of Free Accelerated Image Memory

 
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.VolatileImage;
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++) {
      VolatileImage im = gs[i].getDefaultConfiguration()
          .createCompatibleVolatileImage(1, 1);
      int bytes = gs[i].getAvailableAcceleratedMemory();
      if (bytes < 0) {
        System.out.println("Amount of memory is unlimited");
      }
      im.flush();
    }
  }
}



Getting Number of Colors

 
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();
    }
  }
}



Getting Refresh Rates

 
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();
    }
  }
}



Getting Screen Sizes

 
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();
    }
  }
}



Getting the Current Screen Refresh Rate and Number of Colors

 
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);
    }
  }
}



Getting the Font Faces for a Font Family

 
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
  public static void main(String[] argv) throws Exception {
    Map<String, List<String>> fontFaceNames = new HashMap<String, List<String>>();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font[] fonts = ge.getAllFonts();
    for (int i = 0; i < fonts.length; i++) {
      String familyName = fonts[i].getFamily();
      String faceName = fonts[i].getName();
      List<String> list = fontFaceNames.get(familyName);
      if (list == null) {
        list = new ArrayList<String>();
        fontFaceNames.put(familyName, list);
      }
      list.add(faceName);
    }
    System.out.println(fontFaceNames);
  }
}



Getting the Number of Screens

 
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
      GraphicsDevice[] gs = ge.getScreenDevices();
      int numScreens = gs.length;
    } catch (HeadlessException e) {
    }
  }
}



If more than one screen is available, gets the size of each screen

 
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 screenWidth = dm.getWidth();
      int screenHeight = dm.getHeight();
    }
  }
}



Leave full-screen mode (Return to normal windowed mode)

 
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
  public static void main(String[] argv) throws Exception {
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     GraphicsDevice gs = ge.getDefaultScreenDevice();
     gs.setFullScreenWindow(null);
  }
}



List all available fonts in the system

 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class AllAvailableFontsComboBox extends JPanel {
  JComboBox fonts;
  public AllAvailableFontsComboBox() {
    add(new JLabel("Fonts"));
    GraphicsEnvironment gEnv = GraphicsEnvironment
        .getLocalGraphicsEnvironment();
    String envfonts[] = gEnv.getAvailableFontFamilyNames();
    Vector vector = new Vector();
    for (int i = 1; i < envfonts.length; i++) {
      vector.addElement(envfonts[i]);
    }
    fonts = new JComboBox(vector);
    add(fonts);
  }
  public static void main(String s[]) {
    JFrame f = new JFrame("FontSelection");
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    AllAvailableFontsComboBox fontSelection = new AllAvailableFontsComboBox();
    f.getContentPane().add(fontSelection, BorderLayout.CENTER);
    f.setSize(new Dimension(350, 250));
    f.setVisible(true);
  }
}



Listing All Available Font Families

 

import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] argv) throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String fontNames[] = ge.getAvailableFontFamilyNames();
    for (int i = 0; i < fontNames.length; i++) {
      System.out.println(fontNames[i]);
    }
  }
}



Retrieve and print the graphic device information

 
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class Main {
  public static void main(String[] args) {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    for (int j = 0; j < gs.length; j++) {
      GraphicsDevice gd = gs[j];
      System.out.println("Device " + j + ": " + gd);
      GraphicsConfiguration[] gc = gd.getConfigurations();
      for (int i = 0; i < gc.length; i++) {
        System.out.println(gc[i]);
        System.out.println(gc[i].getBounds());
      }
    }
  }
}



Setting the Screen Size, Refresh Rate, or Number of Colors

 
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);
      }
    }
  }
}



Show all fonts you have in your system

 
import java.awt.Font;
import java.awt.GraphicsEnvironment;
public class ShowFonts {
  public static void main(String[] args) {
    Font[] fonts;
    fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
    for (int i = 0; i < fonts.length; i++) {
      System.out.print(fonts[i].getFontName() + " : ");
      System.out.print(fonts[i].getFamily() + " : ");
      System.out.print(fonts[i].getName());
      System.out.println();
    }
  }
}