Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.DisplayMode;
- import java.awt.Graphics2D;
- import java.awt.GraphicsConfiguration;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.Window;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- public class ScreenManager {
- //video card variable
- private GraphicsDevice vc;
- //give vc access to monitor
- public ScreenManager(){
- GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
- vc = e.getDefaultScreenDevice();
- }
- //get all compatible displayModes(width,height, bit depth)
- public DisplayMode[] getCompatiableDisplayModes(){
- return vc.getDisplayModes();
- }
- //compares DMs passed into vc and see if they match
- public DisplayMode findFirstCompatiableMode(DisplayMode modes[]){
- DisplayMode goodModes[] = vc.getDisplayModes();
- for(int x=0;x<modes.length;x++){
- for (int y=0;y<goodModes.length;y++){
- if (displayModesMatch(modes[x], goodModes[y])){
- return modes[x];//since they match we can return either
- }
- }
- }
- return null;//should return true above and never this null
- }
- //get current dm
- public DisplayMode getCurrentDisplayMode(){
- return vc.getDisplayMode();
- }
- //checks if two modes match each other
- public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
- //check resolution
- if(m1.getWidth() != m2.getWidth()|| m1.getHeight() != m2.getWidth()){
- return false;
- }
- //check bi depths
- if (m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
- return false;
- }
- //check refresh rates
- if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2. getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
- return false;
- }
- //if they past tests then they match
- return true;
- }
- public void setFullScreen(DisplayMode dm){
- JFrame f = new JFrame();
- f.setUndecorated(true);
- f.setIgnoreRepaint(true);
- f.setResizable(false);
- vc.setFullScreenWindow(f);
- if(dm != null && vc.isDisplayChangeSupported()){
- try{
- vc.setDisplayMode(dm);
- }catch(Exception ex){
- System.out.println("couldn't set display mode");
- }
- }
- f.createBufferStrategy(2);
- }
- //we will set graphics object equal to this return
- public Graphics2D getGraphics(){
- //stores current full screen in w
- Window w = vc.getFullScreenWindow();
- if(w != null){
- BufferStrategy s = w.getBufferStrategy();
- return (Graphics2D) s.getDrawGraphics();
- }else{
- return null;
- }
- }
- //updates dispay
- public void update(){
- Window w = vc.getFullScreenWindow();
- if(w != null){
- BufferStrategy s = w.getBufferStrategy();
- if(!s.contentsLost()){
- s.show();
- }
- }
- }
- //returns full screen window
- public Window getFullScreenWindow(){
- return vc.getFullScreenWindow();
- }
- public int getWidth(){
- Window w = vc.getFullScreenWindow();
- if(w != null){
- return w.getWidth();
- }else{
- return 0;
- }
- }
- public int getHeight(){
- Window w = vc.getFullScreenWindow();
- if(w != null){
- return w.getHeight();
- }else{
- return 0;
- }
- }
- //exit full screen
- public void restoreScreen(){
- Window w = vc.getFullScreenWindow();
- if (w != null){
- w.dispose();
- }
- vc.setFullScreenWindow(null);
- }
- //create image compatible with monitor. t is transparency
- public BufferedImage createCompatibleImage(int w, int h, int t){
- Window win = vc.getFullScreenWindow();
- if(win != null){
- GraphicsConfiguration gc = win.getGraphicsConfiguration();
- return gc.createCompatibleImage(w, h, t);
- }
- return null;
- }
- }
Add Comment
Please, Sign In to add comment