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

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 hydd, so save this text editor as "hydd.java", and compile it.

In this program, the hydrogen nuclei are gray circles.
Here we use the new units, ( 1 MM = 10-14 meter).
Each coordinate of electrons (+X (MM), +Z (MM)) in the text box means "relative" position from these nuclei.
(ele 0 is from hydrogen nucleus 0 (H0), and ele 1 is from H1.)
"nuc (MM)" means the distance between these nuclei and electrons.
V (eV) T (eV), means the potential, and kinetic energies of each electron.
tV (eV) is the total potential energy.
(FX, FZ) means the force component acting on each particle (electrons or nuclei).
Waves (wn) is number of de Broglie's waves contained in one orbit.
You can change the coordinates (+X, +Z) of electrons freely.
(Enter the values into the textbox, and press the Enter key.)
And when you enter the value (MM) into the textbox next to "H-H" and press the Enter key, the internuclear distance between two nuclei change.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
public class hydd extends JPanel     // virial theorem of hydrogen molecule
 {
  public static void main(String arg[])
 {
   JFrame frame = new JFrame("H2 (hydrogen)");        // set frame
   J2DPanel j2dpanel = new J2DPanel();
   frame.getContentPane().add(j2dpanel); frame.setSize(900,500);
   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
 
    // textboxes:  elp=electrons, mmpho=nuclei, impho=tV, imppo=internuclear distance

    JTextField elp[][]=new JTextField[2][8];  JTextField impho=new JTextField(7); 
    JTextField mmpho[][]=new JTextField[2][2];   JTextField imppo=new JTextField(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[2][8];            // hpr[0,1][]=electron's parameters
    double nleng=7414.0;                       // nleng=internuclear distance
                                              // nux=each nuclear coordinates
    double nux[][]={{8793.0, 12500.00},{8793.0+nleng, 12500.0}}; 
                                                 
     double ina[]={890.0, 4665.0};        // ina=initial coordinate of electron

  public J2DPanel()
 {
  setBackground(Color.black);
  JPanel p=new JPanel();
  p.setLayout(new GridLayout(4,9));
  String vv; int aaa=0;                    
                                        
  String sihy[]={"eNo.", "+X (MM)", "+Z (MM)", "nuc (MM)", 
   "V (eV)", "T (eV)",  "FX ", "FZ ", "Waves "};

  for (int el=0; el <=8; el++) { p.add(new Label(sihy[el])); }
                         
  for (int el=0; el <=1; el++) {          // elp[][0-1], hpr[][0-1] = electron's coordiate
  for (int pos=0; pos <=1; pos++) {
  elp[el][pos]=new JTextField(7); elp[el][pos].addActionListener(this);
  hpr[el][pos]=0.0;      
  }}
                                   
   for (int el=0; el <=1; el++) {      // electron's other parameters
  for (int pos=2; pos <=7; pos++) {
  elp[el][pos]=new JTextField(7);     
  hpr[el][pos]=0.0; 
  }}

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

                                        // layout
 
  for (int el=0; el <=1; el++) {       
  p.add(new Label("ele "+el+" "));
  for (int pos=0; pos <=7; pos++) {
  p.add(elp[el][pos]);
  }}

   p.add(new Label("H0nuc "));
  for (int pos=0; pos <=1; pos++) {
  p.add(mmpho[0][pos]);  
  }
    p.add(new Label("H1nuc "));
  for (int pos=0; pos <=1; pos++) {
  p.add(mmpho[1][pos]);  
  }
 
   p.add(impho); p.add(new Label("H-H")); p.add(imppo); 

  imppo.addActionListener(this);
  imppo.setText(Integer.toString((int)nleng)); 

  add(p,"South");
  
   double nnuc=Math.sqrt(ina[0]*ina[0]+ina[1]*ina[1]);
   aaa=(int)(nnuc);                       // nnuc=distance between nucleus and electron
   
   for (int el=0; el <=1; el++) {
   elp[el][2].setText(Integer.toString(aaa)); 
    double sig=1.0;  if (el==1) {sig=-1.0;}
    for (int jou=0; jou <=1; jou++) {      // set and show initial coordinates
    hpr[el][jou]=nux[el][jou]+ina[jou]*sig;
    elp[el][jou].setText(Integer.toString((int)(ina[jou]*sig)));
     }}
   
   }     // public J2DPanel() end
 
    public void actionPerformed(ActionEvent e) {  
    String ss;
    double Rf1,Rf2; mar=0; int teis=0;

    for (int ett=0; ett <=1; ett++) {    // when electron's position changes
    for (int sws=0; sws <=1; sws++) {             
    
    if (e.getSource()==elp[ett][sws] ) { mar=1;
    ss=elp[ett][sws].getText(); Rf1=Double.parseDouble(ss); 
    Rf2=0.0;  
     Rf2=Rf1+nux[ett][sws];         // change relative coordinate to absolute coordinate
   
   if (sws==0) {                 // upper and lower limits of coordinates
   if (Rf1 >=nleng*0.5 && ett==0) {Rf1=nleng*0.5-1.0; Rf2=Rf1+nux[ett][sws]; teis=1;}  
   if (Rf1 <=-nleng*0.5 && ett==1) {Rf1=-nleng*0.5+1.0; Rf2=Rf1+nux[ett][sws]; teis=1;}
   }   
   if (Rf2 < 1.0) {Rf2=1.0; Rf1=Rf2-nux[ett][sws]; teis=1;}
   if (Rf2 > 24999.0) {Rf2=24999.0; Rf1=Rf2-nux[ett][sws]; teis=1;}    

    hpr[ett][sws]=Rf2; 
   if (teis==1) {elp[ett][sws].setText(Integer.toString((int)Rf1));}
    hpr[ett*-1+1][sws]=nux[ett*-1+1][sws]-Rf1;
    elp[ett*-1+1][sws].setText(Integer.toString((int)(-Rf1)));
     }
    }}
   
    if (e.getSource()==imppo) {mar=2; }    // mar=2 when internuclear distance changes

   repaint();
  }

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

  double kro,gx,gy,gz,ggx,ggy,ggz,ttav,toav,ppot; kro=0.0;
  int ex,ey,ez; String ww;
  double rhp[][]= {{0,0,0,0},{0,0,0,0}};  
  double mmp[][]={{0,0},{0,0}};  


   if (mar==2) {               // when internuclear distance changes    

    ww=imppo.getText(); ggz=Double.parseDouble(ww);
    if (ggz < 0) {ggz=ggz*-1.0; imppo.setText(Integer.toString((int)ggz));}
    ww=elp[1][0].getText(); ggx=Double.parseDouble(ww);
    
     if (nux[0][0]+ggz+ggx > 24999.0)  {ggz = 24999-ggx-nux[0][0]; 
     imppo.setText(Integer.toString((int)ggz));}

     if (ggz+ggx < ggz*0.5)  {ggz =-ggx*2.0+1.0; 
     imppo.setText(Integer.toString((int)ggz));}

      nux[1][0]=nux[0][0]+ggz; nleng=ggz;
      hpr[1][0]=ggx+nux[1][0];
    }
                                   

    toav=0.0;                // toav=total potential energy

                                   // interaction between two electrons
    kro=Math.sqrt((hpr[0][0]-hpr[1][0])*(hpr[0][0]-hpr[1][0])+
   (hpr[0][1]-hpr[1][1])*(hpr[0][1]-hpr[1][1]));
   if (kro==0) {kro=5000.0;}
     ppot=(elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14); 
    toav=toav+ppot;
                                              //rhp[][2]=electron's potential energy (eV)
   rhp[0][2]=rhp[0][2]+ppot/2.0; rhp[1][2]=rhp[1][2]+ppot/2.0;  

    for (int jou=0; jou <=2; jou++) {        // calculate force between electrons=rhp[][0-1]
   ggx=(suh*(hpr[0][jou]-hpr[1][jou]))/(kro*kro*kro);
   rhp[0][jou]=rhp[0][jou]+ggx; rhp[1][jou]=rhp[1][jou]-ggx;     
    } 
    
   for (int el=0; el <=1; el++) {        // interaction between electron and nucleus
   for (int rv=0; rv <=1; rv++) {  
   kro=Math.sqrt((hpr[el][0]-nux[rv][0])*(hpr[el][0]-nux[rv][0])+
   (hpr[el][1]-nux[rv][1])*(hpr[el][1]-nux[rv][1])); 
    if (kro == 0) {kro=5000.0;}         
                                        // elp[][2]=show distance between electron and nucleus
    if (el==rv) {elp[el][2].setText(Integer.toString((int)kro));}

    ppot= -(elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14);
   rhp[el][2]=rhp[el][2]+ppot;  toav=toav+ppot; 

     for (int kj=0; kj <=1; kj++) {     // mmp[][0-1]=force compoment acting on nuclei
   ggx=(suh*(hpr[el][kj]-nux[rv][kj]))/(kro*kro*kro); 
   mmp[rv][kj]=mmp[rv][kj]+ggx;  rhp[el][kj]=rhp[el][kj]-ggx;
   }               
   }}
  
                                        //  interaction between two nuclei
     kro=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]));
    ppot=(elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14);
    toav=toav+ppot;  
    rhp[0][2]=rhp[0][2]+ppot/2.0; rhp[1][2]=rhp[1][2]+ppot/2.0; 
      for (int jou=0; jou <=1; jou++) {
     ggx=(suh*(nux[0][jou]-nux[1][jou]))/(kro*kro*kro);
     mmp[0][jou]=mmp[0][jou]+ggx;  mmp[1][jou]=mmp[1][jou]-ggx; 
    }
                                   
   ex=(int)(100*toav); ggx=ex/100.0;
   impho.setText("tV "+Double.toString(ggx));         // show total V
 
   for (int el=0; el <=1; el++) {                // show each electron's V and T (eV)
   ex=(int)(100*rhp[el][2]); ggx=ex/100.0;
   elp[el][3].setText("V "+Double.toString(ggx));
   ex=(int)(100*rhp[el][2]*-0.5); ggx=ex/100.0;
   elp[el][4].setText("T "+Double.toString(ggx));
   }
       
   
    for (int el=0; el <=1; el++) {              
    for (int jou=0; jou <=1; jou++) {      // show force component acting on each electron
    ex=(int)(1000*rhp[el][jou]); 
    elp[el][jou+5].setText(Integer.toString(ex));
     }
    }                                               

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

                                                                                                                     
   for (int el=0; el <=1; el++) {         // show de Broglie wave
   
   gz=Math.sqrt(rhp[el][0]*rhp[el][0]+rhp[el][1]*rhp[el][1]);
   
   gy=(gz*elc*elc)/(4*pai*epsi*suh*1.0e-28);    // gy=force (N)
   gx=Math.sqrt((-rhp[el][2]*1.602177e-19)/me);  // gx=velocity (m/s)
   
   ggx=(me*gx*gx)/gy;                          // ggx="temporary" radius
   ggy=(2*pai*ggx*me*gx)/h;                     // ggy=number of de Broglie's waves
   ex=(int)(ggy*1000); ggy=ex/1000.0; 
   elp[el][7].setText("wn "+Double.toString(ggy));  }


                          // --------------------- show picture
   int nmx[][]=new int[2][2]; int hpk[][]=new int[2][2]; 

 
  for (int yp=0; yp <=1; yp++) {
  for (int kj=0; kj <=1; kj++) {     // change MM to pixel in nuclei
  nmx[yp][kj]=(int)(nux[yp][kj]/71.428);
  }}
  for (int yp=0; yp <=1; yp++) {
  for (int kj=0; kj <=1; kj++) {     // change MM to pixel in electrons
  hpk[yp][kj]=(int)(hpr[yp][kj]/71.428);
  }}

   g.clearRect(9,100,800,780);

   g.setColor(Color.cyan); 
   g.drawLine(nmx[0][0]+275,285, nmx[1][0]+275,285);

  g.setColor(Color.lightGray);         // show two nuclei
  g.fillOval(nmx[0][0]+265,450-nmx[0][1],20,20);
  g.fillOval(nmx[1][0]+265,450-nmx[1][1],20,20);

                                     
  g.setColor(Color.red);              // show electron 0
  g.fillOval(hpk[0][0]+268,453-hpk[0][1],14,14);
  
                                       // show electron 1
  g.setColor(Color.green);
   g.fillOval(hpk[1][0]+268,453-hpk[1][1],14,14);

    
  for (int rw=0; rw <=1; rw++) {         // show particle's number
  g.setColor(Color.white);
  g.drawString("H"+Integer.toString(rw),nmx[rw][0]+268,464-nmx[rw][1]);
  g.setColor(Color.blue);
  g.drawString(Integer.toString(rw),hpk[rw][0]+272,465-hpk[rw][1]);
  }
   }}