📄 propertiesdialog2.java
字号:
dispose();
System.exit(0);
}
});
buttonPanel.add(ok);
buttonPanel.add(cancel);
centerPanel.add(icon, BorderLayout.NORTH);
centerPanel.add(optionsPanel, BorderLayout.SOUTH);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
this.getContentPane().add(mainPanel);
pack();
center();
showDialog();
}
/**
* <code>verifyAndSaveCurrentSelection</code> first verifies that the
* display mode is valid for this system, and then saves the current
* selection as a properties.cfg file.
*
* @return if the selection is valid
*/
private boolean verifyAndSaveCurrentSelection() {
String display = (String) displayResCombo.getSelectedItem();
int width = Integer.parseInt(display.substring(0, display.indexOf(" x ")));
display = display.substring(display.indexOf(" x ") + 3);
int height = Integer.parseInt(display);
String depthString = (String) colorDepthCombo.getSelectedItem();
int depth = Integer.parseInt(depthString.substring(0, depthString.indexOf(' ')));
String freqString = (String) displayFreqCombo.getSelectedItem();
int freq = Integer.parseInt(freqString.substring(0, freqString.indexOf(' ')));
boolean fullscreen = fullscreenBox.isSelected();
if (!fullscreen) {
//query the current bit depth of the desktop
int curDepth = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDisplayMode().getBitDepth();
if (depth > curDepth) {
showError(this,"Cannot choose a higher bit depth in windowed " +
"mode than your current desktop bit depth");
return false;
}
}
String renderer = (String) rendererCombo.getSelectedItem();
//test valid display mode
DisplaySystem disp = DisplaySystem.getDisplaySystem(renderer);
boolean valid = (disp != null) ? disp.isValidDisplayMode(width, height, depth, freq) : false;
if (valid) {
//use the GameSettings class to save it.
source.setWidth(width);
source.setHeight(height);
source.setDepth(depth);
source.setFrequency(freq);
source.setFullscreen(fullscreen);
source.setRenderer(renderer);
try {
source.save();
} catch (IOException ioe) {
logger.log(Level.WARNING,
"Failed to save setting changes", ioe);
}
} else
showError(this, "Your monitor claims to not support the display mode you've selected.\n" +
"The combination of bit depth and refresh rate is not supported.");
return valid;
}
/**
* <code>setUpChooser</code> retrieves all available display modes and
* places them in a <code>JComboBox</code>. The resolution specified
* by GameSettings is used as the default value.
* @return the combo box of display modes.
*/
private JComboBox setUpResolutionChooser() {
String[] res = getResolutions(modes);
JComboBox resolutionBox = new JComboBox(res);
resolutionBox.setSelectedItem(source.getWidth() + " x " + source.getHeight());
resolutionBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateDisplayChoices();
}
});
return resolutionBox;
}
/**
* <code>setUpRendererChooser</code> sets the list of available renderers.
* Data is obtained from the <code>DisplaySystem</code> class. The renderer
* specified by GameSettings is used as the default value.
* @return the list of renderers.
*/
private JComboBox setUpRendererChooser() {
String modes[] = DisplaySystem.getSystemProviderIdentifiers();
JComboBox nameBox = new JComboBox(modes);
nameBox.setSelectedItem(source.getRenderer());
return nameBox;
}
private void updateDisplayChoices() {
String resolution = (String)displayResCombo.getSelectedItem();
//grab available depths
String[] depths = getDepths(resolution, modes);
colorDepthCombo.setModel(new DefaultComboBoxModel(depths));
colorDepthCombo.setSelectedItem(source.getDepth() + " bpp");
//grab available frequencies
String[] freqs = getFrequencies(resolution, modes);
displayFreqCombo.setModel(new DefaultComboBoxModel(freqs));
displayFreqCombo.setSelectedItem(source.getFrequency() + " Hz");
}
//
//Utility methods
//
/**
* Utility method for converting a String denoting a file
* into a URL.
* @return a URL pointing to the file or null
*/
private static URL getURL(String file) {
URL url = null;
try {
url = new URL("file:" + file);
} catch (MalformedURLException e) {}
return url;
}
private static void showError(java.awt.Component parent, String message) {
JOptionPane.showMessageDialog(
parent,
message,
"Error",
JOptionPane.ERROR_MESSAGE);
}
/**
* Reutrns every unique resolution from an array of <code>DisplayMode</code>s.
*/
private static String[] getResolutions(DisplayMode[] modes) {
ArrayList<String> resolutions = new ArrayList<String>(16);
for (int i = 0; i < modes.length; i++) {
String res = modes[i].getWidth() + " x " + modes[i].getHeight();
if (!resolutions.contains(res))
resolutions.add(res);
}
String[] res = new String[resolutions.size()];
resolutions.toArray(res);
return res;
}
/**
* Returns every possible bit depth for the given resolution.
*/
private static String[] getDepths(String resolution, DisplayMode[] modes) {
ArrayList<String> depths = new ArrayList<String>(4);
for (int i = 0; i < modes.length; i++) {
//Filter out all bit depths lower than 16 - Java incorrectly reports
//them as valid depths though the monitor does not support them
if (modes[i].getBitDepth() < 16) continue;
String res = modes[i].getWidth() + " x " + modes[i].getHeight();
String depth = modes[i].getBitDepth() + " bpp";
if (res.equals(resolution) && !depths.contains(depth))
depths.add(depth);
}
String[] res = new String[depths.size()];
depths.toArray(res);
return res;
}
/**
* Returns every possible refresh rate for the given resolution.
*/
private static String[] getFrequencies(String resolution, DisplayMode[] modes) {
ArrayList<String> freqs = new ArrayList<String>(4);
for (int i = 0; i < modes.length; i++) {
String res = modes[i].getWidth() + " x " + modes[i].getHeight();
String freq = modes[i].getRefreshRate() + " Hz";
if (res.equals(resolution) && !freqs.contains(freq))
freqs.add(freq);
}
String[] res = new String[freqs.size()];
freqs.toArray(res);
return res;
}
/**
* Utility class for sorting <code>DisplayMode</code>s. Sorts by resolution,
* then bit depth, and then finally refresh rate.
*/
private class DisplayModeSorter implements Comparator<DisplayMode> {
/**
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(DisplayMode a, DisplayMode b) {
//Width
if (a.getWidth() != b.getWidth())
return (a.getWidth() > b.getWidth()) ? 1 : -1;
//Height
if (a.getHeight() != b.getHeight())
return (a.getHeight() > b.getHeight()) ? 1 : -1;
//Bit depth
if (a.getBitDepth() != b.getBitDepth())
return (a.getBitDepth() > b.getBitDepth()) ? 1 : -1;
//Refresh rate
if (a.getRefreshRate() != b.getRefreshRate())
return (a.getRefreshRate() > b.getRefreshRate()) ? 1 : -1;
//All fields are equal
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -