import java.util.Date; import java.applet.*; import java.awt.Graphics; import java.awt.event.*; import java.awt.*; import Rectangle; import Line; import Potential; public class magnetandloop extends Applet { double x1 = 50.0; double y1 = 250.; int go = 1; int lastx; double tim = 0; double dtim=0.01; static double qvalue = 10; static double betavalue = 200; double recentx[]; long recentt[]; double I_loop=0.; int max = 4; int j; int xcoord, ycoord; long lastt; int maxtim =1; int brectcx; int brectcy; double dist1; double dist2; double compx, compx1, compx2; double compy, compy1, compy2; double dummy = 0.; double rada = 100.; double ang=Math.PI/6.; int x2 = 325; int y2 = 240; int numline = 2; Rectangle rect; Line line; /* public void Reset() { x1 = 50.0; y1 = 250.; go = 1; tim = 0; repaint(); } public void Settvalue(String m) { maxtim = new Integer(m).intValue(); } public void Setqvalue(String m) { qvalue = new Double(m).doubleValue(); } public void Setbvalue(String m) { betavalue = new Double(m).doubleValue(); } public void SetGo() { go = 1; repaint(); } */ public void init() { setBackground(java.awt.Color.white); rect = new Rectangle(0,10,15,(int)x1,(int)y1); recentx = new double[max+1]; recentt = new long[max+1]; line=new Line(0.0,0.0,0.0,0.0); } public void paint(Graphics g) { //if(tim==0 && go==1) // if(go==1) // { //while(tim= -ang) { line.sx = x1 + 30 * Math.cos(line.ang); line.sy = y1 + 30 * Math.sin(line.ang); dist1 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - y1, 2)); double delarc = 9.; double intarc; intarc = delarc/3.; while(dist1 > 25.0 && line.sx < 450 && line.sx > 0 && line.sy < 500 && line.sy > 0 && line.emag > .00000001) { dist1 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - y1, 2)); //compute components of dipole field compy1 = (3.*(line.sx-x1)*(line.sy-y1)/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2)+Math.pow(line.sy-y1,2),2.5) ; compx1 = ((2.*Math.pow(line.sx-x1,2)-Math.pow(line.sy-y1,2))/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2) + Math.pow(line.sy-y1,2),2.5); //compute components of field due to ring, respectively //note we are dividing by lambda beta where lambda is set to 2 Potential pinto=new Potential(dummy,rada, line.sy-250., line.sx-225.); Potential pintx=new Potential(dummy,rada, line.sy-250., line.sx + intarc-225.); Potential pinty=new Potential(dummy,rada, line.sy + intarc-250.,line.sx-225.); compx2 = -0.01*I_loop*(pinty.Pota-pinto.Pota)/ intarc; compy2 =0.01*I_loop*(pintx.Pota-pinto.Pota)/ intarc; //note we are dividing comp2 by "a*a" to scale properly compx=compx1+compx2/rada; compy=compy1+compy2/rada; line.emag = Math.sqrt(Math.pow(compx,2) + Math.pow(compy,2)); line.ex = line.sx + delarc * (compx/line.emag); line.ey = line.sy + delarc * (compy/line.emag); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); line.sx = line.ex; line.sy = line.ey; } line.ang = line.ang - 2.*ang; } brectcx = rect.cx; brectcy = rect.cy; tim+=dtim; System.out.println("tim=" +tim); //g.setColor(java.awt.Color.white); //g.fillRect(0,0,450,500); g.setColor(java.awt.Color.black); g.drawOval(225,150,20,(2*(int)rada)); } // } } /* public boolean mouseMove(Event e, int x, int y) { xcoord = x; ycoord = y; return true; } */ public boolean mouseDown(Event e, int x, int y) { xcoord = x; ycoord = y; if(xcoord < (int)x1 + rect.rw && xcoord > (int)x1-rect.rw && ycoord < (int)y1+rect.rh && ycoord > (int)y1-rect.rh) { rect.hit = 1; } return true; } public boolean mouseUp(Event e, int x, int y) { rect.hit = 0; return true; } public boolean mouseDrag(Event e, int x, int y) { xcoord = x; ycoord = y; if (rect.hit == 1) { lastx = rect.cx; lastt = new Date().getTime(); if(j <= max) { recentx[j] = lastx; recentt[j] = lastt; rect.cx = x; x1 = rect.cx; } else if(j > max) { j = 0; while(j < max) { recentx[j] = recentx[(j+1)]; recentt[j] = recentt[(j+1)]; j++; } recentx[j] = lastx; recentt[j] = lastt; rect.cx = x; x1 = rect.cx; } j++; System.out.println("Recentx[0] = " + recentx[0] + " Recentx[1] = " + recentx[1] + " Recentx[2] = " + recentx[2]); System.out.println("Recentt[0] = " + recentt[0] + " Recentt[1] = " + recentt[1] + " Recentt[2] = " + recentt[2]); System.out.println("x=" + x + " y = " + y+ "I_loop " + I_loop); I_loop=recentx[max]-recentx[max-1]; repaint(); } return true; } }