import java.awt.*; import java.applet.*; public class teufel extends Applet implements Runnable{ Thread moveThread; private static final int MOVEPAUSE = 100; public boolean myGo = false; public boolean inTow = false; public boolean halfkey = false; public boolean stopWatch = false; public Image offScreenImage; public int theW = 150; public int theH = 400; public int yBottle = 120; public int ytop = yBottle; public int theGap = 30; public int yHigh = 30; public int yLine = 2*theH/3; public int dy; public int it; public int dt; public double dytop = (double)ytop; public long t1; 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 init() { super.init(); offScreenImage = createImage(theW,theH); //{{INIT_CONTROLS setLayout(null); resize(150,400); setBackground(new Color(16777215)); //}} repaint(); } //{{DECLARE_CONTROLS //}} public void update(Graphics g){ Graphics offg = offScreenImage.getGraphics(); paint(offg); g.drawImage(offScreenImage, 0, 0, this); } public void paint(Graphics g){ // calcs if (yHigh < 10) yHigh = 10; if (yHigh > 90) yHigh = 90; int yplate = yBottle - 10 + yHigh/10; theGap = 32 - yHigh/4; double diff = (32. - (double)yHigh/4.0) - 20.0; if (Math.abs(diff) < 0.1) { myGo = false; stop(); } else { myGo = true; start(); } // dytop is precise location of top of teufel, ytop its rounded value dytop = dytop - diff; if ((int)dytop < yplate+10) { dytop = (double)(yplate+10); if (!stopWatch) { myGo = false; stop(); } } if ((int)dytop > theH-110) { dytop = (double)(theH-110); if (!stopWatch) { myGo = false; stop(); } } ytop = (int)dytop; if (stopWatch) { myGo = true; start(); } // Draw everything g.setColor(Color.white); g.fillRect(0, 0, theW, theH); g.setColor(Color.darkGray); g.fillRect(0,yBottle,theW,theH-yBottle); // bottle g.setColor(Color.blue); g.fillRect(10,yBottle,theW-20,theH-yBottle-10); // water g.setColor(Color.yellow); g.drawLine(0,yLine,theW,yLine); g.setColor(Color.black); g.fillRect(10,yplate,theW-20,10); // top plate g.setColor(Color.darkGray); g.fillRect(11,yplate+1,theW-22,8); g.setColor(Color.black); g.fillRect(30,ytop,theW-60,100); // teufel g.setColor(Color.white); g.fillRect(40,ytop+10,theW-80,theGap); // air gap g.setColor(Color.blue); g.fillRect(40,ytop+10+theGap,theW-80,100-10-theGap); // water g.setColor(Color.black); g.fillRect(40,ytop+60,25,40); // teufel - bottom g.fillRect(theW-65,ytop+60,25,40); drawVerticalSpring(5,theW-40,40,yHigh+5,yplate-1,g); drawVerticalSpring(5,theW-40,40,yHigh+6,yplate ,g); g.setColor(Color.red); g.fillRect(40,yHigh-5,theW-80,10); // force plate if (stopWatch) { if (Math.abs(ytop-yLine) < 3 && Math.abs(diff) < 0.1) { } else { dt = (int)(System.currentTimeMillis() - t1); } g.setColor(Color.red); g.drawString(""+dt,20,15); } } public void drawVerticalSpring(int nt, int xhigh, int xlow, int yhigh, int ylow, Graphics g){ double denom = 2.0*nt + 1.0; g.drawLine((xhigh+xlow)/2,ylow,xhigh,(int)(ylow+0.5*(yhigh-ylow)/denom)); for (int i = 1; i < 2*nt; i=i+2){ g.drawLine(xhigh,(int)(ylow+(i-0.5)*(yhigh-ylow)/denom), xlow,(int)(ylow+(i+0.5)*(yhigh-ylow)/denom)); g.drawLine(xhigh,(int)(ylow+(i+1.5)*(yhigh-ylow)/denom), xlow,(int)(ylow+(i+0.5)*(yhigh-ylow)/denom)); } g.drawLine(xhigh,(int)(ylow+(2.*nt+0.5)*(yhigh-ylow)/denom),(xhigh+xlow)/2,yhigh); } public boolean handleEvent(Event evt){ if (evt.key == Event.DOWN){ halfkey = !halfkey; if (halfkey) yHigh++; repaint(); return true; } if (evt.key == Event.UP){ halfkey = !halfkey; if (halfkey) yHigh--; repaint(); return true; } if (evt.id == Event.KEY_PRESS) { String thePressedKey = ""+(char)evt.key; if (thePressedKey.equalsIgnoreCase("s")) { stopWatch = !stopWatch; t1 = System.currentTimeMillis(); repaint(); } } if (evt.target == this && evt.id == Event.MOUSE_DOWN) { osc_MouseDown(evt); return true; } if (evt.target == this && evt.id == Event.MOUSE_DRAG) { osc_MouseDrag(evt); return true; } if (evt.target == this && evt.id == Event.MOUSE_UP) { osc_MouseUp(evt); return true; } return false; } void osc_MouseDrag(Event event) { if (inTow) { yHigh = event.y - dy; repaint(); it = 0; } } void osc_MouseDown(Event event) { dy = event.y - yHigh; if ((int)Math.abs(dy) < 10) { inTow = true; //myGo = false; } } void osc_MouseUp(Event event) { inTow = false; } }