-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenManager.java
More file actions
132 lines (120 loc) · 3.26 KB
/
ScreenManager.java
File metadata and controls
132 lines (120 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package start;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.BufferStrategy;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
public class ScreenManager {
private GraphicsDevice vc;
//give vc access to monitor screen
public ScreenManager() {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = e.getDefaultScreenDevice();
}
//get all compatible DM
public DisplayMode[] getCompatibleDisplayModes() {
return vc.getDisplayModes();
}
// compares DM passed in to vc DM and see if match
public DisplayMode findFirstCompatibleMode(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];
}
}
}
return null;
}
//get current DM
public DisplayMode getCurrentDisplayMode() {
return vc.getDisplayMode();
}
//checl if two modes match each other
public boolean displayModesMatch(DisplayMode m1, DisplayMode m2) {
if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()) {
return false;
}
if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()) {
return false;
}
if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()) {
return false;
}
return true;
}
//make frame full screen
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) {
f.createBufferStrategy(2);
}
}
}
// we will set Graphics object = to this
public Graphics2D getGraphics() {
Window w = vc.getFullScreenWindow();
if(w != null) {
BufferStrategy s = w.getBufferStrategy();
return (Graphics2D)s.getDrawGraphics();
}else {
return null;
}
}
// updates display
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();
}
//get width
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;
}
}
//get out of fullscreen
public void restoreScreen() {
Window w = vc.getFullScreenWindow();
if(w != null) {
w.dispose();
}
vc.setFullScreenWindow(null);
}
// create image compatible with monitor
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;
}
}