/* * gab.java - revised 11 Jan 10 - width 409, height 337 * @author jackord@kw.igs.net * Fieldlines, equipotentials, and colour potential plots * for equilateral arrays of masses or charges */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class gab extends Applet implements ActionListener { int kk=0; int flg, xo, yo; // Global variables double d, x, y, xx, yy, gx, gy; double [] m=new double[4]; double [] p=new double[4]; double [] q=new double[4]; double [] r=new double[4]; String b1s = "Lplot"; Button b1 = new Button(b1s); String b2s = "Cplot"; Button b2 = new Button(b2s); CheckboxGroup chg=new CheckboxGroup(); Checkbox[] ch=new Checkbox[2]; Color col[]={ Color.magenta, Color.blue, Color.red, Color.green, Color.yellow, Color.cyan }; public void init() { // Layout add(b1); add(b2); add(ch[0]=new Checkbox("Gfield", chg, true)); add(ch[1]=new Checkbox("Efield", chg, false)); b1.addActionListener(this); b2.addActionListener(this); setBackground(new Color(211, 211, 211)); ch[0].setBackground(getBackground()); ch[1].setBackground(getBackground()); } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { kk=1; } if (b2s.equals(tst)) { kk=2; } repaint(); } public void field() { // Calculate gx, gy double gg, rcube; gx=0; gy=0; for (int i=1; i<=3; i=i+1) { r[i]=Math.pow((xx-p[i])*(xx-p[i])+(yy-q[i])*(yy-q[i]), 1.5); gx=gx+m[i]*(p[i]-xx)/r[i]; gy=gy+m[i]*(q[i]-yy)/r[i]; } gg=Math.sqrt(gx*gx+gy*gy); gx=gx/gg; gy=gy/gg; } public void fdln(Graphics g) { // Fieldline double dx, dy, dxi, dyi; int x1, y1, x2, y2; int tst=0; xx=x; yy=y; x1=(int)(x); y1=(int)(y); field(); dx=-gx*d; dy=-gy*d; do { dxi=dx; dyi=dy; xx=x+dx/2; yy=y+dy/2; field(); dx=-gx*d; dy=-gy*d; x=x+dx; y=y+dy; x2=(int)(x); y2=(int)(y); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; if (flg==1) { if ((x<0)||(y<0)||(x>408)||(y>336)) { tst=1; } // Out of bounds if ((dxi*dx<0)&&(dyi*dy<0)) { tst=1; } // Direction reversal } else { if ((r[1]<1728) || (r[3]<1728)) { tst=1; } } } while (tst==0); } public void eqpt(Graphics g) { // Equipotential double dx, dy, xi, dxi; int x1, y1, x2, y2; x1=(int)(x); y1=(int)(y); xx=x; yy=y; field(); dx=gy*d; dy=-gx*d; dxi=dx; do { xx=x+dx/2; yy=y+dy/2; field(); dx=gy*d; dy=-gx*d; xi=x; x=x+dx; y=y+dy; x2=(int)(x); y2=(int)(y); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } while (((xi-xo)*dxi>=0)||((x-xo)*dxi<0)); } public void paint(Graphics g) { // Main Program ch[0].setBounds(293, 313, 55, 20); // Screen. . ch[1].setBounds(348, 313, 55, 20); b1.setBounds(5, 313, 35, 20); b2.setBounds(45, 313, 35, 20); // . .layout double r2, u, t; int plt, x1, y1, s; if (ch[0].getState()==true) { flg=1; } else { flg=2; } g.setFont(new Font("Arial", Font.BOLD, 15)); d=1; xo=204; yo=168; s=166; p[1]=xo-s/2; q[1]=yo+s/2/Math.sqrt(3); // Define. . p[2]=xo; q[2]=yo-s/Math.sqrt(3); p[3]=xo+s/2; q[3]=q[1]; if (flg==1) { m[1]=1; m[2]=1; m[3]=1; } else { m[1]=-1; m[2]=2; m[3]=-1; } // . .arrays for (int i=1; i<=3; i=i+1) { // Initial. . x1=(int)(p[i]); y1=(int)(q[i]); g.setColor(Color.white); g.fillArc(x1-12, y1-12, 25, 25, 0, 360); g.setColor(Color.black); g.drawArc(x1-12, y1-12, 24, 24, 0, 360); if (flg==1) { g.drawString("M", x1-6, y1+6); } else { if (i==2) { g.drawString("2Q", x1-10, y1+6); } else { g.drawString("-Q", x1-8, y1+6); } } } g.drawRect(0, 0, 2*xo, 2*yo); // . .screen if (kk==1) { if (flg==1) { // Gravity g.setColor(Color.red); // Start Fieldlines for (int j=1; j<=3; j=j+1) { for (int i=1; i<=24; i=i+1) { t=(2*i-1)*Math.PI/24; x=p[j]+12*Math.cos(t); y=q[j]+12*Math.sin(t); fdln(g); } } g.setColor(Color.blue); // Start Equipotentials for (int i=1; i<=11; i=i+1) { x=xo; y=yo-6+14*i; eqpt(g); } } else { // Electrostatics g.setColor(Color.red); // Start Fieldlines for (int i=1; i<=24; i=i+1) { t=(2*i-1)*Math.PI/24; x=p[2]+12*Math.cos(t); y=q[2]+12*Math.sin(t); x1=(int)(x); y1=(int)(y); fdln(g); } g.setColor(Color.blue); // Start Equipotentials for (int i=-11; i<=11; i=i+2) { x=xo; y=yo+6*i; x1=(int)(x); y1=(int)(y); eqpt(g); } } } if (kk==2) { // Colour potential plot for (int k=1; k<=335; k=k+1) { for (int j=1; j<=407; j=j+1) { u=0; plt=1; for (int i=1; i<=3; i=i+1) { r2=(p[i]-j)*(p[i]-j)+(q[i]-k)*(q[i]-k); if (r2>=144) { u=u+m[i]/Math.sqrt(r2); } else { plt=0; } } if (plt==1) { if (flg==1) { g.setColor(col[(int)(285.65*u)%6]); } // G field else { g.setColor(col[(int)(288*(.1+u))%6]); } // E field g.drawLine(j, k, j, k); } } } g.setColor(Color.black); for (int i=1; i<=3; i=i+1) { g.drawArc((int)(p[i])-12, (int)(q[i])-12, 24, 24, 0, 360); } } kk=0; } }