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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
| import java.awt.Canvas; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import javax.swing.JFrame;
import java.awt.DisplayMode; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Image; import java.awt.Point; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.image.BufferStrategy; import java.awt.image.MemoryImageSource; import java.awt.image.VolatileImage; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.Component; public class FakeFullscreen implements Runnable { final static boolean FRAME_SYNC = true; final static int FRAME_RATE = 61; final static boolean STRETCH = false; final static boolean HIGH_QUALITY = true; final static boolean FULLSCREEN = true; boolean isRunning; CustomTimer timer; FPSCounter fpsCounter; JFrame frame; VolatileImage offScreen = null; BufferStrategy buffer; Component drawArea; Dimension screenSize; Dimension gameSize; Rectangle gameArea; Graphics2D offScreenGraphics; Graphics2D graphics; public FakeFullscreen() { frame = new JFrame() { public void paint(Graphics g) { } public void update(Graphics g) { } }; timer = new CustomTimer(); fpsCounter = new FPSCounter(timer); final Toolkit toolkit = Toolkit.getDefaultToolkit();
final int[] pixels = new int[16 * 16]; final Image image = toolkit.createImage(new MemoryImageSource(16, 16, pixels, 0, 16)); final Cursor invisibleCursor = toolkit.createCustomCursor(image, new Point(0, 0), "invisibleCursor");
gameSize = new Dimension(640, 480); screenSize = toolkit.getScreenSize(); gameArea = new Rectangle(0, 80, 640, 400);
frame.setAlwaysOnTop(true); frame.setCursor(invisibleCursor); frame.setIgnoreRepaint(true); frame.setLocation(0, 0); frame.setResizable(false);
frame.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent event) { if (event.getKeyCode() == KeyEvent.VK_ESCAPE) { isRunning = false; } } }); frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { isRunning = false; } });
if (FULLSCREEN) { try { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = env.getDefaultScreenDevice();
GraphicsConfiguration gc = device.getDefaultConfiguration(); frame.setUndecorated(true); device.setFullScreenWindow(frame); frame.setSize(screenSize); frame.setVisible(true); drawArea = frame.getContentPane(); frame.toFront(); frame.createBufferStrategy(2); buffer = frame.getBufferStrategy();
offScreen = gc.createCompatibleVolatileImage(gameSize.width, gameSize.height); } catch (Exception e) { e.printStackTrace(); } } else { final Canvas canvas = new Canvas(); canvas.setIgnoreRepaint(true); drawArea = canvas; canvas.setSize(gameSize); if (STRETCH) frame.setResizable(true); frame.add(canvas); frame.pack(); frame.setVisible(true); frame.toFront(); frame.validate(); canvas.createBufferStrategy(2); buffer = canvas.getBufferStrategy(); offScreen = canvas.createVolatileImage(gameSize.width, gameSize.height); }
isRunning = true; new Thread(this).start(); }
public void run() { int ballWidth = 8; int ballHeight = 8; int ballX = gameArea.width / 2 - ballWidth / 2; int ballY = gameArea.y + (gameArea.height / 2 - ballHeight / 2); int ballDx = 1; int ballDy = 1; while (isRunning) { if (!FRAME_SYNC || timer.update(FRAME_RATE)) { fpsCounter.update(); ballX += ballDx; ballY += ballDy;
if (ballX <= gameArea.x) { ballX = gameArea.x; ballDx = -ballDx; } else if (ballX >= gameArea.width - ballWidth) { ballX = gameArea.width - ballWidth; ballDx = -ballDx; }
if (ballY <= gameArea.y) { ballY = gameArea.y; ballDy = -ballDy; } else if (ballY >= gameArea.y + gameArea.height - ballHeight) { ballY = gameArea.y + gameArea.height - ballHeight; ballDy = -ballDy; } try { offScreenGraphics = offScreen.createGraphics(); offScreenGraphics.setColor(Color.BLACK); offScreenGraphics.fillRect(0, 0, screenSize.width, screenSize.height); offScreenGraphics.setColor(Color.WHITE); offScreenGraphics.drawString(String.format("FPS: %s", fpsCounter.getFPS()), 20, 20);
int renderX = (int) (ballX + ballDx); int renderY = (int) (ballY + ballDy);
offScreenGraphics.drawLine(gameArea.x, gameArea.y - ballHeight, gameArea.width, gameArea.y - ballHeight); offScreenGraphics.fillRect(renderX, renderY, ballWidth, ballHeight); graphics = (Graphics2D) buffer.getDrawGraphics(); if (STRETCH) { if (HIGH_QUALITY) { graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); } graphics.drawImage(offScreen, 0, 0, drawArea.getWidth(), drawArea.getHeight(), 0, 0, gameSize.width, gameSize.height, null); } else { graphics.setColor(Color.BLACK); graphics.fillRect(0, 0, drawArea.getWidth(), drawArea.getHeight()); int offXFactor = offScreen.getWidth()/2; int offYFactor = offScreen.getHeight()/2; int midX = drawArea.getWidth()/2; int midY = drawArea.getHeight()/2; graphics.drawImage(offScreen, midX - offXFactor, midY - offYFactor, null); }
buffer.show(); } finally { if (offScreenGraphics != null) { offScreenGraphics.dispose(); }
if (graphics != null) { graphics.dispose(); } } } Thread.yield(); } frame.setVisible(false); System.exit(0); } public static void main(String[] args) { new FakeFullscreen(); } }
class CustomTimer { private long timeThen; public CustomTimer() { timeThen = System.nanoTime(); } public boolean update(int fps) {
long gapTo = 1000000000L / fps + timeThen; long timeNow = System.nanoTime(); if (gapTo > timeNow) return false; timeThen = timeNow;
return true; }
public long getTime() { return System.nanoTime(); } public long getTimerResolution() { return 1000000000L; } } class FPSCounter { private CustomTimer timer; private float FPS = 0, fc = 0;
private long currentTime, elapsedTime, lastTime; public FPSCounter(CustomTimer timer) { this.timer = timer; currentTime = 0; elapsedTime = lastTime = timer.getTime(); } public boolean update() { currentTime = timer.getTime();
fc++; long updateFrequency = timer.getTimerResolution()>>1; if((elapsedTime = currentTime - lastTime) >= updateFrequency){ FPS = (fc/elapsedTime)*updateFrequency*2; fc = 0; lastTime = currentTime; elapsedTime = 0; return true; } return false; } public float getFPS() { return FPS; } } |