I am currently writing a program to let the user build an atom in javafx, and i decided to show the user the valence electrons in an atom. I also thought it would be nice to make them spin, but I am running into some issues.
This is what I tried:
boolean protonsAreUnder1 = false;
if(numberOfProtons < 1){
protonsAreUnder1 = true;
}
if (!protonsAreUnder1){
//checks if # of protons > 0, or throws exception
int atomicNumber = numberOfProtons + numberOfNeutrons;
String[] elementInfo = getElementInfo(numberOfProtons);
String symbol = elementInfo[0];
String name = elementInfo[1];
String valenceElectronsString = elementInfo[2];
int valenceElectrons = Integer.parseInt(valenceElectronsString);
String atomicMassString = Integer.toString(atomicNumber);
symbolNumberLabel.setText(name + "(" + symbol + ") - " + atomicMassString);
//max radius for atom, based on the number of nucleons
double maxRadius = Math.sqrt(atomicNumber) * 2.5;
double valenceShellRadius = maxRadius + 20;
Circle valenceCircle = new Circle(150, 150, valenceShellRadius, Color.TRANSPARENT);
valenceCircle.setStroke(Color.BLACK );
valenceCircle.setStrokeWidth(2);
pane.getChildren().add(valenceCircle);
double angleBetweenElectrons = 2 * Math.PI / valenceElectrons;
for(int v = 0; v < valenceElectrons; v++){
double angle = v * angleBetweenElectrons;
double x = 150 + valenceShellRadius * Math.cos(angle);
double y = 150 + valenceShellRadius * Math.sin(angle);
Circle electron = new Circle(x, y, 3, Color.YELLOW);
electron.setStroke(Color.BLACK);
electron.setStrokeWidth(1);
pane.getChildren().add(electron);
//this is where i was thinking of putting the orbit code
//but i think this only rotates the electron by itself, and not around the atom
RotateTransition rotateTransition = new RotateTransition(Duration.seconds(1), electron);
rotateTransition.setByAngle(360); // Rotate 360 degrees
rotateTransition.setCycleCount(Animation.INDEFINITE);
rotateTransition.setInterpolator(Interpolator.LINEAR);
rotateTransition.play();
}
But when I run it, the electrons are not moving. I think they are just spinning around themselves.
New contributor
Enrico Sombini is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.