/*
forces.java
Copyright (c) 2001 by Michael F. Robbins.

Compares gravitational and electromechanical forces between
two protons, one of which is fixed, and the other which can move.


##############################################################
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################
*/


import java.awt.*;
import java.applet.*;

public class forces extends Applet
{
	// Double Buffering
	Image im;
	Graphics img;

	// Set up vars
	int point1x = 50;	// \ These say where the points are (left/right).
	int point2x = 100;	// /
	int point1y = 50;	// \ These two control how high the points are.
	int point2y = 50;	// / Make sure they are equal.

	int point2max = 450; // Can't go past here.
	
	int pxpernm = 100;	// Pixels per logical nanometer.
	
	float dist = 0;
	
	double gravforce = 0;	// Store the forces (N)
	double elecforce = 0;	// Store the forces (N)
	
	double valueG = 0;	// Store the G universal constant. (Nm^2 / kg^2)
	double valuek = 0;	// Store the k universal constant. (Nm^2 / C^2)
	
	double protonmass = 0;		// Store mass of a proton (kg)
	double protoncharge = 0;	// Store charge of a proton (C)
	
	
	
	public void init() {
		im = createImage(500, 200);
		img = im.getGraphics();
		
		valueG = (6.67 / (1e11));	// 6.67e-11
		valuek = (9.00 * (1e9));		// 9.00e9
		
		protonmass = (1.6726e-27);		// 1.6726e-27
		protoncharge = (1.6e-19);		// 1.6e-19
		
		trymove(100);
	}
	
	// Calculate the forces...
	public void calculate() {
		double d=0;
		Double dr = new Double(dist);
		
		
		// Get distance in nanometers...
		d = dr.doubleValue() / (double) (1e9);
	
		// First, calculate the gravitational force.
		// F=(G*m1*m2)/(d^2)
		gravforce = (valueG * protonmass * protonmass) / (d * d);
		
		
		// Then, the electromagnetic force.
		// F=(k*q1*q2)/(d^2)
		elecforce = (valuek * protoncharge * protoncharge) / (d * d);
		
	}
	
	// Where the display work happens...
	public void makeshow() {
		int i=0, j=0;
	
		// Do the calculations
		calculate();
	
		// Clear Screen
		img.setColor(Color.white);
		img.fillRect(0, 0, 500, 200);
		
		// Credits...
		img.setColor(Color.black);
		img.drawString("Copyright (c) 2001 by Michael F. Robbins under the GPL.", 5, 170);
		
		// Draw Line
		img.drawLine(point1x,point1y,point2max,point2y);
		
		// Draw Scale
		img.drawString("0nm", point1x + 0, point1y + 20);
		img.drawLine(point1x + (pxpernm * j), point1y, point1x + (pxpernm * j), point1y + 10);
		
		i = (int) Math.floor((point2max - point2y) / pxpernm);
		for(j=1; j <= i; j++) {
			img.drawLine(point1x + (pxpernm * j), point1y, point1x + (pxpernm * j), point1y + 10);
			img.drawString(Integer.toString(j).concat("nm"), point1x + (pxpernm * j), point1y + 20);
		}
		
		// Draw Points
		img.setColor(Color.green);
		img.fillOval(point1x - 4,point1y - 4, 8, 8);
		
		img.fillOval(point2x - 4,point2y - 4, 8, 8);

		// Draw Numbers
		img.setColor(Color.red);
		img.drawString("Distance: " + Float.toString(dist).concat(" nm"), 200, 20);
		
		img.setColor(Color.blue);
		img.drawString("Force of Electromagnetism: " + Double.toString(elecforce).concat(" N"), 50, 100);
		img.drawString("Force of Gravity: " + Double.toString(gravforce).concat(" N"), 50, 120);
		img.setColor(Color.red);
		img.drawString("Ratio between Forces: " + Double.toString((double) elecforce / gravforce), 50, 150);
		
		//img.drawString("G: " + Double.toString(valueG).concat(" Nm^2/kg^2"), 50, 160);
		//img.drawString("k: " + Double.toString(valuek).concat(" Nm^2/C^2"), 50, 180);

		//img.drawString("Proton Mass: " + Double.toString(protonmass).concat(" kg"), 50, 160);
		//img.drawString("Proton Charge: " + Double.toString(protoncharge).concat(" C"), 50, 180);
	}
	
	public void trymove(int x) {
		Integer d = null;
		Integer p = new Integer(pxpernm);
		
	
		if(x <= (point1x + 1)) x = (point1x + 1);
		if(x > point2max) x = point2max;
		
		if(point2x != x) {
			point2x = x;
			repaint();
		}
		
		// Calculate distance
		d = new Integer(point2x - point1x);
		dist = d.floatValue() / p.floatValue();
	}
	
	public boolean mouseDrag(Event e, int x, int y) {
		trymove(x);
		return true;
	}
	
	public boolean mouseDown(Event e, int x, int y) {
		trymove(x);
		return true;
	}
	
	public boolean mouseUp(Event e, int x, int y) {
		trymove(x);
		return true;
	}
	
	public void paint( Graphics g ) {
		// Create the buffer...
		makeshow();
		// Write the buffer to the screen
		g.drawImage(im, 0, 0, null);
	}

}
