?? fullscreenframe.java
字號:
package jp.co.ntl.swing.ext;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import jp.co.ntl.ext.GlobalContext;
import jp.co.ntl.preference.PreferenceInfo;
public abstract class FullScreenFrame extends JFrame {
private static final int bufferSize = 2;
private GraphicsDevice device;
private BufferStrategy strategy; //僆僼僗僋儕乕儞僀儊乕僕
private Graphics gbb; //僶僢僋僶僢僼傽傊偺昤夋梡
private DisplayMode currentMode; //屻偱尦偵栠偡
private Timer timer;
private PreferenceInfo prefInfo;
/// private boolean exitThread = false;
/// private RepaintThread repaintThread = null;
public FullScreenFrame(GraphicsDevice device, DisplayMode[] defaultMode) {
super(device.getDefaultConfiguration());
this.device = device;
currentMode = device.getDisplayMode();
prefInfo = new PreferenceInfo();
try {
prefInfo.readXml();
} catch (ParserConfigurationException e) {
JOptionPane.showMessageDialog(null, e.toString());
return;
} catch (SAXException e) {
JOptionPane.showMessageDialog(null, e.toString());
return;
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, e.toString());
return;
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.toString());
return;
}
initFullScreen(defaultMode);
if (isFullScreenSupported() && prefInfo.getClientKind() != PreferenceInfo.CLIENT_MAC) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
repaintFullScreen();
}
}, 1000);
/* repaintThread = new RepaintThread();
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
repaintThread.start();
}
}, 2000);*/
}
}
/* private class RepaintThread extends Thread {
public void run() {
while (!exitThread) {
repaintFullScreen();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
}*/
public abstract void paintFullScreen(Graphics g);
public void repaintFullScreen() {
if (!isFullScreenSupported() || prefInfo.getClientKind() == PreferenceInfo.CLIENT_MAC) {
Graphics g = getGraphics();
if (g != null) {
this.paintAll(g);
}
} else {
for (int i = 0; i < bufferSize; i++) {
if (!strategy.contentsLost()) {
gbb = strategy.getDrawGraphics();
paintFullScreen(gbb);
strategy.show();
gbb.dispose();
}
}
}
}
public void closeFullScreen() {
if (isFullScreenSupported()) {
changeDisplayMode(new DisplayMode[] { currentMode });
device.setFullScreenWindow(null);
}
if (timer != null) {
timer.cancel();
}
/* if (repaintThread != null) {
exitThread = true;
try {
repaintThread.join();
} catch (InterruptedException e) {
}
}*/
setVisible(false);
}
public boolean isFullScreenSupported() {
if (prefInfo.isDebug() || !device.isFullScreenSupported()) {
return false;
}
return true;
}
/**
* 僼儖僗僋儕乕儞偺弶婜壔
*/
private void initFullScreen(DisplayMode[] defaultMode) {
setUndecorated(true); //僼儗乕儉偺榞傪昞帵偟側(cè)偄 ver1.4
if (isFullScreenSupported()) {
if (prefInfo.getClientKind() != PreferenceInfo.CLIENT_MAC) {
/// setIgnoreRepaint(true); //OS偺嵞昤夋梫媮傪柍帇 ver1.4
}
device.setFullScreenWindow(this); //僼儖僗僋儕乕儞偵偡傞 ver1.4
changeDisplayMode(defaultMode);
if (prefInfo.getClientKind() != PreferenceInfo.CLIENT_MAC) {
createBufferStrategy(bufferSize); //僟僽儖僶僢僼傽儕儞僌
strategy = getBufferStrategy();
}
}
requestFocus();
}
/**
* 揔愗側(cè)僨傿僗僾儗僀儌乕僪偵偡傞
*/
private void changeDisplayMode(DisplayMode[] defaultMode){
//曄峏偵懳墳偟偰偄傞偲偒
if (device.isDisplayChangeSupported()) {
DisplayMode[] modes = device.getDisplayModes();
for (int i = 0; i < defaultMode.length; i++) {
for (int j = 0; j < modes.length; j++) {
if (equalsDisplayMode(defaultMode[i], modes[j])) {
device.setDisplayMode(modes[j]);
return;
}
}
}
}
}
/**
* 僨傿僗僾儗僀儌乕僪偺斾妑
*/
private boolean equalsDisplayMode(DisplayMode mode1,DisplayMode mode2){
return ( (mode1.getWidth() == mode2.getWidth())
&& (mode1.getHeight() == mode2.getHeight())
&& (mode1.getBitDepth() == mode2.getBitDepth()));
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -