/* * ga1.java - revised 27 Feb 08 - width 409, height 337 * @author jackord@kw.igs.net * fieldlines and equipotentials for an equilateral mass array */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class ga1 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]; 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 d=1; m[1]=1; m[2]=1; m[3]=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("M", p[i]-4, q[i]+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 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); x1=(int)(x); y1=(int)(y); fdln(x, y, g); } } g.setColor(Color.red); // Start Equipotentials for (int i=1; i<=11; i=i+1) { x=204; y=162+14*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, dxo, dyo; int tst=0; grav(x, y); dx=-gx*d; dy=-gy*d; do { dxo=dx; dyo=dy; 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; if ((x1<0)||(y1<0)||(x1>408)||(y1>336)||((dxo*dx<0)&&(dyo*dy<0))) { tst=1; } } while (tst==0); } 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, rcube; gx=0; gy=0; for (int i=1;i<=3; i=i+1) { rcube=Math.pow((x-p[i])*(x-p[i])+(y-q[i])*(y-q[i]), 1.5); gx=gx+m[i]*(p[i]-x)/rcube; gy=gy+m[i]*(q[i]-y)/rcube; } 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(); } }