﻿ Carbon visualization program (+4.24)

### Sample JAVA program to visualize the electron's orbit of the Carbon (+4.25e) of Bohr model.

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 cavis2, so save this text editor as "cavis2.java", and compile it.
In this program, the central positive charge is supposed to be +4.25e instead of +4.0e.

In this program, the carbon nucleus (gray color) is at (9285.6MM, 12499MM, 11857MM).
Here we use the new units, ( 1 MM = 10-14 meter, 1 SS = 10-21 second, 1 MM/SS = 107 m/s ).
Each coordinate of electrons (+X (MM), +Y (MM), +Z (MM)) in the text box means "relative" position from this nucleus.
"nuc (MM)" means the distance between the nucleus and each electron.
"VX: VY: VZ" means the ratio of each component of the electron's initial velocity vector.
So, (VX: VY; VZ)=(100:100;0) is the same as (VX: VY: VZ) = (10:10:0).
When you choose the number (= wn) of de Broglie's waves (0.1 -- 2.0) in the scrollbar, and click "orbit" button, this program starts to compute the electron's orbit until de Broglie's waves contained in the orbit becomes wn.
And then, if you click "retur" button, all electrons return to the initial(starting) conditions.
You can change the coordinates and the direction of velocity vector freely.
(Enter the values into the textbox, and press the Enter key.)
And when you enter the value r (MM) into the textbox next to "radiu" button and click this button, the size (radius) of the four electrons' tetrahedron change.

