/* * gc3.java - revised 15 Apr 05 - width 409, height 337 * @author jackord@kw.igs.net * magnetic fieldlines in the x-y plane due to * (1) a circular current-carrying loop centered in the x-z plane * (2) a north/south pole pair centered on the y axis * when "Far" is selected, the dimensions of the loop and pole pair * are reduced by a factor of ten below their plotted size */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class gc3 extends Applet implements ActionListener { double Bx, By, rb; int kk=0; // Declarations String bs = "Plot"; Button b = new Button(bs); Checkbox ch=new Checkbox("Far"); public void init() { // Layout setBackground(new Color(211, 211, 211)); ch.setBackground(getBackground()); add(b); add(ch); b.addActionListener(this); } public void pin(Graphics g) { // Initialize int r=3; g.setColor(Color.black); g.drawRect(0, 0, 408, 336); g.setColor(Color.blue); g.drawLine(204, 156, 204, 180); g.fillArc(204-r, 156-r, 2*r, 2*r, 0, 360); g.fillArc(204-r, 180-r, 2*r, 2*r, 0, 360); g.setColor(Color.red); g.fillRect(192, 167, 24, 3); } public void paint(Graphics g) { // Main Program double x, y, d, dx, dy, z; int x1, y1, x2, y2, r; rb=6; if (ch.getState()==true) { rb=rb/10; } d=0.5; r=12; pin(g); if (kk>0) { for (int j=1; j<=2; j=j+1) { for (int i=5; i<=175; i=i+10) { z=i*Math.PI/180; x=r*Math.cos(z); y=r*Math.sin(z); x1=(int)(2*x+204); y1=(int)(168-2*y); if (j==1) { bfld(x, y); } else { qfld(x,y); } dx=Bx*d; dy=By*d; do { if (j==1) { bfld(x+dx/2, y+dy/2); } else { qfld(x+dx/2, y+dy/2); } dx=Bx*d; dy=By*d; x=x+dx; y=y+dy; x2=(int)(2*x+204); y2=(int)(168-2*y); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } while (x*x+y*y>=r*r); } g.setColor(Color.blue); } } kk=0; } public void bfld(double x, double y) { // Calculate Bx, By double b, dt, r3, sn; // using Biot-Savart dt=Math.PI/30; // law Bx=0; By=0; for (int i=1; i<=60; i=i+1) { sn=Math.sin(i*dt); r3=Math.pow(x*x+y*y+rb*rb-2*x*rb*sn, 1.5); Bx=Bx+y*sn/r3; By=By+(rb-x*sn)/r3; } b=Math.sqrt(Bx*Bx+By*By); Bx=Bx/b; By=By/b; } public void qfld(double x, double y) { // Calculate Bx, By double b, r; // using inverse square r=Math.pow(x*x+(y-rb)*(y-rb), 1.5); // law Bx=x/r; By=(y-rb)/r; r=Math.pow(x*x+(y+rb)*(y+rb), 1.5); Bx=Bx-x/r; By=By-(y+rb)/r; 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(); } }