/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////*\ 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; class Trajectory extends Canvas implements Runnable { private Thread myThread; private AFMSimulator afmSim; private BufferedImage image; private Graphics back; private Color backgroundColor; private Color textColor; private int ind; private final int W = 500, H = 300; private final double INTERVAL = (double) W / AFMConstants.SAMPLES; private int delay = 100; public Trajectory() { 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); backgroundColor = AFMApplet.default_BG_color; textColor= AFMApplet.default_text_color; back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back.setColor(textColor); back.drawString("( x( t ), y( t ) )", 400, 20); ind = 0; AFMApplet.drawSurface(image, afmSim); } public Trajectory(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 drawSurface() { back = image.getGraphics(); back.setColor(Color.blue); for(int i=0; i=W) xCoord-=W; back.drawOval(xCoord,yCoord,0,0); } }*/ public void reset() { image = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB); back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back.setColor(textColor); back.drawString("( x( t ), y( t ) )", 400, 20); ind = 0; 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; back = null; backgroundColor = null; textColor = null; } public AFMSimulator getState() { //AFMSimulator afmSimu = afmSim.getNotify(); return afmSim; } /*public void destroy() { this.stop(); }*/ public void clear() { back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back.setColor(textColor); back.drawString("( x( t ), y( t ) )", 400, 20); AFMApplet.drawSurface(image, afmSim); /*back.setColor(AFMApplet.default_surface_color); for (int i = 0; i < AFMConstants.SAMPLES; i++) { int xCoord = W / 2 + (int) (i * INTERVAL); int yCoord = AFMApplet.SURFACE_HEIGHT - (int) Math.round(afmSim.getSurface().getY(i) * AFMApplet.Y_SCALE); if (xCoord >= W) xCoord -= W; back.drawOval(xCoord, yCoord, 0, 0);*/ } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { //setBackground(Color.white); back = image.getGraphics(); back.setColor(textColor); back.drawString("( x( t ), y( t ) )", 400, 20); back.setColor(AFMApplet.default_info_color); back.drawString("Time = " + ind, 20, 290); for (int j = 0; j < AFMConstants.SAMPLES; j += 5) { back.setColor(new Color(0xFF00 + j * 0xFF)); // gives nice colors. On every iteration, // the same indices have the same color int xCoord = (int) (W / 2 + AFMMath.mod(afmSim.getAFM().getX(j), Surface.L) * (double) W / (Surface.L)); /***Changed this... experimenting with Force. Change back to getY(j)***/ int yCoord = AFMApplet.SURFACE_HEIGHT - (int) Math.round(afmSim.getAFM().getY(j) * AFMApplet.Y_SCALE); if (xCoord >= W) xCoord -= W; // *** DO you need this? Aren't you redrawing the surface each time anyway? // Not needed. Exception is okay (AFM bounces too high). //try { // if (image.getRGB(xCoord, yCoord) != AFMApplet.default_surface_color.getRGB()) back.drawOval(xCoord - 2, yCoord - 2, 1, 1); //} //catch (ArrayIndexOutOfBoundsException e) { // System.out.println("Array Index out of bounds in Trajectory.class" + e); // System.out.println(xCoord + " " + yCoord); //} } g.drawImage(image, 0, 0, this); back.setColor(backgroundColor); if(!AFMApplet._accumulate2) { for (int j = 0; j < AFMConstants.SAMPLES; j += 5) { int xCoord = (int) (W / 2 + AFMMath.mod(afmSim.getAFM().getX(j), Surface.L) * (double) W / (Surface.L)); int yCoord = AFMApplet.SURFACE_HEIGHT - (int) Math.round(afmSim.getAFM().getY(j) * AFMApplet.Y_SCALE); if (xCoord >= W) xCoord -= W; back.drawOval(xCoord - 2, yCoord - 2, 1, 1); } } back.drawString("Time = " + ind, 20, 290); afmSim.getMore(); ind++; }//end paint }//end Trajectory