``````
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
public class cavis2 extends JPanel     // visualization of carbon atom
{                                            // nuclear charge = 4.25
public static void main(String arg[])
{                                      // set frame
JFrame frame = new JFrame("visualization of oxygen");
J2DPanel j2dpanel = new J2DPanel();
frame.setSize(1180,700);
frame.setVisible(true);
frame.setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class J2DPanel extends JPanel implements AdjustmentListener, 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 den=4.25;     // den= nuclear charge=4.25
JTextField elp[][]=new JTextField[4][7];     // coordinate and velocity text
JTextField impho=new JTextField(7);
JButton b1=new JButton("orbit");            // button definition
JButton b2=new JButton("retur");

String ope[]={"0.1","0.3","0.5","0.7","0.9","1.1","1.3","1.5","1.7","1.9","2.0"};
JComboBox coom=new JComboBox(ope);          // de Broglie's wave selection

double rtw=Math.sqrt(2); double rth=Math.sqrt(3); double rsi=Math.sqrt(6);
double rfi=Math.sqrt(5); int marki[]={0,0,0,0,0,0,0,0};  // marki=marking
double hpr[][]=new double[4][7]; double hhpr[][]=new double[4][7];
double nux[]={9285.64, 12499.90, 11857.08};    // nux=nucleus coordinate (MM)

public J2DPanel()
{
setBackground(Color.black);
JPanel p=new JPanel();  p.setLayout(new GridLayout(7,8));
String vv,zww; int aaa=0;

for (int el=0; el <=3; el++) {     // elp[el][0-2]=each electron's coordinate
for (int pos=0; pos <=2; pos++) {
}}

for (int el=0; el <=3; el++) {
for (int pos=3; pos <=6; pos++) {  // elp[][3-6]=other parameterts of electron
elp[el][pos]=new JTextField(7);
}}
// layout

String mida[][]={{"Nucleus", "X = 9285.6", "Y = 12499", "Z = 11857",
"  ---  ", " --- ", "  --- ", "  --- "},
{"Electron","+ X (MM)","+ Y (MM)","+ Z (MM)", "nuc(MM)","  VX : ", "  VY : ", "  VZ : "}};
for (int ett=0; ett <=1; ett++) {
for (int pos=0; pos <=7; pos++) {

for (int el=0; el <=3; el++) {
for (int pos=0; pos <=6; pos++) {
}}

impho.setText("4890"); coom.setSelectedItem("2.0");

// hpr[0-3][0-2]=initial coordinates of electrons

double sd1=4890.0; double sd2=sd1/rtw;
hpr[0][0]=sd1+nux[0]; hpr[1][0]=nux[0]-sd1; hpr[2][0]=nux[0]; hpr[3][0]=nux[0];
hpr[0][1]=nux[1]; hpr[3][1]=nux[1]-sd1; hpr[1][1]=nux[1]; hpr[2][1]=nux[1]+sd1;
hpr[0][2]=nux[2]+sd2; hpr[1][2]=hpr[0][2]; hpr[2][2]=nux[2]-sd2; hpr[3][2]=hpr[2][2];

double zah1=0.0; int zai1=0;
for (int el=0; el <=3; el++) {
for (int pos=0; pos <=2; pos++) {            // calculate coordinate to two decimal places
// change to the relative coordinate
zai1=(int)(100*(hpr[el][pos]-nux[pos]));  zah1=zai1/100.0;
elp[el][pos].setText(Double.toString(zah1)); hhpr[el][pos]=hpr[el][pos];
}}
// initial velocity vector of electron

double velo[][]={{0,100,0},{0,-100,0},{-100,0,0},{100,0,0}};

for (int el=0; el <=3; el++) {
for (int pos=0; pos <=2; pos++) {
hpr[el][pos+4]=velo[el][pos]; hhpr[el][pos+4]=hpr[el][pos+4];
// set initial velocity vector of electron
elp[el][pos+4].setText(Double.toString((double)velo[el][pos]));
}}

double inik=0.0;                        // initial distance between nucleus and electron
for (int el=0; el <=3; el++) {
inik=Math.sqrt((hpr[el][0]-nux[0])*(hpr[el][0]-nux[0])+
(hpr[el][1]-nux[1])*(hpr[el][1]-nux[1])+(hpr[el][2]-nux[2])*(hpr[el][2]-nux[2]));
hpr[el][3]=inik; hhpr[el][3]=inik;
aaa=(int)(inik*100.0); inik=aaa/100.0;  // calculate the diatance to two decimal places
elp[el][3].setText(Double.toString(inik));
}
}  // method  end

repaint();
}

public void actionPerformed(ActionEvent e)  {
String ss;
int RR; double Rf1,Rf2,Rf3,Rf4; int teis=0;
for (int ett=0; ett <=3; ett++) {       // when coordinate texts change
for (int sws=0; sws <=2; sws++) {       // marki[2] becomes 2 only after orbiting
if (e.getSource()==elp[ett][sws] && marki[2] !=2 ) {
marki[4]=1;
ss=elp[ett][sws].getText(); Rf1=Double.parseDouble(ss);
Rf2=(Rf1+nux[sws])/71.428;   // plus nuclear coordinate and change to pixel
Rf3=Rf1+nux[sws]; hpr[ett][sws]=Rf3; hhpr[ett][sws]=Rf3;
// upper and lower limits of coordinate
if (Rf2 > 319 && sws==0) {Rf3=319*71.428;teis=1;}
if (Rf2 > 349 && sws==2) {Rf3=349*71.428;teis=1;}
if (Rf2 > 349 && sws==1) {Rf3=349*71.428;teis=1;}
if (Rf2 < 1) {Rf3=71.428;teis=1;}

RR=(int)(100*(Rf3-nux[sws]));  // change to relative coordinate
Rf1=RR/100.0;
if (teis==1) {elp[ett][sws].setText(Double.toString(Rf1));}
}}}

if (e.getSource() == b1 && marki[2] !=2) {marki[0]=1;}
if (e.getSource() == b3 && marki[2] !=2) {   // change radius of tetrahedron
marki[4]=1;                          // marki[4] becomes 1 when coordinates change
ss=impho.getText(); Rf1=Double.parseDouble(ss);
Rf2=Rf1/rtw;
hpr[0][0]=Rf1+nux[0]; hpr[1][0]=nux[0]-Rf1; hpr[2][0]=nux[0]; hpr[3][0]=nux[0];
hpr[0][1]=nux[1]; hpr[3][1]=nux[1]-Rf1; hpr[1][1]=nux[1]; hpr[2][1]=nux[1]+Rf1;
hpr[0][2]=nux[2]+Rf2; hpr[1][2]=hpr[0][2]; hpr[2][2]=nux[2]-Rf2; hpr[3][2]=hpr[2][2];

for (int ett=0; ett <=3; ett++) {
for (int sws=0; sws <=2; sws++) {          // calculate the values to two decimal places
RR=(int)(100*(hpr[ett][sws]-nux[sws])); Rf1=RR/100.0;
elp[ett][sws].setText(Double.toString(Rf1));
hhpr[ett][sws]=hpr[ett][sws];
}}
}
repaint();
}

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

public void paintComponent(Graphics g)  {

double kro,krr,krk,pot,pota,potb,gx,gy,gz; int ex,ey,ez,xk,yk,zk; String ww,pyw;
double rhp[][]= {{0,0},{0,0},{0,0},{0,0}};
int qpp[][]=new int[4][3];
double ggx,ggy,ggz; double ttav,toav; int tttav;

ggx=0.0; ex=0;
if (marki[2] != 2) {                       // get values from textfield
for (int ett=0; ett <=3; ett++) {
for (int sws=3; sws <=6; sws++) {
ww=elp[ett][sws].getText(); ggx=Double.parseDouble(ww);
hpr[ett][sws]=ggx; hhpr[ett][sws]=hpr[ett][sws];
}}}

for (int qz=0; qz <=3; qz++) {
for (int ka=0; ka <=6; ka++)      {
if (ka < 3) {ggx=hpr[qz][ka]-nux[ka]; ex=(int)(100*ggx); ggx=ex/100.0;}
else {ex=(int)(100*hpr[qz][ka]); ggx=ex/100.0; }
elp[qz][ka].setText(Double.toString(ggx));
hhpr[qz][ka]=hpr[qz][ka];
}}
marki[1]=0;  marki[2]=0;
}

if (marki[2] !=2) {           // before calculation of electron's orbit

toav=0.0;                    // toav =total potential energy
for (int yp=0; yp <=3; yp++) {   // calculate repulsive forces between electrons
for (int kj=0; kj <= 3; 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;}
// pot=potential energy between electrons (eV)
pot=(elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14);
// rhp[][0]=potential energy of each electron
rhp[yp][0]=rhp[yp][0]+pot/2;  rhp[kj][0]=rhp[kj][0]+pot/2;
toav=toav+pot;
}
} }

for (int rv=0; rv <=3; rv++) {
// kro=distance between C nucleus and electrons
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;}
hpr[rv][3]=kro; hhpr[rv][3]=kro;
ex=(int)(100.0*kro); ggx=ex/100.0;  // calculate to two decimal place
elp[rv][3].setText(Double.toString(ggx));  // show nuc(MM)

ttav= (-den*elc*elc*6.241509e18)/(4*pai*epsi*kro*1.0e-14);
rhp[rv][0]=rhp[rv][0]+ttav; toav=toav+ttav;   // add potential energy
}

//  distribute each kinetic energy based on each V
gy=-148.024/4.0;          // -148.024=total energy(eV) of carbon (of four valence electrons)
for (int rv=0; rv <=3; rv++) {
rhp[rv][1]=gy-rhp[rv][0];     // rhp[][1]=kinetic energy (eV)
}
}   //  if (marki[2] !=2 ) end

