/* Faraday induction applet */ import java.awt.*; import java.applet.*; public class faraday extends Applet { public MediaTracker tracker; public Image offScreenImage; public Image theBox; public boolean copyRight = false; public boolean dragLoop = false, pole = false; public int loop = 100, loop1, v=0, v1=0, vmax=50, dx = 0; public int ytop = 234 - 20; public int ybot = 234 + 20; public void init() { super.init(); // load the backgrond image tracker= new MediaTracker(this); theBox = this.getImage(getCodeBase(),"magnet.jpg"); tracker.addImage(theBox,0); try { tracker.waitForID(0); } catch (InterruptedException e) { } offScreenImage = createImage(500,350); //{{INIT_CONTROLS setLayout(null); resize(500,350); //}} } public void update(Graphics g){ Graphics offg = offScreenImage.getGraphics(); paint(offg); g.drawImage(offScreenImage, 0, 0, this); } public void paint(Graphics g){ g.setColor(Color.white); g.fillRect(0,0,500,350); g.drawImage(theBox,0,0,theBox.getWidth(this),theBox.getHeight(this),this); // galvanometer needle g.setColor(Color.blue); int x = 78 + v; if (pole) x = 78 - v; int y = 105 - (int)(Math.sqrt(73*73 - v*v)); g.drawLine(78,105,x,y); g.fillOval(x-3,y-3,7,7); // loop int xlt = loop - 35 - 26; int xlb = loop - 35 + 26; int xrt = loop + 35 - 26; int xrb = loop + 35 + 26; int xt1 = loop + 7 + 26; int xt2 = loop - 7 + 26; g.setColor(Color.red); for (int i=-1; i<=1; i++) { g.drawLine(xlt-i,ytop+i,xrt-i,ytop+i); g.drawLine(xrt-i,ytop+i,xrb-i,ybot+1); g.drawLine(xrb-i,ybot+i,xt1-i,ybot+i); g.drawLine(xlb-i,ybot+i,xt2-i,ybot+i); g.drawLine(xlb-i,ybot+i,xlt-i,ytop+i); } // matching cable endpoints g.setColor(Color.green); g.fillOval(xt1-4,ybot-4,8,8); g.fillOval(59,137,8,8); g.setColor(Color.yellow); g.fillOval(xt2-4,ybot-4,8,8); g.fillOval(87,139,8,8); // write polarity on magnet g.setColor(Color.green); g.setFont(new Font("Helvetica", Font.BOLD, 24)); if (pole) { g.drawString("+",188,175); g.drawString("-",188,317); } else { g.drawString("-",188,175); g.drawString("+",188,317); } // copyright notice: if (copyRight) { g.setColor(Color.green); g.fillRect(100,100,240,70); g.setColor(Color.white); g.setFont(new Font("Helvetica", Font.PLAIN, 18)); g.drawString("This applet was written by:",110,120); g.drawString("Wolfgang Bauer",110,140); g.drawString("Copyright: WB 1999",110,160); } } public boolean handleEvent(Event event) { int xclick = event.x; int yclick = event.y; if (event.target == this && event.id == Event.MOUSE_DOWN) { dragLoop = false; System.out.println("x:"+event.x+"; y:"+event.y); if (yclickytop && xclickloop-40) { dx = loop - xclick; loop1 = loop; dragLoop = true; } if (yclick<203 && yclick>122 && xclick<209 && xclick>182) { pole = !pole; repaint(); return true; } if (yclick<348 && yclick>265 && xclick<209 && xclick>182) { pole = !pole; repaint(); return true; } } if (event.target == this && event.id == Event.MOUSE_DRAG) { if (dragLoop) { loop = xclick + dx; if (loop > 154 && loop < 234) { v = 8*(loop - loop1); v = (v+v1)/2; // this just damps the galv. motion if (v > vmax) v = vmax; if (v < -vmax) v = -vmax; v1 = v; } else { v = 0; } loop1 = loop; repaint(); return true; } } if (event.target == this && event.id == Event.MOUSE_UP) { dragLoop = false; v = 0; v1 = 0; repaint(); return true; } return super.handleEvent(event); } //{{DECLARE_CONTROLS //}} }