/* * gb1.java - revised 27 Feb 08 - width 409, height 337 * @author jackord@kw.igs.net * fieldlines and equipotentials for a -Q, 2Q, -Q charge array */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class gb1 extends Applet implements ActionListener { int d, x1, y1, x2, y2; int kk=0; // Declarations double gx, gy; int [] m=new int[4]; int [] p=new int[4]; int [] q=new int[4]; double [] r=new double[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 m[1]=-1; m[2]=2; m[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("Arial", Font.PLAIN, 12)); g.setColor(Color.gray); for (int i=1; i<=3; i=i+1) { g.fillArc(p[i]-12, q[i]-12, 25, 25, 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("2Q", p[2]-7, q[2]+5); g.drawString("-Q", p[1]-7, q[1]+5); g.drawString("-Q", p[3]-7, q[3]+5); g.drawRect(0, 0, 408, 336); } public void paint(Graphics g) { // Main Program double x, y, t; pin(g); if (kk>0) { // Start Fieldlines g.setColor(Color.blue); 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(x, y, g); } g.setColor(Color.red); // Start Equipotentials for (int i=-11; i<=11; i=i+2) { x=204; y=168+6*i; x1=(int)(x); y1=(int)(y); eqpt(x, y, g); } } kk=0; } public void fdln(double x, double y, Graphics g) { // Fieldline double dx, dy; grav(x, y); dx=-gx*d; dy=-gy*d; do { grav(x+dx/2, y+dy/2); 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; } while ((r[1]>1728) && (r[3]>1728)); } public void eqpt(double x, double y, Graphics g) { // Equipotential double dx, dy, xold, dxi; grav(x,y); dx=gy*d; dy=-gx*d; dxi=dx; do { grav(x+dx/2, y+dy/2); dx=gy*d; dy=-gx*d; xold=x; x=x+dx; y=y+dy; x2=(int)(x); y2=(int)(y); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } while (((xold-204)*dxi>=0) || ((x-204)*dxi<=0)); } public void grav(double x, double y) { // Calculate gx, gy double gg; gx=0; gy=0; for (int i=1; i<=3; i=i+1) { r[i]=Math.pow((x-p[i])*(x-p[i])+(y-q[i])*(y-q[i]), 1.5); gx=gx+m[i]*(p[i]-x)/r[i]; gy=gy+m[i]*(q[i]-y)/r[i]; } gg=Math.sqrt(gx*gx+gy*gy); gx=gx/gg; gy=gy/gg; } public void actionPerformed(ActionEvent e) { // Button String tst; tst=e.getActionCommand(); if (bs.equals(tst)) { kk=1; } repaint(); } }