import java.applet.Applet; import java.awt.*; import java.awt.event.*; import Line.*; import Circle.*; public class myst_biot extends Applet { /* All of the below values can be changed, and any changes will be updated further on in the code automatically. However, you must be sure that if the value is initialized as a double, the new value you assign it is also a double NOTE: Certain variables have minimum values which they shouldn't fall below. For example, the minimum x-value of the applet is 22, because the program is told not to draw anything that goes beyond that point. Therefore, if you set the initial x position to below 22, you will probably see nothing when the applet is run. The minimum and maximum x/y values are sxpos,sypos,lxpos, and lypos, and they should only be altered if you are trying to change the overall dimensions of the applet */ // coordinates of source points double x1 = 100.; //Initial starting positions for double y1 = 200.0; //the two wires. double x2 = 300.0; double y2 = 200.0; // coordinates of component vector point double x3 = 100.0; double y3 = 150.0; double bconst = .0; double i1 = 1.; //Charges and masses for the two double i2 = .33; // wires double i3 = 1.00; double dt = 16.0; //Initial value for delta T int r1 = 20; //Radius of circles int r2 = 20; int r3 = 20; int dx = 20; //Initial value for delta X int sig = 1; //Initial sign value int ssval1 = 0; //Initial starting pos. for int ssval2 = dx/10; //the scrollbars int ssval3 = (int)i2; int width = 400; //Defines the dimensions of the int height = 400; //main canvas as variables so int ixpos = 22; //they can be easily changed int iypos = 37; int minx = 0; int maxx = 400; //Defines the min and max x and int miny = 0; //y values of the canvas, after int maxy = 200; //subtracting the north + south //borders boolean move = false; boolean drawline = false; /* The next 5 values are used to create and reference the invisible border that exists. */ int bord = 8; int sxpos = 0; int lxpos = 450; int sypos = (iypos + bord); int lypos = (iypos + height) - bord; /* The rest of the variables have no preset values, so they are just initialized and then given values later on */ int xcoord, ycoord; double dist1; double dist2; double compx; double compx1; double compx2; int ii; double compy; double compy1; double compy2; double emag1; double emag2; double ex1; double ex2; double ey1; double ey2; double squarefunc; double squarefunc1; double squarefunc2; double dx11; double dy11; double dx12; double dy12; double dx21; double dx22; double dy21; double dy22; double arrowlength; double arrowlength2 = 0; double arrowlength3 = 0; double angle = 20; double radangle = angle*(Math.PI/180); double sinangle = Math.sin(radangle); double cosangle = Math.cos(radangle); double max = .000003; double r; long red; long green; long blue; double dx1; double dx2; double dy1; double dy2; Line one; // Defines a name for the Lines Circle cone, ctwo, cthr; // Defines names for the Circles /* Adds scrollbars, labels, and buttons, while at the same time assigning handles to them */ Scrollbar dxScroll = new Scrollbar(Scrollbar.HORIZONTAL,ssval2,1,1,5),i2Scroll = new Scrollbar(Scrollbar.HORIZONTAL,1,1,1,10); Label dxLabel = new Label("Speed = "+dx); Button resetbutton = new Button("Reset"); /* Saves the initial values as backups so they can easily be reset to the starting values if neccasery */ double bx1 = x1; double by1 = y1; double bx2 = x2; double by2 = y2; double bx3 = x3; double by3 = y3; double bi1 = i1; double bi2 = i2; double bi3 = i3; int bdx = dx; int br2 = r2; int br3 = r3; int br1 = r1; int bsig = sig; public void init() { setBackground(Color.white); /*Sets up the background and foreground colors for both the buttons and the scrollbars */ resetbutton.setForeground(Color.black); resetbutton.setBackground(Color.lightGray); dxScroll.setForeground(Color.black); dxScroll.setBackground(Color.lightGray); i2Scroll.setForeground(Color.black); i2Scroll.setBackground(Color.lightGray); /*Creates a new layout manager, and groups the buttons and scrollbars into panels, which are then placed on the North and South sectors of the applet. */ setLayout(new BorderLayout()); Panel p = new Panel(); p.setLayout(new FlowLayout(FlowLayout.CENTER)); p.add(resetbutton); add("North",p); Panel t = new Panel(); t.setLayout(new FlowLayout(FlowLayout.CENTER)); t.add(dxScroll); t.add(dxLabel); add("South",t); /*Draws lines and circles using the syntax defined in their respective classes */ one = new Line(0.0,0.0,0.0,0.0); cone = new Circle(0,r1,(int)x1,(int)y1); ctwo = new Circle(0,r2,(int)x2,(int)y2); cthr = new Circle(0,r3,(int)x3,(int)y3); } public void update(Graphics g) { /*Overrides the update function to only redraw specified graphics */ if(move == true) { paint(g,one,cone, ctwo, cthr); move = false; } if(drawline == true) { drawline(g,one); } } public void paint(Graphics g) { paint(g,one,cone,ctwo,cthr); } public void paint(Graphics g,Line line,Circle circ, Circle circl, Circle circll) { /*Fills the main drawing canvas with black and then draws four white borders around the new drawn area. In order to make this fully customizable, the drawing uses variables defined in the beginning. */ g.setColor(Color.white); g.fillRect(ixpos,iypos,width,height); g.setColor(Color.white); g.fillRect(minx,miny,ixpos,(maxy-miny)); g.fillRect(ixpos,miny,width,(iypos - miny)); g.fillRect((ixpos + width),miny,(maxx - (ixpos + width)),(maxy - miny)); g.fillRect(ixpos,(iypos + height),width,(maxy - (iypos + height))); x1 = circ.cx; y1 = circ.cy; circ.cx = (int)x1; circ.cy = (int)y1; circ.sx = (int)(x1 - r1/2); circ.sy = (int)(y1 - r1/2); x2 = circl.cx; y2 = circl.cy; circl.cx = (int)x2; circl.cy = (int)y2; circl.sx = (int)(x2 - r2/2); circl.sy = (int)(y2 - r2/2); x3 = circll.cx; y3 = circll.cy; circll.cx = (int)x3; circll.cy = (int)y3; circll.sx = (int)(x3 - r3/2); circll.sy = (int)(y3 - r3/2); g.setColor(new Color(26,103,244)); // draw vector field int ix=0; int iy=0; int numy; int numx; numx=2+ width/dx; numy=2+ height/dx; while(ix255) {red=255;} if(green>255) {green=255;} if(blue>255) {blue=255;} if(red<0) {red=0;} if(green<0) {green=0;} if(blue<0) {blue=0;} arrowlength = 0; line.ey = line.sy + (compy/line.emag) * dx/2.; line.ex = line.sx + dx*(compx/line.emag)/2.; dy1 = 1*(arrowlength*((sinangle*compx-cosangle*compy)/squarefunc)); dx1 = -1*(arrowlength*((cosangle*compx+sinangle*compy)/squarefunc)); dy2 = 1*(arrowlength*((-sinangle*compx-cosangle*compy)/squarefunc)); dx2 = -1*(arrowlength*((cosangle*compx-sinangle*compy)/squarefunc)); g.setColor(new Color((int)red,(int)green,(int)blue)); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); g.drawLine((int)line.ex, (int)line.ey, (int)(line.ex + dx1),(int)(line.ey + dy1)); g.drawLine((int)line.ex, (int)line.ey, (int)(line.ex + dx2),(int)(line.ey + dy2)); iy++; } ix++; iy=0; } // draw component arrows and resultant /* line.sx = x3; line.sy = y3; dist1 = Math.sqrt(Math.pow(line.sx-x2, 2) + Math.pow(line.sy-y2, 2)); dist2 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy-y1, 2)); compy = -i2*(line.sx - x2) / Math.pow(dist1,3) - i1*(line.sx - x1) / Math.pow(dist2,3); compx = i2*(line.sy - y2) / Math.pow(dist1,3) + i1*(line.sy - y1) / Math.pow(dist2,3); compy2 = -i2*(line.sx - x2) / Math.pow(dist1,3); compx2 = i2*(line.sy - y2) / Math.pow(dist1,3); compy1 = -i1*(line.sx - x1) / Math.pow(dist2,3); compx1 = i1*(line.sy - y1) / Math.pow(dist2,3); squarefunc = .000030; squarefunc1 = squarefunc; squarefunc2 = squarefunc; line.emag = squarefunc; emag1 = squarefunc1; emag2 = squarefunc2; line.ex = line.sx + (compx/line.emag) * arrowlength3; line.ey = line.sy + (compy/line.emag) * arrowlength3; ex1 = line.sx + (compx1/emag1) * arrowlength3; ey1 = line.sy + (compy1/emag1) * arrowlength3; ex2 = line.sx + (compx2/emag2) * arrowlength3; ey2 = line.sy + (compy2/emag2) * arrowlength3; g.setColor(Color.blue); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); dy1 = 1*(arrowlength2*((sinangle*compx-cosangle*compy)/squarefunc)); dx1 = -1*(arrowlength2*((cosangle*compx+sinangle*compy)/squarefunc)); dy2 = 1*(arrowlength2*((-sinangle*compx-cosangle*compy)/squarefunc)); dx2 = -1*(arrowlength2*((cosangle*compx-sinangle*compy)/squarefunc)); dy11 = 1*(arrowlength2*((sinangle*compx1-cosangle*compy1)/squarefunc1)); dx11 = -1*(arrowlength2*((cosangle*compx1+sinangle*compy1)/squarefunc1)); dy21 = 1*(arrowlength2*((-sinangle*compx1-cosangle*compy1)/squarefunc1)); dx21 = -1*(arrowlength2*((cosangle*compx1-sinangle*compy1)/squarefunc1)); dy12 = 1*(arrowlength2*((sinangle*compx2-cosangle*compy2)/squarefunc2)); dx12 = -1*(arrowlength2*((cosangle*compx2+sinangle*compy2)/squarefunc2)); dy22 = 1*(arrowlength2*((-sinangle*compx2-cosangle*compy2)/squarefunc2)); dx22 = -1*(arrowlength2*((cosangle*compx2-sinangle*compy2)/squarefunc2)); g.drawLine((int)line.ex, (int)line.ey, (int)(line.ex + dx1),(int)(line.ey + dy1)); g.drawLine((int)line.ex, (int)line.ey, (int)(line.ex + dx2),(int)(line.ey + dy2)); g.setColor(Color.black); // g.setColor(new Color(0,190,0)); g.drawLine((int)line.sx, (int)line.sy, (int)x1, (int)y1); g.drawLine((int)line.sx, (int)line.sy, (int)ex1, (int)ey1); g.drawLine((int)ex1, (int)ey1, (int)(ex1 + dx11),(int)(ey1 + dy11)); g.drawLine((int)ex1, (int)ey1, (int)(ex1 + dx21),(int)(ey1 + dy21)); g.setColor(Color.red); g.drawLine((int)line.sx, (int)line.sy, (int)x2, (int)y2); g.drawLine((int)line.sx, (int)line.sy, (int)ex2, (int)ey2); g.drawLine((int)ex2, (int)ey2, (int)(ex2 + dx12),(int)(ey2 + dy12)); g.drawLine((int)ex2, (int)ey2, (int)(ex2 + dx22),(int)(ey2 + dy22)); */ // draw symbols for the line current locations g.setColor(Color.black); if(i2<0) { g.drawOval(circl.sx,circl.sy,r2,r2); g.drawLine(circl.sx-(r2*71/200)+r2/2,circl.sy-(r2*71/200)+r2/2,circl.sx+(r2*71/200)+r2/2,circl.sy+(r2*71/200)+r1/2); g.drawLine(circl.sx-(r2*71/200)+r2/2,circl.sy+(r2*71/200)+r2/2,circl.sx+(r2*71/200)+r2/2,circl.sy-(r2*71/200)+r1/2); } else { g.drawOval(circl.sx,circl.sy,r2,r2); g.fillOval(circl.sx+r2/2-r2/6,circl.sy+r2/2-r2/6,r2/3,r2/3); } if(i1<0) { g.drawOval(circ.sx,circ.sy,r1,r1); // g.drawLine(circ.sx-(r1*71/200)+r1/2,circ.sy-(r1*71/200)+r1/2,circ.sx+(r1*71/200)+r1/2,circ.sy+(r1*71/200)+r1/2); // g.drawLine(circ.sx-(r1*71/200)+r1/2,circ.sy+(r1*71/200)+r1/2,circ.sx+(r1*71/200)+r1/2,circ.sy-(r1*71/200)+r1/2); } else { g.drawOval(circ.sx,circ.sy,r2,r2); // g.fillOval(circ.sx+r2/2-r2/6,circ.sy+r2/2-r2/6,r2/3,r2/3); } repaint(); } public void drawline(Graphics g,Line line) // draw field line one way { line.sx = xcoord; line.sy = ycoord; g.setColor(Color.blue); dist1 = Math.sqrt(Math.pow(line.sx-x2, 2) + Math.pow(line.sy-y2, 2)); dist2 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy-y1, 2)); int ilinc = 0; int ilimit = 0; while(ilimit < 8000) { ilimit++; dist1 = Math.sqrt(Math.pow(line.sx-x2, 2) + Math.pow(line.sy-y2, 2)); dist2 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy-y1, 2)); compy = -1.*(i2*(line.sx - x2) / Math.pow(dist1,3) + i1*(line.sx - x1) / Math.pow(dist2,3)); compx = i2*(line.sy - y2) / Math.pow(dist1,3) + i1*(line.sy - y1) / Math.pow(dist2,3)+bconst; line.emag = Math.sqrt(Math.pow(compx,2) + Math.pow(compy,2)); line.ey = line.sy + (compy/line.emag) * dx/10; line.ex = line.sx + dx*(compx/line.emag)/10; //g.setColor(new Color(0,190,0)); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); // draw arrow head if (ilinc>10) { double vey,vex; vey = line.sy ; vex = line.sx ; dy1 = 1*(arrowlength2*((sinangle*compx-cosangle*compy)/line.emag)); dx1 = -1*(arrowlength2*((cosangle*compx+sinangle*compy)/line.emag)); dy2 = 1*(arrowlength2*((-sinangle*compx-cosangle*compy)/line.emag)); dx2 = -1*(arrowlength2*((cosangle*compx-sinangle*compy)/line.emag)); g.drawLine((int)vex, (int)vey, (int)(vex + dx1),(int)(vey + dy1)); g.drawLine((int)vex, (int)vey, (int)(vex + dx2),(int)(vey + dy2)); ilinc=0; } // draw field line other way line.sx = line.ex; line.sy = line.ey; ilinc++; } line.sx = xcoord; line.sy = ycoord; ilimit=0; dist1 = Math.sqrt(Math.pow(line.sx-x2, 2) + Math.pow(line.sy-y2, 2)); dist2 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy-y1, 2)); ilinc=0; while(ilimit < 8000) { dist1 = Math.sqrt(Math.pow(line.sx-x2, 2) + Math.pow(line.sy-y2, 2)); dist2 = Math.sqrt(Math.pow(line.sx-x1, 2) + Math.pow(line.sy-y1, 2)); compy = 1.*(i2*(line.sx - x2) / Math.pow(dist1,3) + i1*(line.sx - x1) / Math.pow(dist2,3)); compx = -1.*(i2*(line.sy - y2) / Math.pow(dist1,3) + i1*(line.sy - y1) / Math.pow(dist2,3)+bconst); line.emag = Math.sqrt(Math.pow(compx,2) + Math.pow(compy,2)); line.ey = line.sy + (compy/line.emag) * dx/10; line.ex = line.sx + dx*(compx/line.emag)/10; //g.setColor(new Color(0,190,0)); g.drawLine((int)line.sx, (int)line.sy, (int)line.ex, (int)line.ey); // draw arrow head if (ilinc>10) { double vey,vex; vey = line.sy ; vex = line.sx ; dy1 = -1*(arrowlength2*((sinangle*compx-cosangle*compy)/line.emag)); dx1 = 1*(arrowlength2*((cosangle*compx+sinangle*compy)/line.emag)); dy2 = -1*(arrowlength2*((-sinangle*compx-cosangle*compy)/line.emag)); dx2 = 1*(arrowlength2*((cosangle*compx-sinangle*compy)/line.emag)); g.drawLine((int)vex, (int)vey, (int)(vex + dx1),(int)(vey + dy1)); g.drawLine((int)vex, (int)vey, (int)(vex + dx2),(int)(vey + dy2)); ilinc=0; } // line.sx = line.ex; line.sy = line.ey; ilinc++; ilimit++; } drawline = false; } public boolean mouseMove(Event e, int x, int y) { /*Defines the xcoord and ycoord variables in terms of the x/y values of the object being moved */ xcoord = x; ycoord = y; return true; } public boolean deterHit(Circle circ, int mx, int my) { /*Tells the program how to decide if an object is being selected or not. */ if (mx > circ.cx - r1 && mx < circ.cx + r1 && my > circ.cy - r1 && my < circ.cy + r1) { return true; } return false; } public boolean mouseDown(Event e, int x, int y) { if (deterHit(cone,x,y)) {cone.hit = 1; } if (deterHit(ctwo,x,y)) {ctwo.hit = 1; } if (deterHit(cthr,x,y)) {cthr.hit = 1; } if( cone.hit == 0 && ctwo.hit == 0 && cthr.hit == 0) { xcoord = x; ycoord = y; drawline = true; move=false; repaint(); } return true; } public boolean mouseUp(Event e, int x, int y) { cone.hit = 0; ctwo.hit = 0; cthr.hit = 0; move = false; return true; } public boolean mouseDrag(Event e, int x, int y) { xcoord = x; ycoord = y; if (cone.hit == 1) {cone.cx = x; cone.cy = y; repaint(); } if (ctwo.hit == 1) {ctwo.cx = x; ctwo.cy = y; repaint(); } if (cthr.hit == 1) {cthr.cx = x; cthr.cy = y; repaint(); } move = true; return true; } public boolean handleEvent(Event e) { Object target=e.target; if(target==dxScroll) { dx = 10*dxScroll.getValue(); dxLabel.setText("Speed = " + dx); move = true; repaint(); } if (e.id==Event.ACTION_EVENT) { if(target==resetbutton) { cone.hit = 0; ctwo.hit = 0; cthr.hit = 0; x1 = bx1; y1 = by1; x2 = bx2; y2 = by2; x3 = bx3; y3 = by3; cone.cx = (int)bx1; cone.cy = (int)by1; ctwo.cx = (int)bx2; ctwo.cy = (int)by2; cthr.cx = (int)bx3; cthr.cy = (int)by3; i1 = bi1; i2 = bi2; sig = bsig; dx = bdx; dxScroll.setValue(ssval2); dxLabel.setText("Delta X = " + dx); move = true; repaint(); } } return super.handleEvent(e); } }