// $AISD_VERSION:NumDemo.java@1.2/home/coei/arc/archive%shell3.ba.best.com$ //COPYRIGHT: Copyright (c) 1997 by Christopher K. Oei, Incorporated. //COPYRIGHT: //COPYRIGHT: http://www.chrisoei.com //COPYRIGHT: info@chrisoei.com //COPYRIGHT: //COPYRIGHT: Permission to use, display, and modify this code //COPYRIGHT: is hereby granted provided that: //COPYRIGHT: //COPYRIGHT: 1) this notice is included in its entirety //COPYRIGHT: 2) we are notified of the usage import java.applet.*; import java.awt.Graphics; import java.awt.*; import RKDemo; import Rectangle; import Line; public class lev_mag extends Applet { int width=100; // the real width is 200 int height=100; // the real height is 200 int offsetx=50; int offsety=50; static int sig = -1; //Initial sign value double x1 = 350.0; double y1 = 30; double ysum; int go = 0; double tim = 0; double dtim=0.001; public double[] x; public double[][] values; public double[][] graph; static double qvalue = 0; static double betavalue = 18.; static double ystat = 400.; int maxtim = 1; int brectcx; int brectcy; double dist1; double dist2; double distt; double dists; double compx, compx1, compx2; double compy, compy1, compy2; double dummy = 0.; double rada = 100.; int x2 = 325; int y2 = 240; int numline = 2; int si = 0; int ki = 0; int max = 24; int sii = 0; int iv; double xolder; Scrollbar timeScroll = new Scrollbar(Scrollbar.HORIZONTAL,maxtim,1,1,10),betaScroll = new Scrollbar(Scrollbar.HORIZONTAL,(int)betavalue,1,3,50),qvalueScroll = new Scrollbar(Scrollbar.HORIZONTAL,(int)qvalue,1,0,100); public Label qvalueLabel = new Label("Friction = " + qvalue), betaLabel = new Label("Speed = " +betavalue), timeLabel = new Label("T = " +maxtim); Button restartbutton = new Button("Restart"), gobutton = new Button("Go"); Button signbutton = new Button("Flip Top Magnet"); public void init() { restartbutton.setForeground(Color.black); restartbutton.setBackground(Color.lightGray); signbutton.setForeground(Color.black); signbutton.setBackground(Color.lightGray); gobutton.setForeground(Color.black); gobutton.setBackground(Color.lightGray); timeScroll.setForeground(Color.black); timeScroll.setBackground(Color.lightGray); betaScroll.setForeground(Color.black); betaScroll.setBackground(Color.lightGray); qvalueScroll.setForeground(Color.black); qvalueScroll.setBackground(Color.lightGray); setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.CENTER)); p.add(gobutton); p.add(restartbutton); p.add(signbutton); add("North",p); Panel t = new Panel(); t.setLayout(new FlowLayout(FlowLayout.CENTER)); t.add(betaScroll); t.add(betaLabel); t.add(timeScroll); t.add(timeLabel); t.add(qvalueScroll); t.add(qvalueLabel); add("South",t); } public void Reset() { x1 = 350.0; y1 = 30; go = 0; tim = 0; repaint(); } public void Settvalue(String m) { maxtim = new Integer(m).intValue(); } public void Setqvalue(String m) { qvalue = new Double(m).doubleValue(); } public void Setbvalue(String m) { betavalue = new Double(m).doubleValue(); } public void SetGo() { go = 1; repaint(); } public void paint(Graphics g) { setBackground(java.awt.Color.white); // double values[] = {21.5,25.,30.}; // double values[] = {15.5,20.,25.}; double values[][] = { {15.5,20.,25.}, {12.5,19.,26.} }; x = new double[3]; graph = new double[2][(max+1)]; x[0]=3; x[1]=0; x[2]=0; double[] xold=new double[3]; xold[0]=x[0]; xold[1]=x[1]; xold[2]=x[2]; Rectangle rect = new Rectangle(15,35,(int)x1,(int)y1); RKDemo nint=new RKDemo(tim,x,.01); Line line=new Line(0.0,0.0,0.0,0.0); if(tim==0 && go==1) { while(tim 30.0 && dists > 30.0) // while(ij < 50 && distt > 30.0 ) { dist1 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - y1, 2)); dists = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - ystat, 2)); if (ij > 5) { distt=dist1; } else { distt=100.; } //compute components of dipole field of falling magnet compx1 = (3.*(line.sx-x1)*(line.sy-y1)/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2)+Math.pow(line.sy-y1,2),2.5) ; compy1 = ((2.*Math.pow(line.sy-y1,2)-Math.pow(line.sx-x1,2))/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2) + Math.pow(line.sy-y1,2),2.5); //compute components of dipole field of stationary magnet compx2 = (3.*(line.sx-x1)*(line.sy-ystat)/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2)+Math.pow(line.sy-ystat,2),2.5) ; compy2 = ((2.*Math.pow(line.sy-ystat,2)-Math.pow(line.sx-x1,2))/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2) + Math.pow(line.sy-ystat,2),2.5); compx=-(compx1+(double)sig*compx2); compy=-(compy1+(double)sig*compy2); line.emag = Math.sqrt(Math.pow(compx,2) + Math.pow(compy,2)); line.ex = line.sx + delarc * (compx/line.emag); line.ey = line.sy + delarc * (compy/line.emag); if(si != 0) { g.setColor(java.awt.Color.blue); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); g.drawLine(350-((int)line.sx-350),(int)line.sy,350-((int)line.ex-350),(int)line.ey); // draw lines from stationary magnet g.drawLine((int)line.sx, -(int)line.sy+(int)ysum, (int)line.ex, -(int)line.ey+(int)ysum); g.drawLine(350-((int)line.sx-350),-(int)line.sy+(int)ysum,350-((int)line.ex-350),-(int)line.ey+(int)ysum); g.setColor(java.awt.Color.black); } line.sx = line.ex; line.sy = line.ey; ij++; si++; } sii++; if(sii==15) { g.setColor(java.awt.Color.white); g.fillRect(0,0,700,500); sii=0; } g.setColor(java.awt.Color.black); iv++; } if( sig > 0) { // draw the three field lines from falling magnet going the other way iv=0; while(iv < 3) { if(sig<0) { line.ang = values[1][iv]*(Math.PI/180.); } else { line.ang = values[0][iv]*(Math.PI/180.); } line.sx = x1 + 5 * Math.sin(line.ang); line.sy = y1 + 5 * Math.cos(line.ang); distt=100.; dists=100.; double delarc = 9.; double intarc; int ij; double output1,output2; intarc = delarc/3.; ij = 0; si = 0; // draw field lines from falling magnet, at three different angles while(ij < 100 && distt > 30.0 && dists > 30.0) // while(ij < 50 && distt > 30.0 ) { dist1 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - y1, 2)); dists = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy - ystat, 2)); if (ij > 5) { distt=dist1; } else { distt=100.; } //compute components of dipole field of falling magnet compx1 = (3.*(line.sx-x1)*(line.sy-y1)/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2)+Math.pow(line.sy-y1,2),2.5) ; compy1 = ((2.*Math.pow(line.sy-y1,2)-Math.pow(line.sx-x1,2))/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2) + Math.pow(line.sy-y1,2),2.5); //compute components of dipole field of stationary magnet compx2 = (3.*(line.sx-x1)*(line.sy-ystat)/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2)+Math.pow(line.sy-ystat,2),2.5) ; compy2 = ((2.*Math.pow(line.sy-ystat,2)-Math.pow(line.sx-x1,2))/4.*Math.PI)/Math.pow(Math.pow(line.sx-x1,2) + Math.pow(line.sy-ystat,2),2.5); compx=(compx1+(double)sig*compx2); compy=(compy1+(double)sig*compy2); line.emag = Math.sqrt(Math.pow(compx,2) + Math.pow(compy,2)); line.ex = line.sx + delarc * (compx/line.emag); line.ey = line.sy + delarc * (compy/line.emag); if(si != 0) { g.setColor(java.awt.Color.blue); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); g.drawLine(350-((int)line.sx-350),(int)line.sy,350-((int)line.ex-350),(int)line.ey); // draw lines from stationary magnet g.drawLine((int)line.sx, -(int)line.sy+(int)ysum, (int)line.ex, -(int)line.ey+(int)ysum); g.drawLine(350-((int)line.sx-350),-(int)line.sy+(int)ysum,350-((int)line.ex-350),-(int)line.ey+(int)ysum); g.setColor(java.awt.Color.black); } line.sx = line.ex; line.sy = line.ey; ij++; si++; } sii++; if(sii==15) { g.setColor(java.awt.Color.white); g.fillRect(0,0,500,500); sii=0; } g.setColor(java.awt.Color.black); iv++; } } g.setColor(java.awt.Color.white); g.drawLine(rect.cx-rect.rw,(int)ystat-rect.rh+2,rect.cx-rect.rw,(int)ystat+rect.rh+2); g.drawOval(rect.cx-rect.rw,(int)ystat-rect.rh,(2*rect.rw),4); g.drawLine(rect.cx+rect.rw,(int)ystat-rect.rh+2,rect.cx+rect.rw,(int)ystat+rect.rh+2); g.drawOval(rect.cx-rect.rw,(int)ystat+rect.rh,(2*rect.rw),4); g.setColor(java.awt.Color.black); g.drawLine(rect.cx-rect.rw,(int)ystat-rect.rh+2,rect.cx-rect.rw,(int)ystat+rect.rh+2); g.drawOval(rect.cx-rect.rw,(int)ystat-rect.rh,(2*rect.rw),4); g.drawLine(rect.cx+rect.rw,(int)ystat-rect.rh+2,rect.cx+rect.rw,(int)ystat+rect.rh+2); g.drawOval(rect.cx-rect.rw,(int)ystat+rect.rh,(2*rect.rw),4); brectcx = rect.cx; brectcy = rect.cy; nint.iterate(); xold[0]=nint.x[0]; xold[1]=nint.x[1];xold[2]=nint.x[2]; tim+=dtim; xolder = xold[1] * 9; if(xolder > 45) { xolder = 45; } if(xolder < -45) { xolder = -45; } // g.drawLine(438,168,(438+(int)xolder),115); // g.drawLine(398,168,(398+(int)xolder),115); // do line plot at bottom graph[0][ki] = 25.+(tim*40); // graph[1][ki] = 449. - Math.pow(xold[1],2); graph[1][ki] = 400. - 4.*xold[1]; g.setColor(Color.red); g.setColor(Color.white); // g.fillOval((int)graph[0][ki],(int)graph[1][ki],3,3); ki++; //System.out.println("ki = " +ki ); //System.out.println("Xcoord = " +(int)(25.+(tim*40))); g.setColor(java.awt.Color.black); // g.drawOval(150,220,((2*(int)rada)-50),20); // g.drawOval(150,240,((2*(int)rada)-50),20); // g.drawLine(150,230,150,250); // g.drawLine(300,230,300,250); // g.drawRect(25,350,450,100); if (ki <= max) { int bki = ki; ki = 0; while (ki < bki) { if(graph[1][ki] < 350) {g.setColor(java.awt.Color.white);} else if(graph[1][ki] > 350) {g.setColor(java.awt.Color.white);} g.fillOval((int)graph[0][ki],(int)graph[1][ki],3,3); ki++; } ki = bki; } if(ki == max) { ki = 0; while (ki < max) { graph[0][ki] = graph[0][(ki+1)]-1; graph[1][ki] = graph[1][(ki+1)]; ki++; } ki = max -1; } } } g.setColor(java.awt.Color.black); //draw ring /* g.drawLine(125,230,125,250); g.drawLine(325,230,325,250); g.drawOval(125,220,(2*(int)rada),20); g.drawOval(125,240,(2*(int)rada),20); g.drawLine(150,230,150,250); g.drawLine(300,230,300,250); g.drawOval(150,220,((2*(int)rada)-50),20); g.drawOval(150,240,((2*(int)rada)-50),20); */ // draw moving magnet g.drawLine(rect.cx-rect.rw,rect.cy-rect.rh+2,rect.cx-rect.rw,rect.cy+rect.rh+2); g.drawOval(rect.cx-rect.rw,rect.cy-rect.rh,(2*rect.rw),4); g.drawLine(rect.cx+rect.rw,rect.cy-rect.rh+2,rect.cx+rect.rw,rect.cy+rect.rh+2); g.drawOval(rect.cx-rect.rw,rect.cy+rect.rh,(2*rect.rw),4); // draw stationary magnet g.drawLine(rect.cx-rect.rw,(int)ystat-rect.rh+2,rect.cx-rect.rw,(int)ystat+rect.rh+2); g.drawOval(rect.cx-rect.rw,(int)ystat-rect.rh,(2*rect.rw),4); g.drawLine(rect.cx+rect.rw,(int)ystat-rect.rh+2,rect.cx+rect.rw,(int)ystat+rect.rh+2); g.drawOval(rect.cx-rect.rw,(int)ystat+rect.rh,(2*rect.rw),4); // g.drawRect(25,350,450,100); } public boolean handleEvent(Event e) { Object target=e.target; if(target==timeScroll) { maxtim = timeScroll.getValue(); timeLabel.setText("T = " + maxtim); } if(target==qvalueScroll) { qvalue = qvalueScroll.getValue(); qvalueLabel.setText("Friction = " + qvalue); } if(target==betaScroll) { betavalue = betaScroll.getValue(); betaLabel.setText("Speed = " + betavalue); } if (e.id==Event.ACTION_EVENT) { if(target==restartbutton) { x1 = 350.0; y1 = 30; go = 0; tim = 0; go = 0; repaint(); } else if(target==signbutton) { sig = (-1*sig); repaint(); } else if(target==gobutton) { go = 1; repaint(); } } return super.handleEvent(e); } }