📄 menus.java
字号:
items[7] = new JRadioButtonMenuItem(I18n.tr("command.palette.customGradient")); items[selectedItem].setSelected(true); ButtonGroup grp = new ButtonGroup(); for (int i = 0; i < items.length; i++) { grp.add(items[i]); items[i].addActionListener(this); } } void setDefault() { // Selects the default item (item 0) and sets the state of the // MandelbrotDisplay to match; this is used by the Restore All Defaults command. items[0].setSelected(true); selectedItem = 0; owner.getDisplay().setPaletteType(MandelbrotDisplay.PALETTE_SPECTRUM); } String valueAsString() { // Converts the setting of this menu to a string that can be saved // in an XML file. This is used by the currentSettingAsXML() method, // which is used in turn by the Save Params command. if (selectedItem < valueStrings.length) return valueStrings[selectedItem]; else { Color c1 = owner.getDisplay().getGradientPaletteColor1(); Color c2 = owner.getDisplay().getGradientPaletteColor2(); if (c1 == null || c2 == null) return valueStrings[0]; // Should not happen! return "Custom/" + c1.getRed() + "," + c1.getGreen() + "," + c1.getBlue() + "/" + c2.getRed() + "," + c2.getGreen() + "," + c2.getBlue(); } } void setValueFromString(String str) { // Takes a string from an XML file (which originally came from the // previous method when the file was saved) and restores the setting // represented by that string. This is called by the retrieveSettingsFromXML() // method, which is called in turn by the Open Params command for (int i = 0; i < valueStrings.length; i++) { if (valueStrings[i].equalsIgnoreCase(str)) { items[i].setSelected(true); applySelection(); return; } } String[] tokens = explode(str,"/,"); if ( ! tokens[0].equalsIgnoreCase("custom")) throw new IllegalArgumentException(); Color c1 = new Color( Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3]) ); Color c2 = new Color( Integer.parseInt(tokens[4]), Integer.parseInt(tokens[5]), Integer.parseInt(tokens[6]) ); owner.getDisplay().setGradientPalette(c1, c2); items[7].setSelected(true); selectedItem = 7; } public void actionPerformed(ActionEvent evt) { // Responds to a command by calling the applySelection() method. // The applySelection() method has been factored out of the actionPerformed() // method because it is also used in the setValueFromString() method. applySelection(); } private void applySelection() { // Sets the palette selected in the MandelbrotDisplay to match the // currently selected item in the menu. if (items[0].isSelected()) { owner.getDisplay().setPaletteType(MandelbrotDisplay.PALETTE_SPECTRUM); selectedItem = 0; } else if (items[1].isSelected()) { owner.getDisplay().setPaletteType(MandelbrotDisplay.PALETTE_PALE_SPECTRUM); selectedItem = 1; } else if (items[2].isSelected()) { owner.getDisplay().setPaletteType(MandelbrotDisplay.PALETTE_GRAYSCALE); selectedItem = 2; } else if (items[3].isSelected()) { owner.getDisplay().setPaletteType(MandelbrotDisplay.PALETTE_REVERSE_GRAYSCALE); selectedItem = 3; } else if (items[4].isSelected()) { owner.getDisplay().setGradientPalette(Color.BLACK, Color.RED); selectedItem = 4; } else if (items[5].isSelected()) { owner.getDisplay().setGradientPalette(Color.RED, Color.CYAN); selectedItem = 5; } else if (items[6].isSelected()) { owner.getDisplay().setGradientPalette( new Color(255,130,20), new Color(0,0,255)); selectedItem = 6; } else { // The setting is for a custom gradient. NOTE that this case never occurs when // this method is called from the setValueFromString() method; it only occurs // when called from actionPerformed() in response to a user action. The // command is "Custom gradient", and the response is to show two Color // dialog boxes where the user can pick the start and end color for the // gradient. Note that if the user CANCELS, then the state of the // MandelbrotDisplay is not changed, and the menu must be reset to show // the selection that was in place before the user action so that the // menu will properly reflect the state of the display. This is the // main reason why I keep the current selectedItem in an instance variable. Color c1 = Color.BLACK; Color c2 = Color.WHITE; if (owner.getDisplay().getPaletteType() == MandelbrotDisplay.PALETTE_GRADIENT) { // If the display is already using a gradient palette, then the colors // from that gradient will be used as the initially selected colors in // the color chooser dialog boxes. c1 = owner.getDisplay().getGradientPaletteColor1(); c2 = owner.getDisplay().getGradientPaletteColor2(); } c1 = JColorChooser.showDialog(owner, "Select Gradient Start Color", c1); if (c1 == null) { items[selectedItem].setSelected(true); // Restore previous selection in menu. return; // (The menu selection has been changed by the user } // action, but the value of selectedItem has not changed. c2 = JColorChooser.showDialog(owner, "Select Gradient End Color", c2); if (c2 == null) { items[selectedItem].setSelected(true); return; } owner.getDisplay().setGradientPalette(c1, c2); selectedItem = 7; } } } // end nested class PaletteManager /** * Defines the object that manages the PaletteLength menu. Similar in * structure to PaletteManager; see above. */ private class PaletteLengthManager implements ActionListener{ int[] standardLengths = { 25, 50, 100, 250, 500, 1000, 2000, 5000, 10000 }; int selectedItem = 0; JRadioButtonMenuItem[] items; PaletteLengthManager() { items = new JRadioButtonMenuItem[ 2 + standardLengths.length ]; items[0] = new JRadioButtonMenuItem(I18n.tr("command.pallete.lengthTracksMaxIterations")); for (int i = 0; i < standardLengths.length; i++) items[i+1] = new JRadioButtonMenuItem( I18n.tr("command.palette.length", ""+standardLengths[i])); items[items.length-1] = new JRadioButtonMenuItem(I18n.tr("command.palette.customLength")); items[selectedItem].setSelected(true); ButtonGroup grp = new ButtonGroup(); for (int i = 0; i < items.length; i++) { grp.add(items[i]); items[i].addActionListener(this); } } void setDefault() { selectedItem = 0; owner.getDisplay().setPaletteLength(0); items[0].setSelected(true); } String valueAsString() { return "" + owner.getDisplay().getPaletteLength(); } void setValueFromString(String str) { int length = Integer.parseInt(str); if (length < 0 || length > 500000) throw new IllegalArgumentException(); if (length == 0) { selectedItem = 0; owner.getDisplay().setPaletteLength(0); items[0].setSelected(true); return; } for (int i = 0; i < standardLengths.length; i++) { if (length == standardLengths[i]) { selectedItem = i+1; owner.getDisplay().setPaletteLength(standardLengths[i]); items[i+1].setSelected(true); return; } } items[items.length-1].setSelected(true); owner.getDisplay().setPaletteLength(length); } public void actionPerformed(ActionEvent evt) { if (items[0].isSelected()) { owner.getDisplay().setPaletteLength(0); selectedItem = 0; } else if (items[items.length-1].isSelected()) { String lengthStr = JOptionPane.showInputDialog( I18n.tr("command.palette.customLengthQuestion"), owner.getDisplay().getPaletteLength()); if (lengthStr == null || lengthStr.trim().length() == 0) { items[selectedItem].setSelected(true); return; } try { int length = Integer.parseInt(lengthStr); if (length < 0) throw new NumberFormatException(); if (length > 500000) throw new NumberFormatException(); owner.getDisplay().setPaletteLength(length); selectedItem = items.length - 1; } catch (NumberFormatException e) { JOptionPane.showMessageDialog( owner,I18n.tr("command.palette.customLengthError",lengthStr)); items[selectedItem].setSelected(true); return; } } else { for (int i = 0; i < standardLengths.length; i++) { if (items[i+1].isSelected()) { owner.getDisplay().setPaletteLength(standardLengths[i]); selectedItem = i+1; break; } } } } } // end nested class PaletteLengthManager /** * Defines the object that manages the MaxIterations menu. Similar in * structure to PaletteManager; see above. */ private class MaxIterationsManager implements ActionListener{ int[] standardValues = { 50, 100, 250, 500, 1000, 2000, 5000, 20000, 50000, 100000 }; int selectedItem = 0; JRadioButtonMenuItem[] items; MaxIterationsManager() { items = new JRadioButtonMenuItem[ 1 + standardValues.length ]; for (int i = 0; i < standardValues.length; i++) items[i] = new JRadioButtonMenuItem( I18n.tr("command.maxiterations", ""+standardValues[i])); items[items.length-1] = new JRadioButtonMenuItem(I18n.tr("command.maxiterations.custom")); items[selectedItem].setSelected(true); ButtonGroup grp = new ButtonGroup(); for (int i = 0; i < items.length; i++) { grp.add(items[i]); items[i].addActionListener(this); } } void setEnabled(boolean enable) { for (JRadioButtonMenuItem item : items) item.setEnabled(enable); } void setDefault() { selectedItem = 0; owner.getDisplay().setMaxIterations(standardValues[0]); items[0].setSelected(true); } String valueAsString() { return "" + owner.getDisplay().getMaxIterations(); } void setValueFromString(String str) { int length = Integer.parseInt(str); if (length < 0 || length > 500000) throw new IllegalArgumentException(); for (int i = 0; i < standardValues.length; i++) { if (length == standardValues[i]) { selectedItem = i; owner.getDisplay().setMaxIterations(standardValues[i]); items[i].setSelected(true); return; } } items[items.length-1].setSelected(true); owner.getDisplay().setMaxIterations(length); } public void actionPerformed(ActionEvent evt) { if (items[items.length-1].isSelected()) { String valueStr = JOptionPane.showInputDialog( I18n.tr("command.maxiterations.customQuestion"), owner.getDisplay().getMaxIterations()); if (valueStr == null || valueStr.trim().length() == 0) { items[selectedItem].setSelected(true); return; } try { int value = Integer.parseInt(valueStr); if (value < 0) throw new NumberFormatException(); if (value > 500000) throw new NumberFormatException(); owner.getDisplay().setMaxIterations(value); selectedItem = items.length - 1; } catch (NumberFormatException e) { JOptionPane.showMessageDialog( owner,I18n.tr("command.maxiterations.customError",valueStr)); items[selectedItem].setSelected(true); return; } } else { for (int i = 0; i < standardValues.length; i++) { if (items[i].isSelected()) { owner.getDisplay().setMaxIterations(standardValues[i]); selectedItem = i; break; } } } } } // end nested class MaxIterationsManager } // end class Menus
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -