/* Put an electron into orbit */ import java.awt.*; import java.applet.*; public class Orbit extends Applet implements Runnable{ Thread moveThread; private static final int MOVEPAUSE = 30; public boolean myGo = false, inTow = false, copyRight = false; public boolean holdIt = false, crashed = false;; public Image theBack, theFront; public Image offScreenImage; public int xS=100, yS=100, vx=0, vy=0; public int xE=250, yE=175, rE=15; public double vv=0, xx=xS, yy=yS, xxV=0, yyV=0; public int t=0; public double vscale=10000.0, rscale=0.01; public void start() { if(myGo) { if(moveThread == null){ moveThread = new Thread(this); moveThread.start(); } } } public void stop() { if(moveThread != null){ moveThread.stop(); try { moveThread.join(); } catch (InterruptedException e) { } } moveThread=null; } public void run() { while (true){ try { moveThread.sleep(MOVEPAUSE); }catch(InterruptedException e) { } repaint(); } } public void update(Graphics g){ Graphics offg = offScreenImage.getGraphics(); paint(offg); g.drawImage(offScreenImage, 0, 0, this); } public void init() { super.init(); offScreenImage = createImage(500,350); //{{INIT_CONTROLS setLayout(null); resize(500,350); //}} repaint(); } public void paint(Graphics g){ g.setColor(Color.lightGray); g.fillRect(0,0,500,350); for (int i=0; i<10; i++){ g.setColor(new Color(255,i*20,i*20)); g.fillOval(xE-rE+i,yE-rE+i,2*rE-3*i,2*rE-3*i); } g.setColor(Color.black); g.setFont(new Font("Helvetica", Font.BOLD, 24)); g.drawString("+",xE-7,yE+7); if (crashed) { g.setColor(Color.red); g.fillOval((int)xx-10,(int)yy-10,20,20); myGo = false; stop(); } else { for (int i=0; i<10; i++){ g.setColor(new Color(i*20,i*20,255)); g.fillOval((int)xx-rE+i,(int)yy-rE+i,2*rE-3*i,2*rE-3*i); } g.setColor(Color.black); g.setFont(new Font("Helvetica", Font.BOLD, 30)); g.drawString("-",(int)xx-6,(int)yy+6); vv = Math.sqrt(xxV*xxV + yyV*yyV); if (inTow){ t=0; g.setColor(Color.lightGray); if (vv < 10) { g.drawLine(xS, yS, xS+vx, yS+vy); } else { drawArrow(g, xS, yS, xS+vx, yS+vy); } xx = (double)xS; yy = (double)yS; xxV = (double)vx; yyV = (double)vy; } else { g.setColor(Color.yellow); if (vv < 10) { g.drawLine((int)xx, (int)yy, (int)(xx+xxV), (int)(yy+yyV)); } else { drawArrow(g, (int)xx, (int)yy, (int)(xx+xxV), (int)(yy+yyV)); } } if (myGo) { t++; double dist = Math.sqrt((xx-xE)*(xx-xE) + (yy-yE)*(yy-yE)); xxV = xxV - vscale*(xx-xE)/(dist*dist*dist); yyV = yyV - vscale*(yy-yE)/(dist*dist*dist); xx = xx + rscale*xxV; yy = yy + rscale*yyV; if (dist < rE) crashed = true; } } if (xx > 520 || xx < -20 || yy > 370 || yy < -20) { myGo = false; stop(); } if (myGo) { g.setColor(Color.red); g.setFont(new Font("Helvetica", Font.BOLD, 12)); g.drawString("Running ...",430,10); } /* g.setColor(Color.white); g.drawString("xx ="+xx,400,30); g.drawString("yy ="+yy,400,60); g.drawString("xxV ="+xxV,400,90); g.drawString("yyV ="+yyV,400,120); g.drawString("t ="+t,400,150); */ // 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) { if (event.target == this && event.id == Event.KEY_PRESS) { String thePressedKey = ""+(char)event.key; if (thePressedKey.equalsIgnoreCase("w")) { copyRight = !copyRight; repaint(); } if (thePressedKey.equalsIgnoreCase("s")) { holdIt = !holdIt; if (holdIt) { stop(); } else { start(); repaint(); } } } if (event.target == this && event.id == Event.MOUSE_DOWN) { myGo = false; inTow = true; holdIt = false; crashed = false; xS = event.x; yS = event.y; vy = 0; vy = 0; repaint(); return true; } if (event.target == this && event.id == Event.MOUSE_DRAG) { vx = event.x - xS; vy = event.y - yS; repaint(); return true; } if (event.target == this && event.id == Event.MOUSE_UP) { myGo = true; inTow = false; vx = event.x - xS; vy = event.y - yS; start(); repaint(); return true; } return super.handleEvent(event); } public void drawArrow(Graphics g, int tailX, int tailY, int headX, int headY) { double theta = Math.atan( (double)(tailY-headY)/(tailX-headX) ); if((tailX-headX)>=0) theta+= Math.PI; int w1=8/2, w2=16/2, w3=24/2; double s = Math.sin(theta); double c = Math.cos(theta); int sw1 = (int)(s*w1); int sw2 = (int)(s*w2); int sw3 = (int)(s*w3); int cw1 = (int)(c*w1); int cw2 = (int)(c*w2); int cw3 = (int)(c*w3); int[] exes = {tailX-sw1, headX-cw3-sw1, headX-cw3-sw2, headX, headX-cw3+sw2, headX-cw3+sw1, tailX+sw1, tailX-sw1}; int[] whys = {tailY+cw1, headY-sw3+cw1, headY-sw3+cw2, headY, headY-sw3-cw2, headY-sw3-cw1, tailY-cw1, tailY+cw1}; Polygon arrow = new Polygon(exes, whys, 8); g.fillPolygon(arrow); g.setColor(Color.black); g.drawPolygon(arrow); } public String outString(String inString, int l){ // returns the first l chars of string int ls = (int)Math.min(inString.length(),l); return inString.substring(0,ls); } //{{DECLARE_CONTROLS //}} }