/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////*\ This class is an extention of Canvas. Runs its own thread. paint methods cannot be called directly by browser (good) \*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////*/ import java.awt.*; import java.awt.image.BufferedImage; import java.text.DecimalFormat; class Animation extends Canvas implements Runnable { private Thread myThread; private AFMSimulator afmSim; private BufferedImage image; private int ind, time; private final int W = 500, H = 300; private final double INTERVAL = (double) W / AFMConstants.SAMPLES; private int delay = 100; private int k = 6; // speed of animation (skip rate) private Color backgroundColor; private Color textColor; public Animation() { setSize(W, H); afmSim = new AFMSimulator(); afmSim.setParams(AFMApplet.params[0], AFMApplet.params[1], AFMApplet.params[2], AFMApplet.params[3], AFMApplet.params[4], AFMApplet.params[5], AFMApplet.params[6], AFMApplet.params[7]); afmSim.resetSim(); image = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB); textColor = AFMApplet.default_text_color; backgroundColor = AFMApplet.default_BG_color; Graphics back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back.setColor(textColor); back.drawString("AFM Animation", 400, 20); ind = 0; time = 0; AFMApplet.drawSurface(image, afmSim); } public Animation(int _delay) { this(); delay = _delay; } public void start() { if (myThread != null) stop(); myThread = new Thread(this); myThread.start(); AFMApplet.drawSurface(image, afmSim); }//end start public void reset() { image = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB); ind = 0; time = 0; Graphics back = image.getGraphics(); back.setColor(backgroundColor); // background color back.fillRect(0, 0, W, H); back.setColor(textColor); // title text color back.drawString("AFM Animation", 400, 20); afmSim = new AFMSimulator(); afmSim.setParams(AFMApplet.params[0], AFMApplet.params[1], AFMApplet.params[2], AFMApplet.params[3], AFMApplet.params[4], AFMApplet.params[5], AFMApplet.params[6], AFMApplet.params[7]); afmSim.resetSim(); AFMApplet.drawSurface(image, afmSim); } public void resume() { start(); } public void run() { while (myThread != null) { repaint(); try { Thread.sleep(delay); } catch (InterruptedException e) { this.stop(); } } }//end run public void stop() { myThread = null; } public void cleanup() { myThread = null; afmSim = null; image = null; backgroundColor = null; textColor = null; } /*public void destroy() { this.stop(); }*/ public void setState(AFMSimulator afmSim) { this.afmSim = afmSim; } public void clear() { Graphics back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); } public void update(Graphics g) { Graphics back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back = image.getGraphics(); back.setColor(textColor); back.drawString("AFM Animation", 400, 20); int old; if (ind > 0) old = ind - k; else old = k * (AFMConstants.SAMPLES / k); /* int tipX = (int)(W/2 +(afmSim.getAFM().getX(old)-afmSim.getAFM().getV()*afmSim.getAFM().getT(old))*AFMConstants.SAMPLES/(Surface.L)); int tipY = H/2+30 - (int)Math.round(afmSim.getAFM().getY(old)*150); int x[]={tipX-5,tipX,tipX+5}, y[] = {tipY-9,tipY-2,tipY-9}; back.setColor(getBackground()); back.fillPolygon(x,y,3); back.drawLine((int)(W/2+(afmSim.getAFM().getX(old)-afmSim.getAFM().getV()*afmSim.getAFM().getT(old))*AFMConstants.SAMPLES/Surface.L), (int)(H/2+30 - (int)Math.round(afmSim.getAFM().getY(old)*150)-10), W/2, 0); */ for (int i = 0; i < AFMConstants.SAMPLES; i++) { int xOld = W / 2 + (int) (i * INTERVAL); //**int j=i+old; if(j>=AFMConstants.SAMPLES) j-=AFMConstants.SAMPLES; // int yOld = H/2+30 - (int)Math.round(afmSim.getSurface().getY(j)*150); int j = AFMMath.mod((i + ind + AFMSimulator.STROBE_OFFSET), AFMConstants.SAMPLES); //if(j>=AFMConstants.SAMPLES) j-=AFMConstants.SAMPLES; int yNew = AFMApplet.SURFACE_HEIGHT - (int) Math.round(afmSim.getSurface().getY(j) * AFMApplet.Y_SCALE); if (xOld > W) xOld -= W; // back.drawOval(xOld, yOld, 0, 0); back.setColor(AFMApplet.default_surface_color); back.drawOval(xOld, yNew, 0, 0); // back.setColor(getBackground()); } back.setColor(backgroundColor); DecimalFormat dec = new DecimalFormat("0.###"); String t = dec.format(afmSim.getAFM().getTime(old)); back.drawString("Time = " + time + " = " + t, 20, 290); //afmSim.getAFM().getTime(old), others (0) ind-1-->time paint(g); } public void paint(Graphics g) { //setBackground(Color.white); Graphics back = image.getGraphics(); if (AFMMath.dif(AFMConstants.SAMPLES, ind, 20)) /**** and selection is STROBOSCOPIC,... ****/ back.setColor(textColor); else back.setColor(AFMApplet.default_info_color); DecimalFormat dec = new DecimalFormat("0.###"); String t = dec.format(afmSim.getAFM().getTime(ind)); back.drawString("Time = " + time + " = " + t + " ms", 20, 290); //afmSim.getAFM().getTime(ind) //back.translate(W-10*ind, H/2+30);//(int)(afmSim.getAFM().getV()*afmSim.getAFM().getT(ind)), H/3); //g.setColor(Color.white); //g.drawString("|", (int)(W/2 +(afmSim.getAFM().getX(ind)-afmSim.getAFM().getV()*afmSim.getAFM().getT(ind))*AFMConstants.SAMPLES/(Surface.L)), H/2+30 - (int)Math.round(afmSim.getAFM().getY(ind)*150)); int tipX = (int) (W / 2 + (afmSim.getAFM().getX(ind) - afmSim.getAFM().getV() * afmSim.getAFM().getT(ind)) * AFMConstants.SAMPLES / (Surface.L)); int tipY = AFMApplet.SURFACE_HEIGHT - (int) Math.round(afmSim.getAFM().getY(ind) * AFMApplet.Y_SCALE); int x[] = {tipX - 5, tipX, tipX + 5}, y[] = {tipY - 7, tipY, tipY - 7}; back.fillPolygon(x, y, 3); back.setColor(AFMApplet.default_AFMspring_color); back.drawLine((int) (W / 2 + (afmSim.getAFM().getX(ind) - afmSim.getAFM().getV() * afmSim.getAFM().getT(ind)) * AFMConstants.SAMPLES / Surface.L), (int) (AFMApplet.SURFACE_HEIGHT - (int) Math.round(afmSim.getAFM().getY(ind) * AFMApplet.Y_SCALE) - 8), W / 2, 0); g.drawImage(image, 0, 0, this); ind += k; if (ind >= AFMConstants.SAMPLES) { afmSim.getMore(); ind = 0; time++; } }//end paint }//end Animation