/* * gc1.java - 27 Apr 08 - width 409, height 337 * @author jackord@kw.igs.net * magnetic fieldlines about parallel wires carrying * currents of -I, 2I, and -I */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class gc1 extends Applet implements ActionListener { int d, x1, y1, x2, y2; int kk=0; // Declarations double bx, by; int [] c=new int[4]; int [] p=new int[4]; int [] q=new int[4]; 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 c[1]=-1; c[2]=2; c[3]=-1; d=1; p[1]=121; q[1]=216; p[2]=204; q[2]=72; p[3]=287; q[3]=216; g.setFont(new Font("TimesRoman", Font.PLAIN, 14)); g.setColor(Color.gray); for (int i=1; i<=3; i=i+1) { g.fillArc(p[i]-12, q[i]-12, 24, 24, 0, 360); } g.setColor(Color.black); for (int i=1; i<=3; i=i+1) { g.drawArc(p[i]-12, q[i]-12, 24, 24, 0, 360); } g.drawString("2I", p[2]-6, q[2]+6); g.drawString("-I", p[1]-6, q[1]+6); g.drawString("-I", p[3]-6, q[3]+6); g.drawRect(0, 0, 408, 336); } public void paint(Graphics g) { // Main Program double x, y; pin(g); if (kk>0) { // Plot Fieldlines g.setColor(Color.blue); for (int i=-15; i<=11; i=i+1) { x=204; y=170-6*i; x1=(int)(x); y1=(int)(y); fdln(x, y, g); } } kk=0; } public void fdln(double x, double y, Graphics g) { // Fieldline double dx, dxi, dy, z; mfld(x, y); dx=-bx*d; dy=-by*d; dxi=dx; do { mfld(x+dx/2, y+dy/2); dx=-bx*d; dy=-by*d; z=x; x=x+dx; y=y+dy; x2=(int)(x+.5); y2=(int)(y+.5); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } while (((z-204)*dxi>=0)||((x-204)*dxi<0)); } public void mfld(double x, double y) { // Calculate bx, by double b, r2; bx=0; by=0; for (int i=1; i<=3; i=i+1) { r2=(x-p[i])*(x-p[i])+(y-q[i])*(y-q[i]); bx=bx+c[i]*(q[i]-y)/r2; by=by+c[i]*(x-p[i])/r2; } 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(); } }