// // Exp.java // Newton's Cooling Function // // Created by Chris Thiel on Mon Nov 18 2003. // Copyright (c) 2003 Chris Thiel. All rights reserved. // A simple Java applet for students to interact with // // import java.awt.*; import java.applet.*; import java.lang.*; import java.io.*; import java.awt.event.*; import java.awt.Graphics; public class Exp extends Applet implements AdjustmentListener { int rx = 50; //red dot in screen coords int ry = 300; //75 degrees in screen cords int bx = 120; // blue dot int by = 132; // heated to 180 in screen coords int gx = 0; //green dot int gy = 420; Scrollbar horiz=new Scrollbar(Scrollbar.HORIZONTAL,1,20,0,60); public void init() { this.setBackground(Color.cyan); //the next line adds an adjusment listener to horiz so it can //have events processed when the scroller is moved setLayout(new BorderLayout()); add("South", horiz); horiz.addAdjustmentListener(this); } //adjustmentValueChanged() is the method that handles AdjustmentEvents //which is when the value of a Scrollbar is changed public void adjustmentValueChanged(AdjustmentEvent e){ //the next if statement checks to see if the AdjustmentEvent //came from horiz if(e.getAdjustable()==horiz){ //sets the text in num to the value of horiz gx = horiz.getValue(); repaint(); } } public void paint(Graphics g) { double x, y, delta, m, y1, y2; double r,h,k,uy; int x_int, x_int_old, y_int, y_int_old, y1_int, y2_int; float mf; g.setColor(Color.black); g.drawLine(120,0,120,440); g.drawLine(0,420,440,420); g.drawString("250",90,25); g.drawString("200",90,25+80); g.drawString("150",90,25+160); g.drawString("100",90,25+240); g.drawString("50",100,25+320); g.drawString("time",10,435); g.drawString("0.0",100,435); g.drawString("10",190,435); g.drawString("20",270,435); g.drawString("30",350,435); g.drawString("40",430,435); g.setColor(Color.blue); // Cooling Formula y= r + (h-r)e^(kx) r = (250.0/400.0)*(420 - (double)(ry)); //red y goes from 20 to 420, so convert to 250 to 0 k = (-2.0/400.0)*((double)(rx)-20); // red x goes from 20 to 420 so convert to 0 to -2.0 h = (250.0/400.0)*(420 - (double)(by)); //red y goes from 20 to 420, so convert to 250 to 0 //gx = (40.0/400.0)*((double)(gx)-20 //k = -.1627; x = -0.5; delta = .5; x_int_old = 120; y_int_old = 320; for(int i=1;i<=270;i++) { x = x + delta; y = r + (h-r)* Math.pow(Math.E,k*x); x_int = Math.round(Math.round(120+8*x)); y_int = Math.round(Math.round(420-(400.0/250.0)*y)); g.drawLine(x_int_old,y_int_old,x_int,y_int); x_int_old = x_int; y_int_old = y_int; } g.setColor(Color.blue); //g.fillOval(bx-5,by-5,10,10); g.fillOval(120-5,by-5,10,10); g.setColor(Color.red); g.fillOval(rx-5,ry-5,10,10); g.drawString("Room Temp r="+Double.toString(r),330,40); g.drawString("k = "+Double.toString(k),330,60); g.drawLine(0, ry, 500, ry); g.setColor(Color.blue); g.drawString(" Heated to h="+Double.toString(h),130,40); g.setColor(Color.magenta); uy = r + (h-r)* Math.pow(Math.E,k*gx); gy = Math.round(Math.round(420-(400.0/250.0)*uy)); g.drawString(" ("+Integer.toString(gx)+","+Double.toString(uy)+")", 120+8*gx, gy); g.drawString("u(t) = r + (h-r)e",130,60); g.drawString("kt",230,55); g.drawLine(120, gy, 120+8*gx,gy); g.drawLine(120+8*gx,420,120+8*gx,gy); } public boolean mouseDrag(Event e, int mx, int my) { // check if inside red or blue dot // put in approporiate // to ignore y if ((my < 410) || (my > 430)) return true; //check for extremes if (my < 20) my = 20; if (my > 420) my =420; if (mx < 20) mx = 20; if (mx > 420) mx =420; if (Math.abs(my-ry)<17 && Math.abs(mx-rx)<17){ rx = mx; ry = my; repaint(); return true; } if (Math.abs(my-by)<17 && Math.abs(mx-bx)<17){ bx = mx; by = my; repaint(); return true; } return true; } }