Advertisement
ElectricX

Untitled

May 17th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. package GameTest1;
  2.  
  3. import java.awt.DisplayMode;
  4. import java.awt.Graphics2D;
  5. import java.awt.GraphicsConfiguration;
  6. import java.awt.GraphicsDevice;
  7. import java.awt.GraphicsEnvironment;
  8. import java.awt.Window;
  9. import java.awt.image.BufferStrategy;
  10. import java.awt.image.BufferedImage;
  11.  
  12. import javax.swing.JFrame;
  13.  
  14. public class ScreenManager {
  15. private GraphicsDevice vc;
  16. //gives vc access to screen
  17. public ScreenManager(){
  18. GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
  19. vc = e.getDefaultScreenDevice();
  20. }
  21. //get all the compatible display modes
  22. public DisplayMode[] getCompatibleDisplayModes(){
  23. return vc.getDisplayModes();
  24. }
  25. //comapres dm stuff
  26. public DisplayMode findFirstCompatibleMode(DisplayMode modes[]){
  27. DisplayMode goodmodes[] = vc.getDisplayModes();
  28. for(int x = 0; x < modes.length; x++){
  29. for(int y = 0; y < goodmodes.length; y++){
  30. if(displayModesMatch(modes[x], goodmodes[y])){
  31. return modes[x];
  32. }
  33. }
  34. }
  35. return null;
  36. }
  37. //get current display mode
  38. public DisplayMode getCurrentDisplayMode(){
  39. return vc.getDisplayMode();
  40. }
  41. //check if two modes match eachother
  42. public boolean displayModesMatch(DisplayMode m1, DisplayMode m2){
  43. if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
  44. return false;
  45. }
  46. if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
  47. return false;
  48. }
  49. if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
  50. return false;
  51. }
  52. return true;
  53. }
  54. //make frame full screen
  55. public void setFullScreen(DisplayMode dm){
  56. JFrame f = new JFrame();
  57. f.setUndecorated(true);
  58. f.setIgnoreRepaint(true);
  59. f.setResizable(false);
  60. vc.setFullScreenWindow(f);
  61.  
  62. if(dm != null && vc.isDisplayChangeSupported()){
  63. try{
  64. vc.setDisplayMode(dm);
  65. }catch(Exception ex){}
  66. }
  67. f.createBufferStrategy(2);
  68.  
  69. }
  70. //setting graphics object to this
  71. public Graphics2D getGraphics(){
  72. Window w = vc.getFullScreenWindow();
  73. if(w != null){
  74. BufferStrategy s = w.getBufferStrategy();
  75. return (Graphics2D) s.getDrawGraphics();
  76.  
  77. }else{
  78. return null;
  79. }
  80. }
  81. //updates display
  82. public void update(){
  83. Window w = vc.getFullScreenWindow();
  84. if(w != null){
  85. BufferStrategy s = w.getBufferStrategy();
  86. if(!s.contentsLost()){
  87. s.show();
  88. }
  89. }
  90.  
  91. }
  92. //returns full screen window
  93. public Window getFullScreenWindow(){
  94. return vc.getFullScreenWindow();
  95. }
  96. //gets width of window
  97. public int getWidth(){
  98. Window w = vc.getFullScreenWindow();
  99. if(w != null){
  100. return w.getWidth();
  101. }else{
  102. return 0;
  103. }
  104. }
  105. //gets height of window
  106. public int getHeight(){
  107. Window w = vc.getFullScreenWindow();
  108. if(w != null){
  109. return w.getHeight();
  110. }else{
  111. return 0;
  112. }
  113. }
  114. //gets out of full screen
  115. public void restoreScreen(){
  116. Window w = vc.getFullScreenWindow();
  117. if(w!=null){
  118. w.dispose();
  119. }
  120. vc.setFullScreenWindow(null);
  121. }
  122. //create image compatible with monitor
  123. public BufferedImage createCompatibleImage(int w, int h, int t){
  124. Window win = vc.getFullScreenWindow();
  125. if(win != null){
  126. GraphicsConfiguration gc = win.getGraphicsConfiguration();
  127. return gc.createCompatibleImage(w, h, t);
  128. }
  129. return null;
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement