Guest User

Untitled

a guest
Jun 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1.  
  2. import java.awt.DisplayMode;
  3. import java.awt.Graphics2D;
  4. import java.awt.GraphicsConfiguration;
  5. import java.awt.GraphicsDevice;
  6. import java.awt.GraphicsEnvironment;
  7. import java.awt.Window;
  8. import java.awt.image.BufferStrategy;
  9. import java.awt.image.BufferedImage;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13. public class ScreenManager {
  14.  
  15.     //video card variable
  16.     private GraphicsDevice vc;
  17.     //give vc access to monitor
  18.     public ScreenManager(){
  19.         GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
  20.         vc = e.getDefaultScreenDevice();
  21.     }
  22.     //get all compatible displayModes(width,height, bit depth)
  23.     public DisplayMode[] getCompatiableDisplayModes(){
  24.         return vc.getDisplayModes();
  25.     }
  26.    
  27.     //compares DMs passed into vc and see if they match
  28.     public DisplayMode findFirstCompatiableMode(DisplayMode modes[]){
  29.         DisplayMode goodModes[] = vc.getDisplayModes();
  30.         for(int x=0;x<modes.length;x++){
  31.             for (int y=0;y<goodModes.length;y++){
  32.                 if (displayModesMatch(modes[x], goodModes[y])){
  33.                     return modes[x];//since they match we can return either
  34.                 }
  35.             }
  36.         }
  37.         return null;//should return true above and never this null
  38.     }
  39.    
  40.     //get current dm
  41.     public DisplayMode getCurrentDisplayMode(){
  42.         return vc.getDisplayMode();
  43.     }
  44.    
  45.     //checks if two modes match each other
  46.     public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
  47.         //check resolution
  48.         if(m1.getWidth() != m2.getWidth()|| m1.getHeight() != m2.getWidth()){
  49.             return false;
  50.         }
  51.         //check bi depths
  52.         if (m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
  53.             return false;
  54.         }
  55.         //check refresh rates
  56.         if (m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2. getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
  57.             return false;
  58.         }
  59.         //if they past tests then they match
  60.         return true;
  61.     }
  62.    
  63.     public void setFullScreen(DisplayMode dm){
  64.         JFrame f = new JFrame();
  65.         f.setUndecorated(true);
  66.         f.setIgnoreRepaint(true);
  67.         f.setResizable(false);
  68.         vc.setFullScreenWindow(f);
  69.        
  70.         if(dm != null && vc.isDisplayChangeSupported()){
  71.             try{
  72.                 vc.setDisplayMode(dm);
  73.             }catch(Exception ex){
  74.                 System.out.println("couldn't set display mode");
  75.             }
  76.         }
  77.         f.createBufferStrategy(2);
  78.     }
  79.    
  80.     //we will set graphics object equal to this return
  81.     public Graphics2D getGraphics(){
  82.         //stores current full screen in w
  83.         Window w = vc.getFullScreenWindow();
  84.         if(w != null){
  85.         BufferStrategy s = w.getBufferStrategy();
  86.         return (Graphics2D) s.getDrawGraphics();
  87.         }else{
  88.             return null;
  89.         }
  90.     }
  91.    
  92.     //updates dispay
  93.     public void update(){
  94.         Window w = vc.getFullScreenWindow();
  95.         if(w != null){
  96.             BufferStrategy s = w.getBufferStrategy();
  97.             if(!s.contentsLost()){
  98.                 s.show();
  99.             }
  100.         }
  101.     }
  102.     //returns full screen window
  103.     public Window getFullScreenWindow(){
  104.         return vc.getFullScreenWindow();
  105.     }
  106.    
  107.     public int getWidth(){
  108.         Window w = vc.getFullScreenWindow();
  109.         if(w != null){
  110.             return w.getWidth();
  111.         }else{
  112.             return 0;
  113.         }
  114.     }
  115.    
  116.     public int getHeight(){
  117.         Window w = vc.getFullScreenWindow();
  118.         if(w != null){
  119.             return w.getHeight();
  120.         }else{
  121.             return 0;
  122.         }
  123.     }
  124.    
  125.     //exit full screen
  126.     public void restoreScreen(){
  127.         Window w = vc.getFullScreenWindow();
  128.         if (w != null){
  129.             w.dispose();
  130.         }
  131.         vc.setFullScreenWindow(null);
  132.     }
  133.    
  134.     //create image compatible with monitor. t is transparency
  135.     public BufferedImage createCompatibleImage(int w, int h, int t){
  136.         Window win = vc.getFullScreenWindow();
  137.         if(win != null){
  138.             GraphicsConfiguration gc = win.getGraphicsConfiguration();
  139.             return gc.createCompatibleImage(w, h, t);
  140.         }
  141.         return null;
  142.     }
  143. }
Add Comment
Please, Sign In to add comment