package com.lgauvin; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.HashMap; import java.util.Map; public class MMCalculator extends JApplet { /** * Create the GUI. For thread safety, this method should * be invoked from the event-dispatching thread. */ private JTextField textField1; private JTextField textField2; private JTextField textField3; private JTextField textField4; private JTextField textField5; private JTextField textField6; private JTextField textField7; private JTextField textField8; private JButton button1; private JButton button2; private JTextArea textArea1; private void createGUI() { Color C1 = Color.decode("#FFFFFF"); Color C2 = Color.decode("#228B22"); Color C3 = Color.decode("#92C7C7"); Color C4 = Color.decode("#347C2C"); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension dim = tk.getScreenSize(); int xPos = (dim.width / 2) - (this.getWidth() / 2); int yPos = (dim.height / 2) - (this.getHeight() / 2); this.setSize(500,dim.height-100); this.setLocation(xPos, yPos); this.setName("Molar Mass Calculator"); JPanel thePanel = new JPanel(); thePanel.setBackground(C2); JLabel Element1 = new JLabel("Enter Element 1:"); Element1.setForeground(Color.WHITE); thePanel.add(Element1); //JPanel //button button1 = new JButton("Calculate Molar Mass"); ListenForButton lForButton = new ListenForButton(); button1.addActionListener(lForButton); button1.setBackground(C1); //button //button 2 button2 = new JButton("Clear"); ListenForButton2 lForButton2 = new ListenForButton2(); button2.addActionListener(lForButton2); button2.setBackground(C1); //text field 1 textField1 = new JTextField("", 15); ListenForKeys lForKeys = new ListenForKeys(); textField1.addKeyListener(lForKeys); textField1.setBackground(C1); textField1.setBounds(20,45,200,40); thePanel.add(textField1); //text field 1 JLabel Quantity1 = new JLabel("Quantity:"); Quantity1.setForeground(Color.WHITE); thePanel.add(Quantity1); //text field 2 textField2 = new JTextField("0", 10); textField2.addKeyListener(lForKeys); textField2.setBackground(C1); thePanel.add(textField2); //text field 2 JLabel Element2 = new JLabel("Enter Element 2:"); Element2.setForeground(Color.WHITE); thePanel.add(Element2); //text field 3 textField3 = new JTextField("", 15); textField3.addKeyListener(lForKeys); textField3.setBackground(C1); thePanel.add(textField3); //text field 3 JLabel Quantity2 = new JLabel("Quantity:"); Quantity2.setForeground(Color.WHITE); thePanel.add(Quantity2); //text field 4 textField4 = new JTextField("0", 10); textField4.addKeyListener(lForKeys); textField4.setBackground(C1); thePanel.add(textField4); //text field 4 JLabel Element3 = new JLabel("Enter Element 3:"); Element3.setForeground(Color.WHITE); thePanel.add(Element3); //text field 5 textField5 = new JTextField("", 15); textField5.addKeyListener(lForKeys); textField5.setBackground(C1); thePanel.add(textField5); //text field 5 JLabel Quantity3 = new JLabel("Quantity:"); Quantity3.setForeground(Color.WHITE); thePanel.add(Quantity3); //text field 6 textField6 = new JTextField("0", 10); textField6.addKeyListener(lForKeys); textField6.setBackground(C1); thePanel.add(textField6); //text field 6 JLabel Element4 = new JLabel("Enter Element 4:"); Element4.setForeground(Color.WHITE); thePanel.add(Element4); //text field 7 textField7 = new JTextField("", 15); textField7.addKeyListener(lForKeys); textField7.setBackground(C1); thePanel.add(textField7); //text field 8 JLabel Quantity4 = new JLabel("Quantity:"); Quantity4.setForeground(Color.WHITE); thePanel.add(Quantity4); //text field 8 textField8 = new JTextField("0", 10); textField8.addKeyListener(lForKeys); textField8.setBackground(C1); thePanel.add(textField8); //text field 8 //text area textArea1 = new JTextArea(40, 40); textArea1.setRows(((dim.height)/25)); textArea1.setText(""); textArea1.setLineWrap(true); textArea1.setWrapStyleWord(true); textArea1.setBackground(C1); thePanel.add(button1); thePanel.add(button2); //text area //scrollbar JScrollPane scrollbar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); thePanel.add(scrollbar1); this.add(thePanel); //scrollbar this.setVisible(true); ListenForMouse lForMouse = new ListenForMouse(); thePanel.addMouseListener(lForMouse); } private class ListenForButton implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == button1) textArea1.append(null); Map<String, Double> molarMass = new HashMap<>(); molarMass.put("H", 1.01); //Element 1 molarMass.put("He", 4.00); //2 molarMass.put("Li",6.94); //3 molarMass.put("Be",9.01); //4 molarMass.put("B", 10.81); //5 molarMass.put("C", 12.01); //6 molarMass.put("N", 14.01); //7 molarMass.put("O", 16.00); //8 molarMass.put("F", 19.00); //9 molarMass.put("Ne", 20.18); //10 molarMass.put("Na", 22.99); //11 molarMass.put("Mg", 24.31); //12 molarMass.put("Al", 26.98); //13 molarMass.put("Si", 28.07); //14 molarMass.put("P", 30.97); //15 molarMass.put("S", 32.07); //16 molarMass.put("Cl", 35.45); //17 molarMass.put("Ar", 39.95); //18 molarMass.put("K", 39.10); //19 molarMass.put("Ca", 40.08); //20 molarMass.put("Sc", 44.96); //21 molarMass.put("Ti", 47.87); //22 molarMass.put("V", 50.94); //23 molarMass.put("Cr", 52.00); //24 molarMass.put("Mn", 54.94); //25 molarMass.put("Fe",55.85); //26 molarMass.put("Co", 58.93); //27 molarMass.put("Ni", 58.69); //28 molarMass.put("Cu", 63.55); //29 molarMass.put("Zn", 65.38); //30 molarMass.put("Ga", 69.72); //31 molarMass.put("Ge", 72.64); //32 molarMass.put("As", 74.92); //33 molarMass.put("Se", 78.96); //34 molarMass.put("Br", 79.90); //35 molarMass.put("Kr", 83.80); //36 molarMass.put("Rb", 85.47); //37 molarMass.put("Sr", 87.62); //38 molarMass.put("Y", 88.91); //39 molarMass.put("Zr", 91.22); //40 molarMass.put("Nb", 92.91); //41 molarMass.put("Mo", 95.94); //42 molarMass.put("Tc", 98.00); //43 molarMass.put("Ru", 101.07); //44 molarMass.put("Rh", 102.91); //45 molarMass.put("Pd", 106.42); //46 molarMass.put("Ag", 107.87); //47 molarMass.put("Cd", 112.41); //48 molarMass.put("In", 114.82); //49 molarMass.put("Sn", 118.71); //50 molarMass.put("Sb", 121.76); //51 molarMass.put("Te", 127.60); //52 molarMass.put("I", 126.90); //53 molarMass.put("Xe", 131.29); //54 molarMass.put("Cs", 132.91); //55 molarMass.put("Ba", 137.33); //56 molarMass.put("La", 138.91); //57 molarMass.put("Ce", 140.12); //58 molarMass.put("Pr", 140.91); //59 molarMass.put("Nd", 144.24); //60 molarMass.put("Pm", 145.00); //61 molarMass.put("Sm", 150.36); //62 molarMass.put("Eu", 151.96); //63 molarMass.put("Gd", 157.25); //64 molarMass.put("Tb", 158.93); //65 molarMass.put("Dy", 162.50); //66 molarMass.put("Ho", 164.93); //67 molarMass.put("Er", 167.26); //68 molarMass.put("Tm", 168.93); //69 molarMass.put("Yb", 173.04); //70 molarMass.put("Lu", 174.97); //71 molarMass.put("Hf", 178.49); //72 molarMass.put("Ta", 180.95); //73 molarMass.put("W", 183.84); //74 molarMass.put("Re", 186.21); //75 molarMass.put("Os", 190.23); //76 molarMass.put("Ir", 192.22); //77 molarMass.put("Pt", 196.08); //78 molarMass.put("Au", 196.97); //79 molarMass.put("Hg", 200.59); //80 molarMass.put("Tl", 204.38); //81 molarMass.put("Pb", 207.20); //82 molarMass.put("Bi", 208.98); //83 molarMass.put("Po", 209.00); //84 molarMass.put("At", 210.00); //85 molarMass.put("Rn", 222.00); //86 molarMass.put("Fr", 223.00); //87 molarMass.put("Ra", 226.00); //88 molarMass.put("", 0.00); //null value //first element String a = textField1.getText(); //first element quantity int n = Integer.parseInt(textField2.getText()); //second element String b = textField3.getText(); // second element quantity int k = Integer.parseInt(textField4.getText()); //third element String c = textField5.getText(); //third element quantity int z = Integer.parseInt(textField6.getText()); //fourth element String d = textField7.getText(); //fourth element quantity int y = Integer.parseInt(textField8.getText()); double mM = (molarMass.get(a) * n) + (molarMass.get(b) * k) + (molarMass.get(c) * z) + (molarMass.get(d) * y); textArea1.append("\n \nThe molar mass of "); textArea1.append(a); if (n > 1){ textArea1.append(String.valueOf(n)); } textArea1.append(b); if (k > 1){ textArea1.append(String.valueOf(k)); } textArea1.append(c); if (z > 1){ textArea1.append(String.valueOf(z)); } textArea1.append(d); if (y > 1){ textArea1.append(String.valueOf(y)); } textArea1.append(" is:"); textArea1.append("\n" + String.valueOf(mM)); } } private class ListenForButton2 implements ActionListener { public void actionPerformed(ActionEvent e) { textArea1.setText(null); } } private class ListenForWindow implements WindowListener { public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent arg0) { // TODO Auto-generated method stub } public void windowClosing(WindowEvent arg0) { // TODO Auto-generated method stub } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent arg0) { } public void windowIconified(WindowEvent arg0) { } public void windowOpened(WindowEvent arg0) { } } public void init() { try { SwingUtilities.invokeAndWait(this::createGUI); } catch (Exception e) { System.err.println("createGUI didn't successfully complete: " + e); } } public static void main(String[] args) { JApplet applet = new MMCalculator(); applet.init(); JFrame frame = new JFrame("Applet in Frame"); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add( applet ); frame.pack(); Toolkit tk = Toolkit.getDefaultToolkit(); Dimension dim = tk.getScreenSize(); frame.setSize(500,dim.height-100); frame.setLocationRelativeTo( null ); frame.setVisible( true ); applet.start(); } } class ListenForKeys implements KeyListener { public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { } } class ListenForMouse implements MouseListener { public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent arg0) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent arg0) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent arg0) { // TODO Auto-generated method stub } }