/** * Created by IntelliJ IDEA. * User: siddhartha * Date: Jan 29, 2006 * Time: 9:55:16 PM * To change this template use File | Settings | File Templates. /*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////*\ 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.event.MouseMotionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.text.DecimalFormat; class Measure extends Canvas implements Runnable { private Thread myThread; private AFMSimulator afmSim; private BufferedImage image; private Color backgroundColor; private Color textColor; private int ind; private final int W = 500, H = 300; private int delay = 100; private int what; // measuring what? 1 = Force, 2 = Friction, 3 = Liapunov, etc. private boolean plot = true; // plot or numerics? private final int X_AXIS = H/2; private final int Y_AXIS = W/2; //private final int MEASURE_SCALE = 10000; private int zoom = 1000; public Measure() { 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; Graphics back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back.setColor(textColor); what = 1; // defaults to Force back.drawString(measuring(what), 400, 20); addMouseMotionListener(new MouseCoordListener()); addMouseListener(new MouseCoordListener()); ind = 0; } public Measure(int _delay) { this(); delay = _delay; } public void start() { if (myThread != null) stop(); myThread = new Thread(this); myThread.start(); drawAxes(); }//end start public void reset() { image = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB); Graphics back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back.setColor(textColor); back.drawString(measuring(what), 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(); } 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 AFMSimulator getState() { return afmSim; } public void clear() { Graphics back = image.getGraphics(); back.setColor(backgroundColor); back.fillRect(0, 0, W, H); back.setColor(textColor); } public void switchDisplay() { clear(); if(plot) plot = false; else plot = true; } public void drawAxes() { double x_max = (double) (W - W / 2) * Surface.L / W; // x_center holds *actual* x center value (in the system, not on the display) double y_max = (double) (X_AXIS - 0) / zoom; AFMApplet.drawAxes(image, X_AXIS, Y_AXIS, "x", "F", x_max, y_max, zoom); } public String measuring(int what) { if(what == 1) return "Force"; if(what == 2) return "Friction"; if(what == 3) return "Energy"; else return "Patience"; } public void measure(int what) { this.what = what; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { Graphics back = image.getGraphics(); if(!plot) { back.setColor(AFMApplet.default_terminal_color); back.drawString("Average force = " + Calculate.average(afmSim.getAFM().getFX()), 10, 200); g.drawImage(image, 0, 0, this); BufferedImage image2 = image.getSubimage(0,10,W,H-10); back.drawImage(image2, 0, -10, this); afmSim.getMore(); ind++; } else { back.setColor(textColor); back.drawString(measuring(what), 400, 20); drawAxes(); 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)); if (xCoord >= W) xCoord -= W; /***Changed this... experimenting with Force. Change back to getY(j)***/ double mag = Calculate.magnitude(afmSim.getAFM().getFX(j), afmSim.getAFM().getFY(j)); //int yCoord = AFMApplet.X_AXIS - (int) Math.round(afmSim.getAFM().getFX(j) * AFMApplet.MEASURE_SCALE/10); int yCoord = X_AXIS - (int) Math.round(mag * zoom); /***workin' all the way ***/ double x_realValue = (double) (xCoord - W / 2) * Surface.L / W; // x_center holds *actual* x center value (in the system, not on the display) if(x_realValue < 0) x_realValue += 1; double y_realValue = (double) (X_AXIS - yCoord) / zoom; //System.out.println("Real values: x = " + afmSim.getAFM().getX(j) + ". mag(F) = " + mag); //System.out.println("Screen values: x = " + xCoord + ". yCoord = " + yCoord); //System.out.println("Converted back to real values: x = " + x_realValue + ". y = " + y_realValue); back.drawOval(xCoord, yCoord, 1, 1); } g.drawImage(image, 0, 0, this); /*** Clear old values ***/ back.setColor(backgroundColor); 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)); double mag = Calculate.magnitude(afmSim.getAFM().getFX(j), afmSim.getAFM().getFY(j)); //int yCoord = AFMApplet.X_AXIS - (int) Math.round(afmSim.getAFM().getFX(j) * AFMApplet.MEASURE_SCALE/10); int yCoord = X_AXIS - (int) Math.round(mag * zoom); if (xCoord >= W) xCoord -= W; back.drawOval(xCoord, yCoord, 1, 1); } back.drawString("Time = " + ind, 20, 290); afmSim.getMore(); ind++; } }//end paint private class MouseCoordListener implements MouseMotionListener, MouseListener { private String lastInfo = ""; public void mouseDragged(MouseEvent e) { //To change body of implemented methods use File | Settings | File Templates. } public void mouseMoved(MouseEvent event) { Graphics back = image.getGraphics(); double x = event.getPoint().getX(); double y = event.getPoint().getY(); double x_realValue = (double) (x - W / 2) * Surface.L / W; // x_center holds *actual* x center value (in the system, not on the display) if(x_realValue > 1) x_realValue -= 1; double y_realValue = (double) (X_AXIS - y) / zoom; back.setColor(backgroundColor); back.drawString(lastInfo, 20, 270); DecimalFormat dec = new DecimalFormat("0.###"); dec.setMaximumFractionDigits((int)Math.round(AFMMath.log2(zoom/100)) + 4); back.setColor(AFMApplet.default_info_color); lastInfo = "(" + dec.format(x_realValue) + ", " + dec.format(y_realValue) + ")"; back.drawString(lastInfo, 20, 270); /***ADD CODE HERE***/ } public void mouseClicked(MouseEvent event) { if(AFMApplet.zoomTool.isSelected()) { if (event.getButton() == MouseEvent.BUTTON1) zoom *= 2; else zoom /= 2; if(zoom<1) zoom = 1; clear(); repaint(); //translate pixels to values } } public void mouseEntered(MouseEvent e) { //To change body of implemented methods use File | Settings | File Templates. } public void mouseExited(MouseEvent e) { //To change body of implemented methods use File | Settings | File Templates. } public void mousePressed(MouseEvent e) { //To change body of implemented methods use File | Settings | File Templates. } public void mouseReleased(MouseEvent e) { //To change body of implemented methods use File | Settings | File Templates. } } }//end Measure