📄 valuatorcontrols.java
字号:
new DecNumCities_10(), new DecNumCities_1(), new IncNumCities_1(), new IncNumCities_10(), new IncNumCities_100(), new IncNumCities_1000(), }, { new DecPopSize_1000(), new DecPopSize_100(), new DecPopSize_10(), new DecPopSize_1(), new IncPopSize_1(), new IncPopSize_10(), new IncPopSize_100(), new IncPopSize_1000(), }, { new DecMutation_1000(), new DecMutation_100(), new DecMutation_10(), new DecMutation_1(), new IncMutation_1(), new IncMutation_10(), new IncMutation_100(), new IncMutation_1000(), } }; public static void loadDefaultSettings() { if (CheckBoxControls.getState(CheckBoxControls.CBC_DEBUG_PRINTOUTS)) { m_numCities = 4; m_popSize = 4; m_mutationRate = 1; } else {/*** Unfortunately, these all have to be the same size for the text widget** sizes to come out the same width; with the leading "0." for the** mutation rate, this collection works. We're reduced to this because** all the setSize() function for labels does is produce spawn time** artifacts in the applet; it does _not_ control the final label size!** Grrrr. At least, long too late, I realized that using a monospaced** font would help. In my defense, the clue was well hidden, in file** /jdk1_4_1/jre/lib/font.properties ; nothing in the jdk2.0** documentation tells you what font families are available and by what** names to evoke them, but the above file does. I finally fixed, or at** least evaded, the valuator text field label width problem, details** are in Traveller.java's initHelper() module, for now, until a more** robust and believable solution is found.*/ m_numCities = 100; m_popSize = 100; m_mutationRate = 50; // units are 1/100ths of 1% } } public static void saveSettings() { m_numCities = Integer .valueOf( m_valuatorTextFields[VAL_CONFIG_NUMCITIES].getText() ) .intValue(); m_popSize = Integer .valueOf( m_valuatorTextFields[VAL_CONFIG_POPSIZE].getText() ) .intValue(); double mutationDouble = Double .valueOf( m_valuatorTextFields[VAL_CONFIG_MUTERATE].getText() ) .doubleValue(); m_mutationRate = (int) Math.round( 100.0D * mutationDouble ); } public static void loadSettings() { m_valuatorTextFields[VAL_CONFIG_NUMCITIES] .setText(String.valueOf(m_numCities)); m_valuatorTextFields[VAL_CONFIG_POPSIZE] .setText(String.valueOf(m_popSize)); m_valuatorTextFields[VAL_CONFIG_MUTERATE] .setText( mutationRateAsString() ); m_valuesPanel.repaint(); } public static void enable() { for (int n = 0; n < VAL_CONFIG_LABEL.length; ++n) { m_btnAddSub[n][BTN_SUB_1000].setEnabled(true); m_btnAddSub[n][BTN_SUB_100].setEnabled(true); m_btnAddSub[n][BTN_SUB_10].setEnabled(true); m_btnAddSub[n][BTN_SUB_1].setEnabled(true); m_valuatorTextFields[n].setEnabled(true); m_btnAddSub[n][BTN_ADD_1].setEnabled(true); m_btnAddSub[n][BTN_ADD_10].setEnabled(true); m_btnAddSub[n][BTN_ADD_100].setEnabled(true); m_btnAddSub[n][BTN_ADD_1000].setEnabled(true); } } public static void disable() { for (int n = 0; n < VAL_CONFIG_LABEL.length; ++n) { m_btnAddSub[n][BTN_SUB_1000].setEnabled(false); m_btnAddSub[n][BTN_SUB_100].setEnabled(false); m_btnAddSub[n][BTN_SUB_10].setEnabled(false); m_btnAddSub[n][BTN_SUB_1].setEnabled(false); m_valuatorTextFields[n].setEnabled(false); m_btnAddSub[n][BTN_ADD_1].setEnabled(false); m_btnAddSub[n][BTN_ADD_10].setEnabled(false); m_btnAddSub[n][BTN_ADD_100].setEnabled(false); m_btnAddSub[n][BTN_ADD_1000].setEnabled(false); } }/*** FIXME There is a better way to do this, with number formatting, but** this works. Maybe someday...*/ private static String mutationRateAsString() { return ( String.valueOf(m_mutationRate / 100) + "." + ( ( m_mutationRate % 100 < 10 ) ? "0" : "" ) + String.valueOf( m_mutationRate % 100 ) ); } public static int getPopulationSize() { return m_popSize; } public static int getNumberOfCities() { return m_numCities; } public static double getMutationRate() { return ( (double) m_mutationRate ) / 10000.0D; // units of 1/100ths of a % } public static void setup() {/*** FIXED All the rest of the pieces needed to construct this panel need** to be moved in here, such as the label and button array declarations,** and this method needs to be modified to return a panel, rather than** have everything passed as globals by the parent class, which is** trashy. Then the whole thing needs to be moved out in a class of its** own, along with the listener methods.*/ // create valuators control window frame m_valuesFrame = new TravellerFrame(VALUES_NAME);/* m_valuesFrame.setSize ( TravellerFrame.PLAYFIELD_FRAME_WIDTH, 100 + ( 2 * TravellerFrame.FRAME_EDGE_THICKNESS ) + TravellerFrame.getTitleBarHeightAllowance() );*/ m_valuesFrame.setVisible(true); m_valuesFrame.invalidate(); // create valuators control panel to go in the frame m_valuesPanel = new EdgedPanel(); m_valuesPanel.setBackground(TravellerColors.BAR_COLOR); m_valuesPanel.setForeground(TravellerColors.BAR_TEXT); // create a reusable constraints bag GridBagConstraints gbc = new GridBagConstraints(); // Set some values in the bag gbc.weightx = 1.0; gbc.weighty = 0.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = GridBagConstraints.REMAINDER; // add controls to panel GridBagLayout valuesLayout = new GridBagLayout(); m_valuesPanel.setLayout(valuesLayout); // add a label for this panel's purpose Font configLabelFont = new Font("SansSerif",Font.PLAIN,15); Label temp = new Label("M: 1000 C: 100 X: 10 I: 1",Label.CENTER); temp.setBackground(TravellerColors.BAR_COLOR); temp.setForeground(TravellerColors.BAR_TEXT); temp.setFont(configLabelFont); Utility.addToBag ( m_valuesPanel, temp, gbc, valuesLayout );/*** Create a font for the individual valuators' labels.*/ Font tfont = new Font("SansSerif",Font.PLAIN,12);/*** Create a font for the button labels and text field contents which** uses a fixed pitch typeface, so that we can better control entity** sizes.*/ Font ffont = new Font("monospaced",Font.PLAIN,12); // allocate control arrays m_valuatorTextFields = new Label[VAL_CONFIG_LABEL.length]; m_btnAddSub = new Button[VAL_CONFIG_LABEL.length][BTN_COUNT]; // add value input text widget controls gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.fill = GridBagConstraints.NONE; for (int n = 0; n < VAL_CONFIG_LABEL.length; ++n) { // add label gbc.gridwidth = 1; gbc.fill = GridBagConstraints.NONE; gbc.anchor = GridBagConstraints.EAST; Label tcontrol = new Label(VAL_CONFIG_LABEL[n],Label.RIGHT); tcontrol.setFont(tfont); Utility.addToBag ( m_valuesPanel, tcontrol, gbc, valuesLayout ); // add a "subtract 1000" button gbc.anchor = GridBagConstraints.CENTER; m_btnAddSub[n][BTN_SUB_1000] = new Button("-M"); m_btnAddSub[n][BTN_SUB_1000].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_SUB_1000].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_SUB_1000].setFont(ffont); m_btnAddSub[n][BTN_SUB_1000]. addActionListener(m_cfgAddSubListeners[n][BTN_SUB_1000]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_SUB_1000], gbc, valuesLayout ); // add a "subtract 100" button gbc.anchor = GridBagConstraints.CENTER; m_btnAddSub[n][BTN_SUB_100] = new Button("-C"); m_btnAddSub[n][BTN_SUB_100].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_SUB_100].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_SUB_100].setFont(ffont); m_btnAddSub[n][BTN_SUB_100]. addActionListener(m_cfgAddSubListeners[n][BTN_SUB_100]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_SUB_100], gbc, valuesLayout ); // add a "subtract 10" button gbc.anchor = GridBagConstraints.CENTER; m_btnAddSub[n][BTN_SUB_10] = new Button("-X"); m_btnAddSub[n][BTN_SUB_10].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_SUB_10].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_SUB_10].setFont(ffont); m_btnAddSub[n][BTN_SUB_10]. addActionListener(m_cfgAddSubListeners[n][BTN_SUB_10]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_SUB_10], gbc, valuesLayout ); // add a "subtract 1" button gbc.anchor = GridBagConstraints.CENTER; m_btnAddSub[n][BTN_SUB_1] = new Button("-I"); m_btnAddSub[n][BTN_SUB_1].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_SUB_1].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_SUB_1].setFont(ffont); m_btnAddSub[n][BTN_SUB_1]. addActionListener(m_cfgAddSubListeners[n][BTN_SUB_1]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_SUB_1], gbc, valuesLayout ); // add text field; a label will do since it is passive m_valuatorTextFields[n] = new Label("XXXXXX",Label.CENTER); m_valuatorTextFields[n].setBackground(Color.white); m_valuatorTextFields[n].setForeground(Color.black); m_valuatorTextFields[n].setFont(ffont); Utility.addToBag ( m_valuesPanel, m_valuatorTextFields[n], gbc, valuesLayout );/*** KPD FIXME This code has no visible effect anywhere I put it, which** is why the third valuator whitespace is sometimes the wrong size. I** need to learn how to control a label's size, and so far I cannot find** the trick.*//*** for (int n = 0; n < VAL_CONFIG_LABEL.length; ++n)** {** m_valuatorTextFields[n].setSize( valuatorWidth , valuatorHeight );** }** int valuatorHeight = 20;** int valuatorWidth = 54;*/ // add an "add 1" button m_btnAddSub[n][BTN_ADD_1] = new Button("+I"); m_btnAddSub[n][BTN_ADD_1].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_ADD_1].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_ADD_1].setFont(ffont); m_btnAddSub[n][BTN_ADD_1]. addActionListener(m_cfgAddSubListeners[n][BTN_ADD_1]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_ADD_1], gbc, valuesLayout ); // add an "add 10" button m_btnAddSub[n][BTN_ADD_10] = new Button("+X"); m_btnAddSub[n][BTN_ADD_10].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_ADD_10].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_ADD_10].setFont(ffont); m_btnAddSub[n][BTN_ADD_10]. addActionListener(m_cfgAddSubListeners[n][BTN_ADD_10]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_ADD_10], gbc, valuesLayout ); // add an "add 100" button m_btnAddSub[n][BTN_ADD_100] = new Button("+C"); m_btnAddSub[n][BTN_ADD_100].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_ADD_100].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_ADD_100].setFont(ffont); m_btnAddSub[n][BTN_ADD_100]. addActionListener(m_cfgAddSubListeners[n][BTN_ADD_100]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_ADD_100], gbc, valuesLayout ); // add an "add 1000" button m_btnAddSub[n][BTN_ADD_1000] = new Button("+M"); m_btnAddSub[n][BTN_ADD_1000].setBackground(TravellerColors.BTN_COLOR); m_btnAddSub[n][BTN_ADD_1000].setForeground(TravellerColors.BTN_TEXT); m_btnAddSub[n][BTN_ADD_1000].setFont(ffont); m_btnAddSub[n][BTN_ADD_1000]. addActionListener(m_cfgAddSubListeners[n][BTN_ADD_1000]); Utility.addToBag ( m_valuesPanel, m_btnAddSub[n][BTN_ADD_1000], gbc, valuesLayout ); // add a horizontal space at the end of each row gbc.gridwidth = GridBagConstraints.REMAINDER; Label hspace_1 = new Label(); hspace_1.setFont(tfont); Utility.addToBag ( m_valuesPanel, hspace_1, gbc, valuesLayout ); } // add completed panel to frame m_valuesFrame.add ( "Center", m_valuesPanel // ValuatorControls.buildValuesPanel() ); m_valuesFrame.pack(); // Let the frame set its own size!/*** Grrr. THIS was the stupid, unfairly obscure trick to get valuator** labels to start out and remain the desired size. Fill them with** spacer "X"s, _validate()_ them once and only once after they are** competently attached to a frame, to get that length attached to them** somewhere in Java's tiny brain, and then merely _repaint()_ forever** after to keep the labels from resizing to match the length of their** contents-of-the-moment. Finding this took _months_.*/ // freeze the text field label sizes before they get changed on us m_valuesFrame.validate(); // return m_valuesPanel; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -