import java.awt.*; import java.applet.*; import java.lang.Math; import java.awt.event.*; import java.awt.AWTEvent.*; import java.lang.Object; public class NucScreen extends Applet implements AdjustmentListener { private Image atomImage; //for the atom. private Graphics g_atomImage; private double size, distance, electronDensity, radiusSq, k1; private double nuclearDensity, k2, k3; private double highLightOffset, radiusHighLightOffsetSq, k4, k5; private double highLightDensity; private Scrollbar walker = new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,110); private int walkerValue; private Panel controls = new Panel(); private Label left = new Label(" Far away"); private Label right = new Label(" Very near"); private int electronIntensity, nuclearIntensity, highLightIntensity; private int redComponent, greenComponent, blueComponent; private Label copyRight = new Label("Copyright 2006, Peter H. Bird"); private Font def; public void init() //Entry point for the applet. { walker.addAdjustmentListener(this); //Listen to the scroll bar. setBackground(Color.white); atomImage = createImage(301,301); //Off screen image of the atom. controls.setLayout(new BorderLayout()); //Controls below atom image. setLayout(new BorderLayout()); g_atomImage = atomImage.getGraphics(); controls.add("West",left); controls.add("North",walker); controls.add("East",right); add("South",controls); k1 = 0.0002; k2 = 0.1; k3 = 0.01; k4 = 0.5; highLightOffset = 0.5*Math.sqrt(k2); //Nucleus highlight offset depends on nuclear size. makeAtomImage(g_atomImage); repaint(); } public void paint(Graphics g) { Font def; g.drawImage(atomImage,0,15,null); //Update the image of the atom def = g.getFont(); Font f = new Font(def.getName(),Font.PLAIN,9); g.setFont(f); g.drawString("Copyright 2006, Peter H. Bird",10,10); g.setFont(def); walker.addAdjustmentListener(this); // and turn the listener back on. } public void update(Graphics g) { paint(g); } public void adjustmentValueChanged(AdjustmentEvent evt) { walker.removeAdjustmentListener(this); //Turn off listener for now. walkerValue = walker.getValue(); //Read slider value. k1 = 0.0002/(1.0 + 0.1*(float)(walkerValue)); //Will go from 0.0002 to 1.67E-5 k2 = 10.0 + 0.818*(float)(walkerValue); // will go from 10 to 100 highLightOffset = 0.30*Math.sqrt(k2); k3 = 2.55*(float)(walkerValue); k4 = 0.3/(1.0 + 0.05*(float)(walkerValue)); makeAtomImage(g_atomImage); repaint(0,0,301,316); // System.out.println("walker value = "+Integer.toString(walkerValue)); } public void makeAtomImage(Graphics g_atom) { for (int i = 0; i <=300; i++) //Main loops for offscreen image. { for (int j = 0; j <= 300; j++) { radiusSq = (i - 150)*(i - 150) + (j - 150)*(j - 150); //Calculate radii. radiusHighLightOffsetSq = (i - 150 - highLightOffset)*(i - 150 - highLightOffset) + (j - 150 + highLightOffset)*(j - 150 + highLightOffset); electronDensity = Math.exp(-k1*radiusSq); //Calculate electron "density" electronIntensity = (int)(electronDensity*230.0); // using a guassian distribution. if (radiusSq <= k2) nuclearDensity = Math.sqrt(k2 - radiusSq); else nuclearDensity = 0.0; nuclearIntensity = (int)(k3*nuclearDensity/10.0); //Calculate nucleus using highLightDensity = Math.exp(-k4*radiusHighLightOffsetSq);// a spherical distribution. highLightIntensity = (int)(k3*highLightDensity/1.5); //Calculate nuclear highlight redComponent = electronIntensity + highLightIntensity; // using a gaussian distrib. if (redComponent > 255) redComponent = 255; //Calculate RGB colour intensities. greenComponent = electronIntensity - nuclearIntensity + highLightIntensity; if (greenComponent > 255) greenComponent = 255; blueComponent = electronIntensity - nuclearIntensity + highLightIntensity; if (blueComponent > 255) blueComponent = 255; g_atom.setColor(new Color(redComponent, greenComponent, blueComponent)); g_atom.drawLine(i,j,i+1,j); //Set pixel. } } } public String getAppletInfo() { return "Name: NucScreen\r\n" + "Author: Dr. P. Bird\r\n" + "Created with Microsoft Visual J++ Version 1.0" + "Copyright 2006"; } }