// ----------orbit calculation start  ----------
if (marki[0]==1) {    // put orbit button

double sj[][]=new double[4][13]; double wn=0.0; double nvx[]=new double[3];
String pew=(String)coom.getSelectedItem();
if (pew == "0.1") {wn=0.1;} if (pew == "0.3") {wn=0.3;}
if (pew == "0.5") {wn=0.5;} if (pew == "0.7") {wn=0.7;}
if (pew == "0.9") {wn=0.9;} if (pew == "1.1") {wn=1.1;}
if (pew == "1.3") {wn=1.3;} if (pew == "1.5") {wn=1.5;}
if (pew == "1.7") {wn=1.7;} if (pew == "1.9") {wn=1.9;}
if (pew == "2.0") {wn=2.0;}
for (int kj=0; kj <=2; kj++) {
nvx[kj]=nux[kj]*1.0e-14; }       // change MM to meter in C nucleus

for (int yp=0; yp <=3; yp++) {
sj[yp][9]=0.0;                   // sj[][9]=number of de Broglie's waves
gx=Math.sqrt(hpr[yp][4]*hpr[yp][4]+hpr[yp][5]*hpr[yp][5]+hpr[yp][6]*hpr[yp][6]);
gy=Math.sqrt((2.0*rhp[yp][1]*1.602177e-19)/me);  // gy=initial velocity (m/s)

for (int kj=0; kj <=2; kj++) {
sj[yp][kj] = hpr[yp][kj]; sj[yp][kj+6]=sj[yp][kj];  //sj[][0-2], sj[][6-8] =each coordinate
sj[yp][kj+3]=(gy*1.0e-7*hpr[yp][kj+4])/gx;    // sj[][3-5] =each component of velocity(MM/SS)
sj[yp][kj+10]=0.0;                  // sj[][10-12] =each component of acceleration (MM/SS^2)
}}

double ac=(elc*elc)/(4*pai*epsi*me);

do {
for (int yp=0; yp <=3; yp++) {
for (int kj=0; kj <=2; kj++) {
sj[yp][kj]=sj[yp][kj+6]+sj[yp][kj+3];  // electron's position(MM) after 1SS
sj[yp][10+kj]=0.0;
}}
for (int yp=0; yp <=3; yp++) {
gx=sj[yp][3]*sj[yp][3]+sj[yp][4]*sj[yp][4]+sj[yp][5]*sj[yp][5];
sj[yp][9]=sj[yp][9]+(me*gx*1.0e-7)/h;       // add de Brogli's waves
for (int kj=0; kj <=2; kj++) {
sj[yp][kj+6]=sj[yp][kj+6]*1.0e-14;        // change MM to meter
}}
for (int yp=0; yp <=3; yp++) {       // interaction between electrons
for (int kj=0; kj <=3; kj++) {
if (yp < kj) {                    // gy=distances between electrons
gy=Math.sqrt((sj[yp][6]-sj[kj][6])*(sj[yp][6]-sj[kj][6])+
(sj[yp][7]-sj[kj][7])*(sj[yp][7]-sj[kj][7])+(sj[yp][8]-sj[kj][8])*(sj[yp][8]-sj[kj][8]));
for (int rv=0; rv <=2; rv++) {
gx=(1.0e-28*ac*(sj[yp][rv+6]-sj[kj][rv+6]))/(gy*gy*gy);  // set acceleration(gx) at sj[][10-12]
sj[yp][rv+10]=sj[yp][rv+10]+gx ;sj[kj][rv+10]=sj[kj][rv+10]-gx ;
}}}}

for (int yp=0; yp <=3; yp++) {                   // gy=distances between electron and C nucleus
gy=Math.sqrt((sj[yp][6]-nvx[0])*(sj[yp][6]-nvx[0])+
(sj[yp][7]-nvx[1])*(sj[yp][7]-nvx[1])+(sj[yp][8]-nvx[2])*(sj[yp][8]-nvx[2]));
for (int rv=0; rv <=2; rv++) {
gx=(1.0e-28*den*ac*(sj[yp][rv+6]-nvx[rv]))/(gy*gy*gy); // gx=acceleration by nucleus
sj[yp][rv+10]=sj[yp][rv+10]-gx;
}}

for (int yp=0; yp <=3; yp++) {
for (int kj=0; kj <=2; kj++) {
sj[yp][kj+6]=sj[yp][kj];
sj[yp][kj+3]=sj[yp][kj+3]+sj[yp][kj+10];  // change velocity vector
}}

} while (sj[0][9] < wn );     // repeat above until de Broglie's waves becomes wn

marki[2]=2;                 // marki[2] becomes 2 only after orbit calculation
for (int yp=0; yp <=3; yp++) {
for (int kj=0; kj <=2; kj++) {
hhpr[yp][kj]=sj[yp][kj]; ggx=sj[yp][kj]/71.428;  // ggx=pixel

// upper and lower limits of coordinates
if (ggx > 319 && kj==0) {ggx=319;  hhpr[yp][kj]=ggx*71.428; }
if (ggx > 349 && kj==1) {ggx=349; hhpr[yp][kj]=ggx*71.428;}
if (ggx > 349 && kj==2) {ggx=349;  hhpr[yp][kj]=ggx*71.428;}
if (ggx < 1) {ggx=1; hhpr[yp][kj]=ggx*71.428;}

yk=(int)(100.0*(hhpr[yp][kj]-nux[kj])); gx=yk/100.0;  // calculate to two decimal places
elp[yp][kj].setText(Double.toString(gx));

ggx=Math.sqrt(sj[yp][3]*sj[yp][3]+sj[yp][4]*sj[yp][4]+sj[yp][5]*sj[yp][5]);
ggy=(1000*sj[yp][kj+3])/ggx; ex=(int)ggy; hhpr[yp][kj+4]=ggy;
elp[yp][kj+4].setText(Double.toString((double)ex));         // print each component of velocity
}
ggz=Math.sqrt((hhpr[yp][0]-nux[0])*(hhpr[yp][0]-nux[0])+
(hhpr[yp][1]-nux[1])*(hhpr[yp][1]-nux[1])+(hhpr[yp][2]-nux[2])*(hhpr[yp][2]-nux[2]));
hhpr[yp][3]=ggz;
ex=(int)(ggz*100.0); ggx=ex/100.0;  // calculate to two decimal place
elp[yp][3].setText(Double.toString(ggx));  // show distance between nucleus and electron
}
}   // if (mark[0]==1) end

