import java.awt.*; import java.applet.*; import java.lang.*; public class rutherford extends Applet implements Runnable { int xLoc = 0; int yLoc = 0; int xNucl = 150; int yNucl = 150; double forceConst = 100000; double radius; double xSpeedInit = 9; double xSpeed = xSpeedInit; double ySpeed = 0; double time=0,delta_time=.01; int circleSize = 20; int nuclSize = 30; int appletSize = 300; Thread moveCircle=null; Rectangle clipRect; public void init() { setBackground(Color.white); resize(appletSize,appletSize); clipRect = new Rectangle(0,0,0,0); } public void paint(Graphics g) { clipRect = new Rectangle(xLoc,yLoc,circleSize,circleSize); g.setColor(Color.black); g.fillRect(0,0,appletSize,appletSize); g.setColor(Color.red); g.fillOval(xLoc,yLoc,circleSize,circleSize); g.setColor(Color.blue); g.fillOval(xNucl,yNucl,nuclSize,nuclSize); } public void update(Graphics g) { // comment out the next line to try repaint clipping g.clipRect(clipRect.x, clipRect.y, clipRect.width + 1, clipRect.height + 1); paint(g); } public void start() { if (moveCircle == null) { moveCircle = new Thread(this); moveCircle.start(); } } public void run() { while (moveCircle != null) { xLoc += xSpeed; yLoc += ySpeed; radius = Math.sqrt((xLoc-xNucl)*(xLoc-xNucl)+(yLoc-yNucl)*(yLoc-yNucl)); xSpeed +=forceConst*(xLoc-xNucl)/(radius*radius*radius)*delta_time; ySpeed +=forceConst*(yLoc-yNucl)/(radius*radius*radius)*delta_time; if ((xLoc + circleSize) > appletSize ||(xLoc + circleSize) <0) {xLoc=circleSize; yLoc=(int)Math.round(Math.random()*300);xSpeed=xSpeedInit;ySpeed=0;} if ((yLoc < 0) || ((yLoc + circleSize) > appletSize)) {xLoc=circleSize; yLoc=(int)Math.round(Math.random()*300);xSpeed=xSpeedInit;ySpeed=0;} clipRect = clipRect.union(new Rectangle(xLoc, yLoc, circleSize,circleSize)); if (xSpeed > 0){ showStatus("angle turned through = "+Math.abs((int)( 57.3*Math.atan((double)(-ySpeed/xSpeed)))) +" degrees");} else {showStatus("angle turned through = "+(180 -Math.abs((int)( 57.3*Math.atan((double)(-ySpeed/xSpeed))))) +" degrees");} repaint(); // comment this line out for repaint clipping //uncomment the next two lines to use repaint clipping //repaint(clipRect.x, clipRect.y, //clipRect.width + 1, clipRect.height + 1); try {Thread.sleep(50);} catch (InterruptedException e){} } } public void stop() { if (moveCircle != null) moveCircle.stop(); moveCircle = null; } public boolean mouseDown(Event evt, int x, int y){ {if(moveCircle == null) start(); else moveCircle = null;} return true; } }