Sample JAVA program to visualize the electron's distribution of Bohr model NH3.

Back to the chemical bond page
Top page ( correct Bohr model including the two electron atom )

This program is a little long. So if you copy and paste the below program source code into a text editor, you can easily compile and run this.
This program's class file name is NH3, so save this text editor as "NH3.java", and compile it.

In this program, nuclei are gray circles.
Here we use the new units, ( 1 MM = 10-14 meter).
Each coordinate of electrons (+X (MM), +Y (MM), +Z (MM)) in the text box means "relative" position from these nuclei.
(ele 0-4 are from nitrogen nucleus, ele 5 is from hydrogen nucleus 0 (H0), ele6 is from H1, and ele7 is from H2.)
You can change the coordinate (+X, +Y, +Z) freely.
(Enter the values into the textboxes, and press the Enter key.)
"nuc (MM)" means the distance between these nuclei and electrons.
V (eV) and T (eV) means the potential, and kinetic energies of each electron.
tV (eV) is the total potential energy.
CF of ele0-4 means the force toward the center, CF of ele5,6,7 means the force toward each N-H line (=perpendicular to N-H line).
(fx, fy, fz) mean force components other than CF.
(FX, FY, FZ) mean the force component acting on each nucleus.
Nf of H0 nucleus means the force toward N nucleus.
Waves (wn) is number of de Broglie's waves contained in one orbit.
when you choose the N-H bond length (MM) in the scrollbar and click "N-H (MM)" button, the internuclear distance of N-H change.
And when you choose the angle in the scrollbar and click "angle" button, H-N-H angle changes.
When you click "e5,6,7" button, electrons 5,6 and 7 are arrahnged "symmetrically".


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
public class NH3 extends JPanel     // virial theorem of ammonia
 {
  public static void main(String arg[])
 {
   JFrame frame = new JFrame("NH3 (ammonia)");
   J2DPanel j2dpanel = new J2DPanel();
   frame.getContentPane().add(j2dpanel); frame.setSize(1180,700);
   frame.setVisible(true); frame.setBackground(Color.black);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  }
  }
 class J2DPanel extends JPanel implements ActionListener
  {
    
    double pai=3.141592653589793; double epsi=8.85418781787346e-12;
   double h=6.62606896e-34; double elc=1.60217653e-19;
   double me=9.1093826e-31;  double suh=5200.0*5200.0; // suh=(Bohr radius)^2
 
    JTextField elp[][]=new JTextField[8][11];  JTextField impho=new JTextField(7); 
    JTextField mmpho[][]=new JTextField[4][4]; 

    JButton b1=new JButton("N-H (MM)");  JButton b2=new JButton("angle");
    String ope[]={"9000","10170","11000"};     // scrollbar of N-H distances
    JComboBox coom=new JComboBox(ope);   
     String ope2[]={"100.0","107.8","110.0"};   // scrollbar of H-N-H angles
    JComboBox coom2=new JComboBox(ope2);  
  
    JButton b3=new JButton("e5,6,7");
   
    int mar=0;     // mar=marking
    double rtw=Math.sqrt(2); double rth=Math.sqrt(3); 
    double rsi=Math.sqrt(6); double rfi=Math.sqrt(5);
    double hpr[][]=new double[8][11]; double hpr2[][]=new double[8][3];
  
    double den=5.25;                         // den = central charge
    double lengt=10170.0; double angl=107.8; 
     double lengt2=lengt*Math.sin(angl*0.5*pai/180.0);  // legth2= half of H-H distance 
     double lengt3=lengt*Math.cos(angl*0.5*pai/180.0);
     double lengt4=lengt2/rth; double lengt5=Math.sqrt(lengt3*lengt3-lengt4*lengt4);
     double lengt6=(lengt2*2.0)/rth;

                   // nux[n][0-2]=nuclear coordinate: n=0 (N), n=1 (H0), n=2 (H1), n=3 (H2) 
    double nux[][]={{12500.00, 12500.00, 17143, den},
    {12500.0+lengt6, 12500.0, 17143-lengt5, 1.0},
    {12500.0-0.5*lengt6, 12500.0+lengt6*rth*0.5, 17143.0-lengt5, 1.0},
    {12500.0-0.5*lengt6, 12500.0-lengt6*rth*0.5, 17143.0-lengt5, 1.0}}; 
                                                     
                                     // te1=initial conditions of each electron
    double te1[][]={{4870.0, 0.0, -2350}, {-2685.0, 3685.0, -2488.0}, 
    {-2685.0, -3685.0, -2488.0}, {595.0, 3780.0, 3950.0}, 
    {595.0, -3780.0, 3950.0}, {340.0, -4380.0, -370.0}, 
    {3790.0, 2506.0, -400.0}, {-4020.0, 1770.0, -450.0}};
    double te2[][]={{4800.0, 0, -2160}, {-2540.0, 3750.0, -2400.0}, 
    {-2540.0, -3750.0, -2400.0}, {490.0, 3800.0, 4150.0}, 
    {450.0, -3800.0, 4100.0}, {800.0, -4200.0, -500.0}, 
    {3470.0, 2480.0, -440.0}, {-3700.0, 1545.0, -450.0}};
    double te3[][]={{4820.0, 0, -2510}, {-2695.0, 3685.0, -2570.0}, 
    {-2695.0, -3685.0, -2570.0}, {616.0, 3780.0, 3940.0}, 
    {600.0, -3780.0, 3940.0}, {350.0, -4280.0, -370.0}, 
    {3680.0, 2500.0, -440.0}, {-4000.0, 1700.0, -450.0}};
     double te4[][]={{4880.0, 0, -2380}, {-2695.0, 3685.0, -2470.0}, 
    {-2695.0, -3685.0, -2470.0}, {650.0, 3780.0, 3940.0}, 
    {650.0, -3780.0, 3940.0}, {290.0, -4400.0, -370.0}, 
    {3820.0, 2600.0, -350.0}, {-4020.0, 1800.0, -450.0}};
     double te5[][]={{4870.0, 0, -2300}, {-2695.0, 3660.0, -2550.0}, 
    {-2695.0, -3650.0, -2560.0}, {530.0, 3740.0, 3940.0}, 
    {520.0, -3740.0, 3940.0}, {220.0, -4480.0, -260.0}, 
    {3890.0, 2400.0, -300.0}, {-4000.0, 2026.0, -300.0}};



  public J2DPanel()
 {
  setBackground(Color.black);
  JPanel p=new JPanel();
  p.setLayout(new GridLayout(11,12));
  int aaa=0; 
                               
  for (int el=0; el <=7; el++) {         // elp[0-7][0-2]=textboxes of each electron's coordinate
  for (int pos=0; pos <=2; pos++) {
  elp[el][pos]=new JTextField(7); elp[el][pos].addActionListener(this);
  hpr[el][pos]=0.0;    
  }}
                                   
   for (int el=0; el <=7; el++) {         // elp[0-7][3-10]=textboxes of other parameters
  for (int pos=3; pos <=10; pos++) {
  elp[el][pos]=new JTextField(7);     
  hpr[el][pos]=0.0; 
  }}

   for (int el=0; el <=3; el++) {
   for (int pos=0; pos <=3; pos++) {
    mmpho[el][pos]=new JTextField(7);
   }}

                                        // layout

  String sihy[]={"eNo ", "+X(MM)", "+Y(MM)", "+Z(MM)", "nuc(MM)", 
   "V(eV)", "T(eV)", "Force", "fx ", "fy", "fz", "Waves"};
  for (int el=0; el <=11; el++) {
   p.add(new Label(sihy[el]));
  }
 
  for (int el=0; el <=7; el++) {       
  p.add(new Label("ele "+el+" "));
  for (int pos=0; pos <=10; pos++) {
  p.add(elp[el][pos]);
  }}

   p.add(new Label("N nuc "));
  for (int pos=0; pos <=2; pos++) {
  p.add(mmpho[0][pos]);  
  }
    p.add(new Label(" -- "));  p.add(impho);
    p.add(new Label("H0nuc "));
  for (int pos=0; pos <=3; pos++) {
  p.add(mmpho[1][pos]);  
  }
    p.add(b3);
 
   p.add(new Label("H1nuc "));
  for (int pos=0; pos <=2; pos++) {
  p.add(mmpho[2][pos]);
  }
   p.add(new Label("H2nuc "));
  for (int pos=0; pos <=2; pos++) {
  p.add(mmpho[3][pos]);
  }
   p.add(b1); p.add(coom); p.add(b2); p.add(coom2); 
   coom.setSelectedItem("10170"); b1.addActionListener(this);
   coom2.setSelectedItem("107.8"); b2.addActionListener(this); b3.addActionListener(this); 

  add(p,"South");
  
   for (int el=0; el <=7; el++) {
   double nnuc=Math.sqrt(te1[el][0]*te1[el][0]+te1[el][1]*te1[el][1]+te1[el][2]*te1[el][2]);
   aaa=(int)(nnuc); 
   elp[el][3].setText(Integer.toString(aaa)); // show distance between nuclei and electrons 
    for (int jou=0; jou <=2; jou++) {          // hpr[0-7][0-2]=each electron's coordinate
    hpr[el][jou]=te1[el][jou]; 
    if (el < 5) {hpr[el][jou]=hpr[el][jou]+nux[0][jou];}
     if (el==5) {hpr[el][jou]=hpr[el][jou]+nux[1][jou];}
     if (el==6) {hpr[el][jou]=hpr[el][jou]+nux[2][jou];}
      if (el==7) {hpr[el][jou]=hpr[el][jou]+nux[3][jou];}
    elp[el][jou].setText(Integer.toString((int)(te1[el][jou])));
     }}
   
  }     // public J2DPanel() end
 
  public void actionPerformed(ActionEvent e) {  
    String ss;
    int RR=0; double Rf1,Rf2,Rf3,Rf4; int teis=0; mar=0;

      if (e.getSource() == b1) { RR=1;    // when internuclear distance change (b1 click)
   ss=(String)coom.getSelectedItem();
   if (ss=="9000") {lengt=9000;}  if (ss=="10170") {lengt=10170;} 
   if (ss=="11000") {lengt=11000;} 
    }

    if (e.getSource() == b2) { RR=1;       // when H-N-H angle change (b2 click)
   ss=(String)coom2.getSelectedItem();
   if (ss=="100.0") {angl=100.0;}  if (ss=="107.8") {angl=107.8;} 
   if (ss=="110.0") {angl=110.0;} 
    }

   if (RR==1) {
      double lengt2=lengt*Math.sin(angl*0.5*pai/180.0);  
     double lengt3=lengt*Math.cos(angl*0.5*pai/180.0);
     double lengt4=lengt2/rth; double lengt5=Math.sqrt(lengt3*lengt3-lengt4*lengt4);
     double lengt6=(lengt2*2.0)/rth;

    double nuux[][]={{12500.00, 12500.00, 17143},
    {12500.0+lengt6, 12500.0, 17143-lengt5},
    {12500.0-0.5*lengt6, 12500.0+lengt6*rth*0.5, 17143.0-lengt5},
    {12500.0-0.5*lengt6, 12500.0-lengt6*rth*0.5, 17143.0-lengt5}};
     
     for (int ett=0; ett <=3; ett++) {
     for (int sws=0; sws <=2; sws++) {          // nuclear coordinate reset
      nux[ett][sws]=nuux[ett][sws];
      }}
 
    for (int ett=0; ett <=7; ett++) {   // each electron's coordinate reset
     for (int sws=0; sws <=2; sws++) {
     Rf1=te1[ett][sws];
    if (lengt==9000 && angl==107.8) {Rf1=te2[ett][sws];}
    if (lengt==10170 && angl==100.0) {Rf1=te3[ett][sws];}
    if (lengt==10170 && angl==110.0) {Rf1=te4[ett][sws];}
    if (lengt==11000 && angl==107.8) {Rf1=te5[ett][sws];}
    elp[ett][sws].setText(Integer.toString((int)Rf1));
     }}
      }

    for (int ett=0; ett <=7; ett++) {    // when electron's positions change
    for (int sws=0; sws <=2; sws++) {             
    
    ss=elp[ett][sws].getText(); Rf1=Double.parseDouble(ss); 
   Rf2=0.0;  
                                   // change relative coordinate to absolute coordinate
   if (ett < 5) {Rf2=Rf1+nux[0][sws];} 
   if (ett == 5) {Rf2=Rf1+nux[1][sws];}       
   if (ett==6) {Rf2=Rf1+nux[2][sws];}
   if (ett==7) {Rf2=Rf1+nux[3][sws];}
  
    hpr[ett][sws]=Rf2;  
    }}
   
    if (e.getSource()==b3 ) {mar=1; }  // when clicl b3 button
    

   repaint();
  }

  public void update(Graphics g)
 {
  paint(g);
 }
 public void paintComponent(Graphics g)
 {
   

  double kro,krr,krk,kro2,krr2,krk2,kkk,kkk2, pot,pota,potb,
  pot2,pota2,potb2,potc,potc2,gx,gy,gz,ggx,ggy,ggz,ttav,toav;
  kro=0.0; krr=0.0; krk=0.0; kro2=0.0; krr2=0.0; krk2=0.0; kkk=0.0; kkk2=0.0;
  int ex,ey,ez,xk,yk,zk; String ww,pyw;
  double rhp[][]= {{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},
  {0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0},{0,0,0,0,0,0}};
  double rpp[][]= {{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
  double mmp[][]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};  
  double teqq[][]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
   double noxx[]={0,0,0,0};

  if (mar==1) {              // arrange electrons 5,6,7 symmetrically

    double cor=Math.cos(120.0*pai/180.0);  double sor=Math.sin(120.0*pai/180.0);
    hpr[6][2]=hpr[5][2]-nux[0][2]; hpr[7][2]=hpr[5][2]-nux[0][2];
    gx=hpr[5][0]-nux[0][0]; gy=hpr[5][1]-nux[0][1];
                                    // rotate x,y components of ele 5 by 120 and 240 degrees
    ggx=cor*gx-sor*gy; ggy=sor*gx+cor*gy; hpr[6][0]=ggx; hpr[6][1]=ggy;
    cor=Math.cos(240.0*pai/180.0);  sor=Math.sin(240.0*pai/180.0);
    ggx=cor*gx-sor*gy; ggy=sor*gx+cor*gy; hpr[7][0]=ggx; hpr[7][1]=ggy;
    for (int yp=6; yp <=7; yp++) { 
   for (int jou=0; jou <=2; jou++) {
     hpr[yp][jou]=hpr[yp][jou]+nux[0][jou]; gz=0.0;
     if (yp==6) {gz=hpr[yp][jou]-nux[2][jou];}
     if (yp==7) {gz=hpr[yp][jou]-nux[3][jou];}
     elp[yp][jou].setText(Integer.toString((int)gz));
    }}
   }

                                    // noxx[0-2] = center of electrons 0-4

   for (int yp=0; yp <=4; yp++) { 
   for (int jou=0; jou <=2; jou++) {
   noxx[jou]=noxx[jou]+hpr[yp][jou];
    }}
    for (int jou=0; jou <=2; jou++) {
    noxx[jou]=noxx[jou]/5.0;
    }
  
        // calculate hpr2[5-7][0-2]= symmetric positions of ele 5,6,7 with respect to N-H lines
   for (int yp=5; yp <=7; yp++) {   
   gx=nux[yp-4][0]-nux[0][0];  gy=nux[yp-4][1]-nux[0][1];  gz=nux[yp-4][2]-nux[0][2];
   pot=Math.sqrt(gx*gx+gy*gy+gz*gz);
                                  // pota=inner product of vectors N-H and N-ele5-7
   pota=((hpr[yp][0]-nux[0][0])*gx+(hpr[yp][1]-nux[0][1])*gy+(hpr[yp][2]-nux[0][2])*gz)/pot;
     for (int jou=0; jou <=2; jou++) {
    ggy=((nux[yp-4][jou]-nux[0][jou])*pota)/pot;
    ggz=ggy-(hpr[yp][jou]-nux[0][jou]);  // ggz=vector perpendicular to N-H line
    hpr2[yp][jou]=hpr[yp][jou]+2*ggz;
    }
    }

   toav=0.0;                // toav=total potential energy

     double ppot;
   for (int yp=0; yp <=4; yp++) {          // interaction among electrons 0-4       
    for (int kj=0; kj <=4; kj++) { 
    if (yp < kj ) {                     // kro= distance between two electrons (0-4)
    kro=Math.sqrt((hpr[yp][0]-hpr[kj][0])*(hpr[yp][0]-hpr[kj][0])+
   (hpr[yp][1]-hpr[kj][1])*(hpr[yp][1]-hpr[kj][1])+
   (hpr[yp][2]-hpr[kj][2])*(hpr[yp][2]-hpr[kj][2]));
   if (kro==0) {kro=5000.0;}
                             // ppot = repulsive potential energy (eV) between electrons (0-4)
     ppot=(elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14); 
    toav=toav+ppot;
                            // rhp[0-7][3]=potential energy of each electron (eV)
   rhp[yp][3]=rhp[yp][3]+ppot/2.0; rhp[kj][3]=rhp[kj][3]+ppot/2.0;
                           // teqq[0-4][3] = potential energy (eV) only in nitrogen atom
   teqq[yp][3]=teqq[yp][3]+ppot/2;  teqq[kj][3]=teqq[kj][3]+ppot/2;
    for (int jou=0; jou <=2; jou++) {
                            //ggx=force between two electrons (0-4)
   ggx=(suh*(hpr[yp][jou]-hpr[kj][jou]))/(kro*kro*kro);
                          // rhp[0-7][0-2]=force component of each electron
   rhp[yp][jou]=rhp[yp][jou]+ggx; rhp[kj][jou]=rhp[kj][jou]-ggx;
                          // teqq[0-5][0-2] = force component only in nitrogen atom     
   teqq[yp][jou]=teqq[yp][jou]+ggx; teqq[kj][jou]=teqq[kj][jou]-ggx;
    } 
   }}}  

    for (int yp=0; yp <=4; yp++) {    // interaction between ele (0-4) and ele (5-7) 
    for (int kj=5; kj <=7; kj++) { 
    kro=Math.sqrt((hpr[yp][0]-hpr[kj][0])*(hpr[yp][0]-hpr[kj][0])+
   (hpr[yp][1]-hpr[kj][1])*(hpr[yp][1]-hpr[kj][1])+
   (hpr[yp][2]-hpr[kj][2])*(hpr[yp][2]-hpr[kj][2]));
   if (kro==0) {kro=5000.0;}

                // kro2=distance between electrons (0-4) and symmetric positions of ele (5-7) 
     kro2=Math.sqrt((hpr[yp][0]-hpr2[kj][0])*(hpr[yp][0]-hpr2[kj][0])+
   (hpr[yp][1]-hpr2[kj][1])*(hpr[yp][1]-hpr2[kj][1])+
   (hpr[yp][2]-hpr2[kj][2])*(hpr[yp][2]-hpr2[kj][2]));
   if (kro2==0) {kro2=5000.0;}

    pot=(elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14); 
    pot2=(elc*elc*6.241509e18)/(4*pai*epsi*kro2*1.0e-14);
    rhp[yp][3]=rhp[yp][3]+pot/4.0+pot2/4.0; rhp[kj][3]=rhp[kj][3]+pot/2.0;
    toav=toav+pot;

     for (int jou=0; jou <=2; jou++) {
   ggx=(suh*(hpr[yp][jou]-hpr[kj][jou]))/(kro*kro*kro);
   ggy=(suh*(hpr[yp][jou]-hpr2[kj][jou]))/(kro2*kro2*kro2);
   rhp[yp][jou]=rhp[yp][jou]+ggx*0.5+ggy*0.5; rhp[kj][jou]=rhp[kj][jou]-ggx;
                    // rpp[0-4][0-2]= the sum of force acting on ele 0-4 from 4 nuclei and ele 5-7
   rpp[yp][jou]=rpp[yp][jou]+ggx*0.5+ggy*0.5;    
    }
    }}

   for (int yp=5; yp <=7; yp++) {            // interaction among electrons (5-7) 
    for (int kj=5; kj <=7; kj++) { 
     if (yp < kj ) {
    kro=Math.sqrt((hpr[yp][0]-hpr[kj][0])*(hpr[yp][0]-hpr[kj][0])+
   (hpr[yp][1]-hpr[kj][1])*(hpr[yp][1]-hpr[kj][1])+
   (hpr[yp][2]-hpr[kj][2])*(hpr[yp][2]-hpr[kj][2]));
   if (kro==0) {kro=5000.0;}

    pot=(elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14); 
   
    rhp[yp][3]=rhp[yp][3]+pot/2.0; rhp[kj][3]=rhp[kj][3]+pot/2.0;
    toav=toav+pot;

     for (int jou=0; jou <=2; jou++) {
   ggx=(suh*(hpr[yp][jou]-hpr[kj][jou]))/(kro*kro*kro);
   rhp[yp][jou]=rhp[yp][jou]+ggx; rhp[kj][jou]=rhp[kj][jou]-ggx;
    }
    }}}

    for (int rv=0; rv <=4; rv++) {     // interaction between noxx(center of ele 0-4) and ele0-4 
    kro=Math.sqrt((hpr[rv][0]-noxx[0])*(hpr[rv][0]-noxx[0])+
   (hpr[rv][1]-noxx[1])*(hpr[rv][1]-noxx[1])+
   (hpr[rv][2]-noxx[2])*(hpr[rv][2]-noxx[2]));
    if (kro == 0) {kro=5000.0;}   
       ppot=(elc*elc*den*6.241509e18)/(4*pai*epsi*kro*1.0e-14);
      teqq[rv][3]=teqq[rv][3]-ppot;   // teqq[0-4][3] = potential energy only in nitrogen atom
    for (int jou=0; jou <=2; jou++) {
     ggx=(suh*den*(hpr[rv][jou]-noxx[jou]))/(kro*kro*kro);
     teqq[rv][jou]=teqq[rv][jou]-ggx;  // teqq[0-4][0-2] = force component only in nitrogen atom
     }}

                                    // interaction between electrons and nuclei
   for (int rv=0; rv <=7; rv++) {    
                                              // ---------------- N nucleus (nux[0][0-2])
   kro=Math.sqrt((hpr[rv][0]-nux[0][0])*(hpr[rv][0]-nux[0][0])+
   (hpr[rv][1]-nux[0][1])*(hpr[rv][1]-nux[0][1])+
   (hpr[rv][2]-nux[0][2])*(hpr[rv][2]-nux[0][2])); kro2=1000.0;
    if (kro == 0) {kro=5000.0;}

                       // kro2 = distance between N nucleus and symmetric positions of ele 5-7
   if (rv > 4) {kro2=Math.sqrt((hpr2[rv][0]-nux[0][0])*(hpr2[rv][0]-nux[0][0])+
   (hpr2[rv][1]-nux[0][1])*(hpr2[rv][1]-nux[0][1])+
   (hpr2[rv][2]-nux[0][2])*(hpr2[rv][2]-nux[0][2])); 
    if (kro2 == 0) {kro2=5000.0;}
     }

    pot =-den/kro;          // ttav= potential energy between each electron (0-7) and N nucleus
   ttav= (elc*elc*6.241509e18*pot)/(4*pai*epsi*1.0e-14);
   rhp[rv][3]=rhp[rv][3]+ttav;  toav=toav+ttav;  
                 
                              // show distance (nuc) between electron 0-4 and N nucleus
   if (rv < 5) {ex=(int)(kro);  elp[rv][3].setText("nuc "+Integer.toString(ex));}
   for (int kj=0; kj <=2; kj++) {
   ggx=(suh*den*(hpr[rv][kj]-nux[0][kj]))/(kro*kro*kro); ggy=ggx;
   if (rv > 4) {ggy=(suh*den*(hpr2[rv][kj]-nux[0][kj]))/(kro2*kro2*kro2);}
                    
                              // mmp[0][0-2] = force component acting on N nucleus
   mmp[0][kj]=mmp[0][kj]+ggx/2.0+ggy/2.0;  rhp[rv][kj]=rhp[rv][kj]-ggx;
                  
                 // rpp[0-4][0-2]=the sum of force acting on ele 0-4 from 4 nuclei and ele 5-7
   if (rv < 5) {rpp[rv][kj]=rpp[rv][kj]-ggx; } 
   }
                                      // --------------------- H0 nucleus (nux[1][0-2])
   krr=Math.sqrt((hpr[rv][0]-nux[1][0])*(hpr[rv][0]-nux[1][0])+
    (hpr[rv][1]-nux[1][1])*(hpr[rv][1]-nux[1][1])+
    (hpr[rv][2]-nux[1][2])*(hpr[rv][2]-nux[1][2])); krr2=1000.0;
   if (krr ==0) {krr=5000.0;}

   if (rv > 4) {krr2=Math.sqrt((hpr2[rv][0]-nux[1][0])*(hpr2[rv][0]-nux[1][0])+
   (hpr2[rv][1]-nux[1][1])*(hpr2[rv][1]-nux[1][1])+
   (hpr2[rv][2]-nux[1][2])*(hpr2[rv][2]-nux[1][2])); 
    if (krr2 == 0) {krr2=5000.0;}
     }
   
    pota=-1.0/krr;
    ttav= (elc*elc*6.241509e18*pota)/(4*pai*epsi*1.0e-14);
   rhp[rv][3]=rhp[rv][3]+ttav;  toav=toav+ttav; 

                                     // show distance (nuc) between electron 5 and H0 nucleus
   if (rv==5) {ex=(int)(krr); elp[rv][3].setText("nuc "+Integer.toString(ex));}
   for (int kj=0; kj <=2; kj++) {
   ggx=(suh*(hpr[rv][kj]-nux[1][kj]))/(krr*krr*krr); ggy=ggx;
   if (rv > 4) {ggy=(suh*(hpr2[rv][kj]-nux[1][kj]))/(krr2*krr2*krr2);}

                                   // mmp[1][0-2] = force component acting on H0 nucleus
   mmp[1][kj]=mmp[1][kj]+ggx/2.0+ggy/2.0;  rhp[rv][kj]=rhp[rv][kj]-ggx;

   if (rv < 5) {rpp[rv][kj]=rpp[rv][kj]-ggx;}
   }

                                  // ---------------------------- H1 nucleus (nux[2][0-2])
   krk=Math.sqrt((hpr[rv][0]-nux[2][0])*(hpr[rv][0]-nux[2][0])+
   (hpr[rv][1]-nux[2][1])*(hpr[rv][1]-nux[2][1])+
   (hpr[rv][2]-nux[2][2])*(hpr[rv][2]-nux[2][2]));  krk2=1000.0;
   if (krk ==0) {krk=5000.0;} 

   if (rv > 4) {krk2=Math.sqrt((hpr2[rv][0]-nux[2][0])*(hpr2[rv][0]-nux[2][0])+
   (hpr2[rv][1]-nux[2][1])*(hpr2[rv][1]-nux[2][1])+
   (hpr2[rv][2]-nux[2][2])*(hpr2[rv][2]-nux[2][2])); 
    if (krk2 == 0) {krk2=5000.0;}
     }

    potb=-1.0/krk;   
    ttav= (elc*elc*6.241509e18*potb)/(4*pai*epsi*1.0e-14);
   rhp[rv][3]=rhp[rv][3]+ttav;  toav=toav+ttav; 
               
                            // show distance (nuc) between electron 6 and H1 nucleus 
   if (rv==6) {ex=(int)(krk);  elp[rv][3].setText("nuc "+Integer.toString(ex));}
   for (int kj=0; kj <=2; kj++) {
   ggx=(suh*(hpr[rv][kj]-nux[2][kj]))/(krk*krk*krk); ggy=ggx;
    if (rv > 4) {ggy=(suh*(hpr2[rv][kj]-nux[2][kj]))/(krk2*krk2*krk2);}
                           // mmp[2][0-2] = force component acting on H1 nucleus
   mmp[2][kj]=mmp[2][kj]+ggx/2.0+ggy/2.0;  rhp[rv][kj]=rhp[rv][kj]-ggx;

   if (rv < 5) {rpp[rv][kj]=rpp[rv][kj]-ggx;}
   }

                                    // ------------------------- H2 nucleus (nux[3][0-2])

   kkk=Math.sqrt((hpr[rv][0]-nux[3][0])*(hpr[rv][0]-nux[3][0])+
   (hpr[rv][1]-nux[3][1])*(hpr[rv][1]-nux[3][1])+
   (hpr[rv][2]-nux[3][2])*(hpr[rv][2]-nux[3][2]));  kkk2=1000.0;
   if (kkk ==0) {kkk=5000.0;} 

   if (rv > 4) {kkk2=Math.sqrt((hpr2[rv][0]-nux[3][0])*(hpr2[rv][0]-nux[3][0])+
   (hpr2[rv][1]-nux[3][1])*(hpr2[rv][1]-nux[3][1])+
   (hpr2[rv][2]-nux[3][2])*(hpr2[rv][2]-nux[3][2]));   
    if (kkk2 == 0) {kkk2=5000.0;}
     }

    potc=-1.0/kkk;   
    ttav= (elc*elc*6.241509e18*potc)/(4*pai*epsi*1.0e-14);
   rhp[rv][3]=rhp[rv][3]+ttav;  toav=toav+ttav; 
                               // show distance (nuc) between electron 7 and H2 nucleus
   if (rv==7) {ex=(int)(kkk);  elp[rv][3].setText("nuc "+Integer.toString(ex));}
   for (int kj=0; kj <=2; kj++) {
   ggx=(suh*(hpr[rv][kj]-nux[3][kj]))/(kkk*kkk*kkk); ggy=ggx;
    if (rv > 4) {ggy=(suh*(hpr2[rv][kj]-nux[3][kj]))/(kkk2*kkk2*kkk2);}
                             // mmp[3][0-2] = force component acting on H2 nucleus
   mmp[3][kj]=mmp[3][kj]+ggx/2.0+ggy/2.0;  rhp[rv][kj]=rhp[rv][kj]-ggx;

   if (rv < 5) {rpp[rv][kj]=rpp[rv][kj]-ggx;}
   }
   
                            // rhp[][5]= potential energy between electron and other nuclei
   if (rv < 5) {rhp[rv][5]=(elc*elc*6.241509e18*(pota+potb+potc))/(4*pai*epsi*1.0e-14); }
   if (rv == 5) {rhp[rv][5]=(elc*elc*6.241509e18*(pot+potb+potc))/(4*pai*epsi*1.0e-14);}
   if (rv == 6) {rhp[rv][5]=(elc*elc*6.241509e18*(pot+pota+potc))/(4*pai*epsi*1.0e-14);}
   if (rv == 7) {rhp[rv][5]=(elc*elc*6.241509e18*(pot+pota+potb))/(4*pai*epsi*1.0e-14);}
   }          
                              
    potc=0.0;
    for (int rv=0; rv <=3; rv++) {         // interaction among four nuclei
    for (int el=0; el <=3; el++) {
     if (rv > el) {
     kro=Math.sqrt((nux[rv][0]-nux[el][0])*(nux[rv][0]-nux[el][0])+
  (nux[rv][1]-nux[el][1])*(nux[rv][1]-nux[el][1])+
   (nux[rv][2]-nux[el][2])*(nux[rv][2]-nux[el][2]));
                                   // nux[rv][3] = positive charge of each nucleus
    ttav=(elc*elc*6.241509e18*nux[rv][3]*nux[el][3])/(4*pai*epsi*kro*1.0e-14);
                                // potc = repulsive potential energy among four nuclei
    toav=toav+ttav; potc=potc+ttav;
      for (int jou=0; jou <=2; jou++) {
                                 // ggx= repulsive force between nuclei
     ggx=(suh*nux[rv][3]*nux[el][3]*(nux[rv][jou]-nux[el][jou]))/(kro*kro*kro);
     mmp[rv][jou]=mmp[rv][jou]+ggx;  mmp[el][jou]=mmp[el][jou]-ggx; 
    }
   }   
   }}    

                                        
   ex=(int)(100*toav); ggx=ex/100.0;
   impho.setText("tV "+Double.toString(ggx));     // show total V to two decimal places
 
                      // distribute repulsive V among nuclei to each electron based on rhp[][5]
     double hiwa=0.0;
  for (int rv=0; rv <=7; rv++) { hiwa=hiwa+rhp[rv][5]; }
  for (int rv=0; rv <=7; rv++) {
  rhp[rv][3]=rhp[rv][3]+(potc*rhp[rv][5])/hiwa;
  
   ex=(int)(100*rhp[rv][3]); ggx=ex/100.0;
   elp[rv][4].setText("V "+Double.toString(ggx));    // show each electron's V (=rhp[][3])
   }

   gx=0.0;                       // gx=sum of each potential energy
   for (int rv=0; rv <=7; rv++) { gx=gx+rhp[rv][3]; }    
                               
   gy=-toav*0.5;             // gy=total kinetic energy         
                            // distribute kinetic energy to each electron based on rhp[][3]
   for (int rv=0; rv <=7; rv++) {       
    gz=(gy*rhp[rv][3])/gx; rhp[rv][4]=gz;    // rhp[][4]=each electron's kinetic energy
   ex=(int)(100*gz); gz=ex/100.0; elp[rv][5].setText("T "+Double.toString(gz)); 
    }               
   
    for (int rv=0; rv <=4; rv++) {        // show force component acting on elctrons 0-4 

    gx=Math.sqrt(rpp[rv][0]*rpp[rv][0]+rpp[rv][1]*rpp[rv][1]+rpp[rv][2]*rpp[rv][2]);
                     // gy=force component (CF) in the direction of rpp = inner product of rhp and rpp
    gy=(rhp[rv][0]*rpp[rv][0]+rhp[rv][1]*rpp[rv][1]+rhp[rv][2]*rpp[rv][2])/gx;
    ex=(int)(1000*gy); ww="CF ";
    elp[rv][6].setText(ww+Integer.toString(ex));
    for (int jou=0; jou <=2; jou++) {
    gz=rhp[rv][jou]-(gy*rpp[rv][jou])/gx;
    ex=(int)(1000*gz);       // show force component other than CF
    elp[rv][jou+7].setText(Integer.toString(ex));
     }
    }


  for (int rv=5; rv <=7; rv++) {      // show force component acting on electron 5-7
                         
                     // ggy=length of N-H vector 
   ggy=Math.sqrt((nux[0][0]-nux[rv-4][0])*(nux[0][0]-nux[rv-4][0])+
  (nux[0][1]-nux[rv-4][1])*(nux[0][1]-nux[rv-4][1])+(nux[0][2]-nux[rv-4][2])*(nux[0][2]-nux[rv-4][2]));   
                     // gy = inner product of vectors N-H and N-ele5-7
  gy=((nux[0][0]-hpr[rv][0])*(nux[0][0]-nux[rv-4][0])+
  (nux[0][1]-hpr[rv][1])*(nux[0][1]-nux[rv-4][1])+(nux[0][2]-hpr[rv][2])*(nux[0][2]-nux[rv-4][2]))/ggy;

    double ttj[][]=new double[3][4];
    for (int jou=0; jou <=2; jou++) {
                         // ttj[][0-2]=vector perpendicular to each N-H line
    ttj[rv-5][jou]=(gy*(nux[rv-4][jou]-nux[0][jou]))/ggy-(hpr[rv][jou]-nux[0][jou]);
     }

    ggz=Math.sqrt(ttj[rv-5][0]*ttj[rv-5][0]+ttj[rv-5][1]*ttj[rv-5][1]+ttj[rv-5][2]*ttj[rv-5][2]);
           // gz= force component (CF) in the direction of ttj = inner product of rhp and ttj 
    gz=(rhp[rv][0]*ttj[rv-5][0]+rhp[rv][1]*ttj[rv-5][1]+rhp[rv][2]*ttj[rv-5][2])/ggz;
    ex=(int)(1000*gz); ww="CF ";
   elp[rv][6].setText(ww+Integer.toString(ex));

    for (int jou=0; jou <=2; jou++) {     // show force component other than CF
     gx=rhp[rv][jou]-(gz*ttj[rv-5][jou])/ggz;
     ex=(int)(1000*gx); elp[rv][jou+7].setText(Integer.toString(ex));
     }
    }
                                               

    for (int rv=0; rv <=3; rv++) {
    for (int jou=0; jou <=2; jou++) {       // show mmpho[0-3][0-2]= force acting on each nucleus
    ex=(int)(1000*mmp[rv][jou]); ww=" ";
   if (jou==0) {ww="FX=";}
   if (jou==1) {ww="FY=";}
   if (jou==2) {ww="FZ=";}
    mmpho[rv][jou].setText(ww+Integer.toString(ex));
    }}

                         // ggy=distance between N and H0 nuclei
   ggy=Math.sqrt((nux[0][0]-nux[1][0])*(nux[0][0]-nux[1][0])+
  (nux[0][1]-nux[1][1])*(nux[0][1]-nux[1][1])+(nux[0][2]-nux[1][2])*(nux[0][2]-nux[1][2]));

                   // ggz=force component of H0 nucleus toward N nucleus 
  ggz=(mmp[1][0]*(nux[0][0]-nux[1][0])+mmp[1][1]*(nux[0][1]-nux[1][1])+mmp[1][2]*(nux[0][2]-nux[1][2]))/ggy;
  mmpho[1][3].setText("Nf"+Integer.toString((int)(1000*ggz)));
                                                                                      
                                 
   for (int rv=0; rv <=7; rv++) {         // show de Broglie wave of each electron
   
   gz=Math.sqrt(rhp[rv][0]*rhp[rv][0]+rhp[rv][1]*rhp[rv][1]+rhp[rv][2]*rhp[rv][2]);
   
                            // electrons 0-4 use forces (=teqq[0-4][0-2]) only in nitrogen atom
   if (rv < 5) {gz=Math.sqrt(teqq[rv][0]*teqq[rv][0]+teqq[rv][1]*teqq[rv][1]+teqq[rv][2]*teqq[rv][2]);
    }

   gy=(gz*elc*elc)/(4*pai*epsi*suh*1.0e-28);    // gy=force (N)
   gx=Math.sqrt((2*rhp[rv][4]*1.602177e-19)/me);  // gx=velocity (m/s) from kinetic energy

                         // electrons 0-4 use potential V (and T) only in nitrogen atom
   if (rv < 5) { gx=Math.sqrt((-teqq[rv][3]*1.602177e-19)/me); }

   ggx=(me*gx*gx)/gy;             // ggx= "tenporary" radius (m)
   ggy=(2*pai*ggx*me*gx)/h;       // ggy (wn) = number of de Broglie's waves contained in one orbit 
   
  ex=(int)(ggy*1000);  ggy=ex/1000.0;   // show wn to three decimal places
  elp[rv][10].setText("wn "+Double.toString(ggy));  }


                          // --------------------- show picture
   int nmx[][]=new int[4][3]; int hpk[][]=new int[8][4]; 

 
  for (int yp=0; yp <=3; yp++) {
  for (int kj=0; kj <=2; kj++) {     // change MM to pixel in nuclei
  nmx[yp][kj]=(int)(nux[yp][kj]/71.428);
  }}
  for (int yp=0; yp <=7; yp++) {
  for (int kj=0; kj <=2; kj++) {     // change MM to pixel in electrons
  hpk[yp][kj]=(int)(hpr[yp][kj]/71.428);
   if (hpk[yp][kj] > 349) {hpk[yp][kj]=349;}    // upper and lower limit
   if (hpk[yp][kj] < 1) {hpk[yp][kj]=1;}
  }}

  g.clearRect(9,299,1170,699);
  g.setColor(Color.cyan); g.drawLine(375,310,375,660); g.drawLine(735,310,735,660);

  g.setColor(Color.lightGray);         // show four nuclei
  g.fillOval(nmx[0][0]+10,650-nmx[0][1],20,20);g.fillOval(370+nmx[0][0],650-nmx[0][2],20,20);
  g.fillOval(730+nmx[0][1],650-nmx[0][2],20,20);
  g.fillOval(13+nmx[1][0],653-nmx[1][1],14,14);g.fillOval(373+nmx[1][0],653-nmx[1][2],14,14);
  g.fillOval(733+nmx[1][1],653-nmx[1][2],14,14);
  g.fillOval(13+nmx[2][0],653-nmx[2][1],14,14);g.fillOval(373+nmx[2][0],653-nmx[2][2],14,14);
  g.fillOval(733+nmx[2][1],653-nmx[2][2],14,14); 
  g.fillOval(13+nmx[3][0],653-nmx[3][1],14,14);g.fillOval(373+nmx[3][0],653-nmx[3][2],14,14);
  g.fillOval(733+nmx[3][1],653-nmx[3][2],14,14); 

  g.setColor(Color.white);           // show electron 0 (particle)
  g.fillOval(hpk[0][0]+13,653-hpk[0][1],14,14); 
  g.fillOval(hpk[0][0]+373,653-hpk[0][2],14,14);
  g.fillOval(hpk[0][1]+733,653-hpk[0][2],14,14);
  
                                      // show electron 1
  g.fillOval(hpk[1][0]+13,653-hpk[1][1],14,14); 
  g.fillOval(hpk[1][0]+373,653-hpk[1][2],14,14);
  g.fillOval(hpk[1][1]+733,653-hpk[1][2],14,14);
                                     
  g.setColor(Color.red);              // show electron 2
  g.fillOval(hpk[2][0]+13,653-hpk[2][1],14,14);
  g.fillOval(hpk[2][0]+373,653-hpk[2][2],14,14);
  g.fillOval(hpk[2][1]+733,653-hpk[2][2],14,14);
  
                                      // show electron 3
  g.fillOval(hpk[3][0]+13,653-hpk[3][1],14,14);
  g.fillOval(hpk[3][0]+373,653-hpk[3][2],14,14);
  g.fillOval(hpk[3][1]+733,653-hpk[3][2],14,14);
    
                                       // show electron 4
  g.setColor(Color.green);
  g.fillOval(hpk[4][0]+13,653-hpk[4][1],14,14);
  g.fillOval(hpk[4][0]+373,653-hpk[4][2],14,14);
  g.fillOval(hpk[4][1]+733,653-hpk[4][2],14,14);
    
                                        // show electron 5
  g.fillOval(hpk[5][0]+13,653-hpk[5][1],14,14);
  g.fillOval(hpk[5][0]+373,653-hpk[5][2],14,14);
  g.fillOval(hpk[5][1]+733,653-hpk[5][2],14,14);
   
                                       // show electron 6
  g.setColor(Color.pink);
  g.fillOval(hpk[6][0]+13,653-hpk[6][1],14,14);
  g.fillOval(hpk[6][0]+373,653-hpk[6][2],14,14);
  g.fillOval(hpk[6][1]+733,653-hpk[6][2],14,14);
   
                                        // show electron 7
  g.fillOval(hpk[7][0]+13,653-hpk[7][1],14,14);
  g.fillOval(hpk[7][0]+373,653-hpk[7][2],14,14);
  g.fillOval(hpk[7][1]+733,653-hpk[7][2],14,14);
  
    
  for (int rw=0; rw <=7; rw++) {         // show each electron's number
  g.setColor(Color.blue);
  g.drawString(Integer.toString(rw),hpk[rw][0]+17,665-hpk[rw][1]);
  g.drawString(Integer.toString(rw),hpk[rw][0]+377,665-hpk[rw][2] );
  g.drawString(Integer.toString(rw),hpk[rw][1]+737,665-hpk[rw][2] );
  }
   }
   }