if (marki[2] != 2 || marki[0]==1 ) {      // show each particle on the screen

for (int yp=0; yp <=3; yp++) {
pota=Math.sqrt(60)*4.25;              // velocity line length (pixel)
potb=Math.sqrt(hhpr[yp][4]*hhpr[yp][4]+hhpr[yp][5]*hhpr[yp][5]+hhpr[yp][6]*hhpr[yp][6]) ;
// qpp[][0-2]=each component of velocity for print
ggx=(pota*hhpr[yp][4])/potb; qpp[yp][0]=(int)ggx;
ggx=(pota*hhpr[yp][5])/potb; qpp[yp][1]=(int)ggx;
ggx=(pota*hhpr[yp][6])/potb; qpp[yp][2]=(int)ggx;
}

int nmx[]=new int[3]; int hpk[][]=new int[4][4];
for (int kj=0; kj <=2; kj++) {
nmx[kj]=(int)(nux[kj]/71.428);   // change to pixel in nucleus coordinate
}
for (int yp=0; yp <=3; yp++) {
for (int kj=0; kj <=2; kj++) {   // change to pixel in electron coordinate
hpk[yp][kj]=(int)(hhpr[yp][kj]/71.428);
}}
g.clearRect(9,299,1170,699);
g.setColor(Color.cyan); g.drawLine(345,310,345,660); g.drawLine(675,310,675,660);

g.setColor(Color.lightGray);       // show C nucleus on x-y, x-z, y-z planes
g.fillOval(nmx[0]+10,650-nmx[1],20,20);g.fillOval(340+nmx[0],650-nmx[2],20,20);
g.fillOval(670+nmx[1],650-nmx[2],20,20);

// show particle and velocity vector of electron 0
g.setColor(Color.white);
g.fillOval(hpk[0][0]+13,653-hpk[0][1],14,14);
g.fillOval(hpk[0][0]+343,653-hpk[0][2],14,14);
g.fillOval(hpk[0][1]+673,653-hpk[0][2],14,14);
g.drawLine(hpk[0][0]+20,660-hpk[0][1], hpk[0][0]+20+qpp[0][0],660-hpk[0][1]-qpp[0][1]);
g.drawLine(hpk[0][0]+350,660-hpk[0][2], hpk[0][0]+350+qpp[0][0],660-hpk[0][2]-qpp[0][2]);
g.drawLine(hpk[0][1]+680,660-hpk[0][2],hpk[0][1]+680+qpp[0][1],660-hpk[0][2]-qpp[0][2]);

// show electron 1
g.setColor(Color.yellow);
g.fillOval(hpk[1][0]+13,653-hpk[1][1],14,14);
g.fillOval(hpk[1][0]+343,653-hpk[1][2],14,14);
g.fillOval(hpk[1][1]+673,653-hpk[1][2],14,14);
g.drawLine(hpk[1][0]+20,660-hpk[1][1], hpk[1][0]+20+qpp[1][0],660-hpk[1][1]-qpp[1][1]);
g.drawLine(hpk[1][0]+350,660-hpk[1][2], hpk[1][0]+350+qpp[1][0],660-hpk[1][2]-qpp[1][2]);
g.drawLine(hpk[1][1]+680,660-hpk[1][2],hpk[1][1]+680+qpp[1][1],660-hpk[1][2]-qpp[1][2]);

// show electron 2
g.setColor(Color.red);
g.fillOval(hpk[2][0]+13,653-hpk[2][1],14,14);
g.fillOval(hpk[2][0]+343,653-hpk[2][2],14,14);
g.fillOval(hpk[2][1]+673,653-hpk[2][2],14,14);
g.drawLine(hpk[2][0]+20,660-hpk[2][1], hpk[2][0]+20+qpp[2][0],660-hpk[2][1]-qpp[2][1]);
g.drawLine(hpk[2][0]+350,660-hpk[2][2], hpk[2][0]+350+qpp[2][0],660-hpk[2][2]-qpp[2][2]);
g.drawLine(hpk[2][1]+680,660-hpk[2][2],hpk[2][1]+680+qpp[2][1],660-hpk[2][2]-qpp[2][2]);

// show electron 3
g.setColor(Color.green);
g.fillOval(hpk[3][0]+13,653-hpk[3][1],14,14);
g.fillOval(hpk[3][0]+343,653-hpk[3][2],14,14);
g.fillOval(hpk[3][1]+673,653-hpk[3][2],14,14);
g.drawLine(hpk[3][0]+20,660-hpk[3][1], hpk[3][0]+20+qpp[3][0],660-hpk[3][1]-qpp[3][1]);
g.drawLine(hpk[3][0]+350,660-hpk[3][2], hpk[3][0]+350+qpp[3][0],660-hpk[3][2]-qpp[3][2]);
g.drawLine(hpk[3][1]+680,660-hpk[3][2],hpk[3][1]+680+qpp[3][1],660-hpk[3][2]-qpp[3][2]);

// show each electron's number
for (int rw=0; rw <=3; rw++) {
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]+347,665-hpk[rw][2] );
g.drawString(Integer.toString(rw),hpk[rw][1]+677,665-hpk[rw][2] );
}
marki[0]=0;
}                  // if (marki[2] !=2 || marki[0] =1) end
}}

``````