Sample JAVA program to visualize electron's distribution of fluorine (F), chlorine (Cl).

Back to atomic radius 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 fluc, so save this text editor as "fluc.java", and compile it.
In some version of JAVA, some notes such as "-Xlint : unchecked ---" may appear on the screen, after you compile it.
But you can run it as it is, neglecting those messages.

In this program, the nitrogen nucleus (gray color) is at (12500MM, 12500MM, 12500MM).
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" positions from this nucleus.
(You can change this coordinates by entering the values into the text boxes, and press the Enter key.)
"nuc (MM)" means the distance between the nucleus and each electron. (All become automatically the same.)
"charge Z" is central positive charge of atom.
( You can change these "nuc" and "charge Z", by inputting arbitrary values into each textboxes and press the Enter key. )

V (eV) and T (eV) mean the potential and kinetic energies of each electron.
CF means the force toward the nucleus acting on each electron.
(fx, fy, fz) mean the each component of the "remaining" force other than CF.
(FX, FY, FZ) of the last row mean each component of the force acting on the nucleus.
(The unit of force: 1000 = the force between +e nucleus and an -e electron which are Bohr radius apart from each other)
tV of the last row is the total potential energy (eV).
Waves (wn) means the number of de Broglie's waves contained in one orbit of each electron.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
public class fluc extends JPanel     // virial theorem of F (fluorine)
 {
  public static void main(String arg[])
 {                                              // set frame
   JFrame frame = new JFrame("Fluorine (F)");
   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=5291.0*5291.0; // suh=(Bohr radius)^2
 
                    // elp[0-6][0-10] = Textboxes of each electron (0-6)
    JTextField elp[][]=new JTextField[7][11];  
    JTextField kat[]=new JTextField[12];    // bond angle text

    JTextField impho=new JTextField(7);    // total V text
    JTextField mmpho[]=new JTextField[3];  // nucleus text

            // imppo = distance between electron and nucleus text   
    JTextField imppo=new JTextField(7);    

    JTextField charge=new JTextField(7);   // charge text
   

    int marki[]=new int[3]; int mar=0;       // 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[7][11];  
    double den=7.3;   // den= central positive charge Z  
     
    double nux[]={12500.0, 12500.0, 12500.0};   // nux[0-2]=nuclear coordinate
                                             
    double ina1=4003.0;      // ina1= distance between nucleus and electron
 
    double rr=4003.0; double rrt=rr/rtw; double rrth=rr*0.5; 
    double rrf=rrth*rth;
                              // te1=initial coodinate of each electron
    double te1[][]={{rrt,-rrt, 0}, {rrt, rrt, 0}, 
    {-rrt, rrt, 0}, {-rrt, -rrt, 0}, 
    {0, 0, rr}, {-rrf, 0, -rrth}, 
    {rrf, 0, -rrth}};

  public J2DPanel()
 {
  setBackground(Color.black);
  JPanel p=new JPanel();
  p.setLayout(new GridLayout(10,12));
  String vv,zww; int aaa=0; 
                               
                     // hpr[0-6][0-2] = coordinate of each electron (0-6)
  for (int el=0; el <=6; el++) {     
  for (int pos=0; pos <=2; pos++) {
  elp[el][pos]=new JTextField(7); 
  if (el==0 || el==4 || el==5 ) {
  elp[el][pos].addActionListener(this);}
  hpr[el][pos]=0.0;      
  }}
                                   
   for (int el=0; el <=6; el++) {       // other parameters of each electron
  for (int pos=3; pos <=10; pos++) {
  elp[el][pos]=new JTextField(7);     
  hpr[el][pos]=0.0; 
  }}

      for (int el=0; el <=11; el++) {    // bond angle
    kat[el]= new JTextField(7);
     }
                          // mmpho[0-2] = force components acting on  nucleus
   for (int pos=0; pos <=2; pos++) {
    mmpho[pos]=new JTextField(7);
   }
                                        // layout

   String sihy[]={"eNo. ", "+X (MM)", "+Y (MM)", "+Z (MM)", "nux (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 <=6; el++) {   
  vv=" "+Integer.toString(el);
   if ( el==0 || el==4 || el==5 ) {
  vv="ele "+Integer.toString(el);
   }
    
  p.add(new Label(vv));
  for (int pos=0; pos <=10; pos++) {
  p.add(elp[el][pos]);
  }}

   p.add(new Label("nucleus "));
  for (int pos=0; pos <=2; pos++) {
  p.add(mmpho[pos]);  
  }

     p.add(new Label(" -- "));   
    p.add(impho);          // impho=textbox of total potential energy (tV)  

                        // charge = central charge text
    p.add(new Label(" Charge Z = "));    
    p.add(charge);  charge.addActionListener(this);

    p.add(new Label(" -- "));
    p.add(new Label(" nuc = "));
                      // imppo=textbox of nuc (distance)
    p.add(imppo);   
    imppo.addActionListener(this);
     
    p.add(new Label(" -- "));   

     p.add(new Label("angle "));
      for (int pos=0; pos <=10; pos++) {
       p.add(kat[pos]);  
      }
 
   add(p,"South");
  
   for (int el=0; el <=6; 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); 
                   // show distance between nuclei and electrons 
   elp[el][3].setText(Integer.toString(aaa));
    for (int jou=0; jou <=2; jou++) {
    hpr[el][jou]=te1[el][jou]+nux[jou]; 
                                              // show initial coordinate
    elp[el][jou].setText(Integer.toString((int)(te1[el][jou])));
     }}
   imppo.setText(Double.toString(ina1));
    charge.setText(Double.toString(den));
  }     // public J2DPanel() end
 
  
  public void actionPerformed(ActionEvent e) {     // when textboxes change
    String ss;  ss=" ";
    mar=0; 
    marki[0]=0; marki[1]=0;
    if (e.getSource()==imppo ) {mar=1;           // when "nuc" changes
    ss=imppo.getText(); ina1=Double.parseDouble(ss);  
   
     }
   
   repaint();
  }

  public void update(Graphics g)
 {
  paint(g);
 }
 public void paintComponent(Graphics g)
 {
   
  double kro,gx,gy,gz,ggx,ggy,ggz,ttav,toav;
  kro=0.0; gx=0.0;gy=0.0;gz=0.0;ggx=0.0; ggy=0.0; ggz=0.0;
  int ex,ey,ez; String ww;
  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}};
  
  double mmp[]={0,0,0};  
  
               // ---------------------  "nuc" = distance changes 

 if (mar==1) {         
     for (int yp=0; yp <=6; yp++) { 
      gx=Math.sqrt((hpr[yp][0]-nux[0])*(hpr[yp][0]-nux[0])+
  (hpr[yp][1]-nux[1])*(hpr[yp][1]-nux[1])+(hpr[yp][2]-nux[2])*(hpr[yp][2]-nux[2]));     
    for (int kj=0; kj <=2; kj++) { 
      gy=hpr[yp][kj]-nux[kj]; gy=(gy*ina1)/gx; hpr[yp][kj]=gy+nux[kj];
       elp[yp][kj].setText(Integer.toString((int)gy));
     }}  mar=0;
   }  
              //  ------------------  change each coordinate

   int bann; double ccoo,kakuu;
   for (int yp=0; yp <=2; yp++) { 
   for  (int jou=0; jou <=2; jou++) {
    bann=0;
    if (yp==1) {bann=4;}   if (yp==2)  {bann=5;}

    ww=elp[bann][jou].getText(); 
    hpr[bann][jou]=Double.parseDouble(ww);

   }}
                         // set ele 1,2,3 coordinate based on ele 0
     hpr[1][0]=hpr[0][0]; hpr[2][0]=-hpr[0][0]; hpr[3][0]=-hpr[0][0];
     hpr[1][1]=-hpr[0][1]; hpr[2][1]=hpr[1][1]; hpr[3][1]=hpr[0][1];
     hpr[1][2]=hpr[0][2]; hpr[2][2]=hpr[1][2]; hpr[3][2]=hpr[0][2];

                        // set ele 6 coordinate based on ele 5
     hpr[6][0]=-hpr[5][0]; hpr[6][1]=hpr[5][1]; hpr[6][2]=hpr[5][2];

                          // show each bond angle
      gx=Math.sqrt(hpr[0][0]*hpr[0][0]+hpr[0][1]*hpr[0][1]+hpr[0][2]*hpr[0][2]);
  
      for (int yp=0; yp <=2; yp++) { 
      bann=1; if (yp==1) {bann=3;}  if (yp==2) {bann=4;}
        gy=Math.sqrt(hpr[bann][0]*hpr[bann][0]+hpr[bann][1]*hpr[bann][1]+hpr[bann][2]*hpr[bann][2]);
        gz=Math.sqrt((hpr[bann][0]-hpr[0][0])*(hpr[bann][0]-hpr[0][0])+(hpr[bann][1]-hpr[0][1])*(hpr[bann][1]-hpr[0][1])+(hpr[bann][2]-hpr[0][2])*(hpr[bann][2]-hpr[0][2]));

      ccoo=(gx*gx+gy*gy-gz*gz)/(2.0*gx*gy);
      kakuu=Math.acos(ccoo)*180.0/pai;
        ww="0-n-" + Integer.toString(bann);
      kat[yp].setText(ww + " " +Integer.toString((int)kakuu));
       }

      for (int yp=0; yp <=1; yp++) {
       bann=yp+4; 
       gx=Math.sqrt(hpr[bann][0]*hpr[bann][0]+hpr[bann][1]*hpr[bann][1]+hpr[bann][2]*hpr[bann][2]);
       gy=Math.sqrt(hpr[bann+1][0]*hpr[bann+1][0]+hpr[bann+1][1]*hpr[bann+1][1]+hpr[bann+1][2]*hpr[bann+1][2]);
       gz=Math.sqrt((hpr[bann][0]-hpr[bann+1][0])*(hpr[bann][0]-hpr[bann+1][0])+(hpr[bann][1]-hpr[bann+1][1])*(hpr[bann][1]-hpr[bann+1][1])+(hpr[bann][2]-hpr[bann+1][2])*(hpr[bann][2]-hpr[bann+1][2]));

      ccoo=(gx*gx+gy*gy-gz*gz)/(2.0*gx*gy);
      kakuu=Math.acos(ccoo)*180.0/pai;
        ww= Integer.toString(bann)+  "-n-" + Integer.toString(bann+1);
      kat[yp+3].setText(ww + " " +Integer.toString((int)kakuu));
       }

                              // show each coordinate in textbox
      for (int yp=0; yp <=6; yp++) { 
      for (int kj=0; kj <=2; kj++) { 
      elp[yp][kj].setText(Integer.toString((int)hpr[yp][kj]));
      hpr[yp][kj]=hpr[yp][kj]+nux[kj];    
     }}  
                      
    ww=charge.getText();        // get charge value from text
   den=Double.parseDouble(ww);

   toav=0.0;                // toav=total potential energy

  double ppot;
   for (int yp=0; yp <=6; yp++) {     // ------ interaction between electrons  
   for (int kj=0; kj <=6; kj++) { 
    if (yp < kj ) {
                                        // kro=distance between electrons
    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) 
     ppot=(elc*elc*6.241509e18)/(4.0*pai*epsi*kro*1.0e-14); 
    toav=toav+ppot;
                                // rhp[][3]= potential energy of each electron

    rhp[yp][3]=rhp[yp][3]+ppot/2.0; rhp[kj][3]=rhp[kj][3]+ppot/2.0;
    for (int jou=0; jou <=2; jou++) {

                          // rhp[][0-2]=force component acting on each electron
   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;     
    } 
   }}}
                       // ----- interaction between nucleus and each electron
    for (int rv=0; rv <=6; rv++) {  

                                 // kro=distance between nucleus and electron  
    kro=Math.sqrt((hpr[rv][0]-nux[0])*(hpr[rv][0]-nux[0])+
   (hpr[rv][1]-nux[1])*(hpr[rv][1]-nux[1])+
   (hpr[rv][2]-nux[2])*(hpr[rv][2]-nux[2]));
    if (kro == 0) {kro=5000.0;}   
     ex=(int)(kro);  elp[rv][3].setText("nuc "+Integer.toString(ex));

                          // ppot = each attractive potential energy
       ppot=-(elc*elc*den*6.241509e18)/(4.0*pai*epsi*kro*1.0e-14);
     toav=toav+ppot; 
     rhp[rv][3]=rhp[rv][3]+ppot; 
    for (int jou=0; jou <=2; jou++) {  
                 // ggx=force component between nucleus and electron
     ggx=(suh*den*(hpr[rv][jou]-nux[jou]))/(kro*kro*kro);
     rhp[rv][jou]=rhp[rv][jou]-ggx; 
      mmp[jou]=mmp[jou]+ggx;     // mmp[0-2] = force acting on nucleus
     }}

   ex=(int)(100*toav); ggx=ex/100.0;
   impho.setText("tV "+Double.toString(ggx));       // show total V (eV)
    
    for (int rv=0; rv <=6; rv++) {      // show forces acting on electrons       
    gx=Math.sqrt((hpr[rv][0]-nux[0])*(hpr[rv][0]-nux[0])+
  (hpr[rv][1]-nux[1])*(hpr[rv][1]-nux[1])+(hpr[rv][2]-nux[2])*(hpr[rv][2]-nux[2]));
                                           // gy=forces toward nucleus (CF)
    gy=-(rhp[rv][0]*(hpr[rv][0]-nux[0])+rhp[rv][1]*(hpr[rv][1]-nux[1])+rhp[rv][2]*(hpr[rv][2]-nux[2]))/gx;
    ex=(int)(1000*gy); ww="CF ";
    elp[rv][6].setText(ww+Integer.toString(ex));

                                             // show  forces other than CF
    for (int jou=0; jou <=2; jou++) {
    gz=rhp[rv][jou]-(gy*(nux[jou]-hpr[rv][jou]))/gx;
    ex=(int)(1000*gz); 
    elp[rv][jou+7].setText(Integer.toString(ex));
     }
    }

    for (int jou=0; jou <=2; jou++) {       // show forces acting on nucleus
    ex=(int)(1000*mmp[jou]); ww=" ";
   if (jou==0) {ww="FX=";}
   if (jou==1) {ww="FY=";}
   if (jou==2) {ww="FZ=";}
    
    mmpho[jou].setText(ww+Integer.toString(ex));
    }
                 
                                                             
                      // show potential and kinetic energy of each electron 
  for (int rv=0; rv <=6; rv++) {
   ex=(int)(100*rhp[rv][3]); ggx=ex/100.0;
   elp[rv][4].setText("V "+Double.toString(ggx)); 

    rhp[rv][4] = -rhp[rv][3] * 0.5;
    ex=(int)(100*rhp[rv][4]); ggx=ex/100.0;
   elp[rv][5].setText("T "+Double.toString(ggx));
   }
                                                                                             
   for (int rv=0; rv <=6; rv++) {        // -----   show de Broglie wave
   
                             // gz = total force acting on each electron
   gz=Math.sqrt(rhp[rv][0]*rhp[rv][0]+rhp[rv][1]*rhp[rv][1]+rhp[rv][2]*rhp[rv][2]);
   
   gy=(gz*elc*elc)/(4.0*pai*epsi*suh*1.0e-28);    // gy=forces (N) 
 
                                  // gx=velocity (m/s) of each electron
   gx=Math.sqrt((2.0*rhp[rv][4]*1.602177e-19)/me);  
   
   ggx=(me*gx*gx)/gy;       // ggx= rotation radius  from centrifugal force

   double debroglie = h/(me*gx);
   ggy=(2*pai*ggx)/debroglie;   // ggy=number of de Broglie's wave in one orbit
  
  ex=(int)(ggy*1000);  ggy=ex/1000.0;
  elp[rv][10].setText("wn "+Double.toString(ggy));  }

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

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

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

   g.drawString("X - Y plane", 150,290);
   g.drawString("X - Z plane", 510,290);
   g.drawString("Y - Z plane", 870,290);

  g.setColor(Color.lightGray);         // show nucleus
  g.fillOval(nmx[0]+10,650-nmx[1],20,20);g.fillOval(370+nmx[0],650-nmx[2],20,20);
  g.fillOval(730+nmx[1],650-nmx[2],20,20);
  
  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);
    
  for (int rw=0; rw <=6; 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] );
  }
   }
   }