import java.applet.*; import java.awt.*; import java.util.Vector; public class SLDCalculator extends Applet { TextField chemicalField, densityField, lambdaField; Button calculate; Label incohLabel, absorbLabel, lengthLabel, neutronLabel; Label xrayCuLabel, xrayMoLabel; Vector elementVector; double density, wavelength; public void init() { Font font = new Font("TimesRoman",Font.PLAIN,12); setLayout(new GridLayout(0,2)); setBackground(Color.white); Label compoundLabel = new Label("Compound"); compoundLabel.setFont(font); chemicalField = new TextField(20); chemicalField.setFont(font); add(compoundLabel); add(chemicalField); Label densityLabel = new Label("Density (g/cm^3)"); densityLabel.setFont(font); densityField = new TextField(20); densityField.setFont(font); add(densityLabel); add(densityField); Label lambdaLabel = new Label("Wavelength (A)"); lambdaLabel.setFont(font); lambdaField = new TextField(20); lambdaField.setFont(font); add(lambdaLabel); add(lambdaField); Label spacer = new Label(" "); spacer.setFont(font); calculate = new Button("Calculate"); calculate.setFont(font); add(spacer); add(calculate); Label neutronExplLabel = new Label("Neutron SLD"); neutronExplLabel.setFont(font); add(neutronExplLabel); neutronLabel = new Label("0.00e-6 + 0.00e-6i (A^-2)"); neutronLabel.setFont(font); add(neutronLabel); Label xrayExplCuLabel = new Label("Cu Ka SLD"); xrayExplCuLabel.setFont(font); add(xrayExplCuLabel); xrayCuLabel = new Label("0.00e-6 + 0.00e-6i (A^-2)"); xrayCuLabel.setFont(font); add(xrayCuLabel); Label xrayExplMoLabel = new Label("Mo Ka SLD"); xrayExplMoLabel.setFont(font); add(xrayExplMoLabel); xrayMoLabel = new Label("0.00e-6 + 0.00e-6i (A^-2)"); xrayMoLabel.setFont(font); add(xrayMoLabel); Label incohExplLabel = new Label("Neutron Inc. XS"); incohExplLabel.setFont(font); add(incohExplLabel); incohLabel = new Label("0.00 (cm^-1)"); incohLabel.setFont(font); add(incohLabel); Label absorbExplLabel = new Label("Neutron Abs. XS"); absorbExplLabel.setFont(font); add(absorbExplLabel); absorbLabel = new Label("0.00 (cm^-1)"); absorbLabel.setFont(font); add(absorbLabel); Label lengthExplLabel = new Label("Neutron 1/e length"); lengthExplLabel.setFont(font); add(lengthExplLabel); lengthLabel = new Label("0.00 (cm)"); lengthLabel.setFont(font); add(lengthLabel); validate(); } public boolean action(Event e, Object a) { if (e.target instanceof Button) { //parse chemical field String inputString = chemicalField.getText(); try { elementVector = NCNRUtils.parseCompound(inputString); } catch (BadCompoundException exc) { neutronLabel.setText(exc.getMessage()); incohLabel.setText(exc.getMessage()); absorbLabel.setText(exc.getMessage()); lengthLabel.setText(exc.getMessage()); return true; } //parse density field density=-1.; String errorText = "Bad Value for Density"; try { density=Double.valueOf(densityField.getText()).doubleValue(); } catch (NumberFormatException exc) { neutronLabel.setText(errorText); xrayCuLabel.setText(errorText); xrayMoLabel.setText(errorText); incohLabel.setText(errorText); absorbLabel.setText(errorText); lengthLabel.setText(errorText); return true; } if((density <= 0.0) || Double.isNaN(density)){ neutronLabel.setText(errorText); xrayCuLabel.setText(errorText); xrayMoLabel.setText(errorText); incohLabel.setText(errorText); absorbLabel.setText(errorText); lengthLabel.setText(errorText); return true; } //parse wavelength field wavelength=1.8; try { wavelength=Double.valueOf(lambdaField.getText()).doubleValue(); } catch (NumberFormatException exc) { lambdaField.setText(String.valueOf(1.8)); } if (wavelength <= 0.0) { wavelength = 1.8; lambdaField.setText(String.valueOf(1.8)); } String signString = "+"; String outputText = ""; String realText =""; double[] output = NCNRUtils.calculateSLD(elementVector, density, wavelength); double tempValue; double count; long tempSLD; //Arrange neutron SLD label if(Double.isNaN(output[0])) { outputText = "That data not available."; neutronLabel.setText(outputText); } else { realText = String.valueOf(NCNRUtils.sigfigs(output[0],3)); if(output[1] != 0.0) { if(output[1] < 0.0) signString = ""; outputText = realText+" "+signString +String.valueOf(NCNRUtils.sigfigs(output[1],3))+"i (A^-2)"; } else { outputText = realText+" (A^-2)"; } neutronLabel.setText(outputText); } //Arrange x-ray SLD label signString = "+"; if(Double.isNaN(output[2])) { outputText = "That data not available."; xrayCuLabel.setText(outputText); xrayMoLabel.setText(outputText); } else { realText = String.valueOf(NCNRUtils.sigfigs(output[2],3)); if(output[3] != 0.0) { if(output[3] < 0.0) signString = ""; outputText = realText+" "+signString +String.valueOf(NCNRUtils.sigfigs(output[3],3))+"i (A^-2)"; } else { outputText = realText+" (A^-2)"; } xrayCuLabel.setText(outputText); realText = String.valueOf(NCNRUtils.sigfigs(output[4],3)); if(output[5] != 0.0) { if(output[5] < 0.0) signString = ""; outputText = realText+" "+signString +String.valueOf(NCNRUtils.sigfigs(output[5],3))+"i (A^-2)"; } else { outputText = realText+" (A^-2)"; } xrayMoLabel.setText(outputText); } if((Double.isNaN(output[6]))||(Double.isNaN(output[9]))) { outputText = "That data not available."; incohLabel.setText(outputText); } else { realText = String.valueOf(NCNRUtils.sigfigs(output[6],3)); //realText += "; "+String.valueOf(NCNRUtils.sigfigs(output[9],3)); outputText = realText+" (cm^-1)"; incohLabel.setText(outputText); } if(Double.isNaN(output[7])) { outputText = "That data not available."; absorbLabel.setText(outputText); } else { realText = String.valueOf(NCNRUtils.sigfigs(output[7],3)); outputText = realText+" (cm^-1)"; absorbLabel.setText(outputText); } if(Double.isNaN(output[8])) { outputText = "That data not available."; lengthLabel.setText(outputText); } else { realText = String.valueOf(NCNRUtils.sigfigs(output[8],3)); outputText = realText+" (cm)"; lengthLabel.setText(outputText); } } return true; } }