/* * gc2.java - revised 27 Apr 08 - width 409, height 337 * @author jackord@kw.igs.net * magnetic fieldlines in the x-y plane due to a circular * current-carrying loop centered in the x-z plane */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class gc2 extends Applet implements ActionListener { int phase; int kk=0; // Declarations double Bx, By; String bs = "Plot"; Button b = new Button(bs); public void init() { // Layout setLayout(new FlowLayout(FlowLayout.LEFT)); add(b); b.addActionListener(this); setBackground(new Color(211, 211, 211)); } public void pin(Graphics g) { // Initialize g.setColor(Color.black); g.drawRect(0, 0, 408, 336); g.setColor(Color.red); g.fillRect(132, 167, 144, 3); } public void paint(Graphics g) { // Main Program pin(g); if (kk>0) { g.setColor(Color.blue); // Plot Fieldlines for (int i=-13; i<=13; i=i+2) { phase=1; fdln(i, -14, g); } for (int i=-14; i<=-2; i=i+4) { phase=2; fdln(-17, i, g); } for (int i=-15; i<=-7; i=i+4) { phase=4; fdln(i, 0, g); } for (int i=-14; i<=-2; i=i+4) { phase=3; fdln(17, i, g); } for (int i=7; i<=15; i=i+4) { phase=4; fdln(i, 0, g); } } kk=0; } public void fdln(double x, double y, Graphics g) { // Fieldline double d, dx, dyi, dy, z; int x1, y1, x2, y2; int test=0; d=0.1; x1=(int)(12*(x+17)); y1=(int)(12*(14-y)); bfld(x, y); dx=Bx*d; dy=By*d; dyi=dy; do { bfld(x+dx/2, y+dy/2); dx=Bx*d; dy=By*d; z=y; x=x+dx; y=y+dy; x2=(int)(12*(x+17)); y2=(int)(12*(14-y)); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; if ((phase==1) && (y>14)) { test=1; } if ((phase==2) && (x<-17)) { test=1; } if ((phase==3) && (x>17)) { test=1; } if ((phase==4) && (z*dyi<0) && (y*dyi>0)) { test=1; } } while (test==0); } public void bfld(double x, double y) { // Calculate Bx, By double b, dt, r3, sn; int r=6; dt=Math.PI/100; Bx=0; By=0; for (int i=1; i<=200; i=i+1) { sn=Math.sin(i*dt); r3=Math.pow(x*x+y*y+r*r-2*x*r*sn, 1.5); Bx=Bx+y*sn/r3; By=By+(r-x*sn)/r3; } b=Math.sqrt(Bx*Bx+By*By); Bx=Bx/b; By=By/b; } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (bs.equals(tst)) { kk=1; } repaint(); } }