// // riemansums1.java // riemansums1 // // Created by Chris Thiel on Sun Jan 26 2003. // Copyright (c) 2003. All rights reserved. // Free to use for education or by permission. // A simple Java applet that demonstrates the // notion of Riemann sums. // import java.awt.*; import java.applet.Applet; import java.io.*; import java.awt.Graphics; import java.awt.event.*; import java.lang.Math; public class riemansums1 extends Applet implements Runnable, AdjustmentListener { int XMAX=400; int YMAX=250; int BOXMAX=150; //the next line creates the scrollbar used in this applet //with a max value of XMAX, and a min value of 1 //takes integral from to to horiz scroller Scrollbar horiz0=new Scrollbar(Scrollbar.HORIZONTAL,1,20,-170,180); Scrollbar horiz1=new Scrollbar(Scrollbar.HORIZONTAL,1,20,-170,180); //the vertical scroll controls the number of rectangles //add 20 to maximum boxes to allow for the size of the arrows on either end Scrollbar vert=new Scrollbar(Scrollbar.VERTICAL,1,20,0,BOXMAX+20); Checkbox integrl=new Checkbox("Show actual area between axis and curve"); final int quantum = 10; Image offScreenImage=null; Graphics offScreenGraphics = null; Dimension offScreenSize = null; Thread t; int h = 100; int h0 = 0; int v = 0; double boxArea = 0; double integral = 0; double a,b; double delta; public void init() { Image img=null; h=100; h0=0; //the next sets the layout as a BorderLayout setLayout(new BorderLayout()); //the next line adds horiz to South add("South",horiz0); add("North",horiz1); add("East",vert); // add("North",integrl); //the next line adds an adjusment listener to horiz so it can //have events processed when the scroller is moved horiz0.addAdjustmentListener(this); horiz1.addAdjustmentListener(this); vert.addAdjustmentListener(this); vert.setValue(BOXMAX);//Put vertical slider on bottom horiz0.setValue(0); horiz1.setValue(100); } //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()==horiz0){ //sets the text in num to the value of horiz h0 = horiz0.getValue(); } if(e.getAdjustable()==horiz1){ //sets the text in num to the value of horiz h = horiz1.getValue(); } if(e.getAdjustable()==vert){ //up is down in vertical location so subtract vertical sider location //from the max to put things in Cartsian order, where small is below v = BOXMAX-vert.getValue(); } } public void run(){ while (true) { update(getGraphics()); try { Thread.sleep(quantum); }catch (InterruptedException e) { break; } }// of while } // of run public void start() { t = new Thread(this); t.start(); } public void stop() { t.stop(); t = null; } public final void update (Graphics theG) { //implements no flickers Dimension dim = getSize(); if ((offScreenImage==null)||(dim.width != offScreenSize.width)||(dim.height !=offScreenSize.height)) { offScreenImage = createImage(dim.width, dim.height); offScreenSize = dim; offScreenGraphics = offScreenImage.getGraphics(); } //first clear old offScreenGraphics.clearRect(0,0,offScreenSize.width, offScreenSize.height); //now draw new paint(offScreenGraphics); //now put the offScreen Image on Screen! theG.drawImage(offScreenImage, 0,0,null); }// update //convert graph coords to screen pixel location private int screenX (double x) { return (int)(.5*XMAX+x*(XMAX/32)); } private int screenY (double y) { return +1*(int)(.5*YMAX-y*(YMAX/22)); } //Find f(x) in terms of a screen coord // ***** change this for a different function ***** private int f(double x){ // the vertical screen coords go in the opposite direction as // the Cartisian Coordinate system, so make y values the // opposite sign. return screenY(0.04*x*x ); } //Find f(x) in terms of 10 time the graph system coords // change this for a different function. private double f1(double x){ return 0.04*x*x; } private int d(double x){ // ***** change this for a different function ***** // to be the deritative of f return (int)(10*( 0.08*x)); } private double dIntegral(double a, double b){ return (b*b*b-a*a*a)/75.0; } public void paint( Graphics g ) { g.setColor(new Color(0x108000)); //green background from (0,20) to (450,295) g.fillRect ( 0, 0, XMAX, YMAX); g.setColor(new Color(0x08602F)); //lines for(int i = -16; i< 17; i++) g.drawLine( screenX(i),0,screenX(i) ,YMAX); // horizontals for(int i = -11; i< 11; i++) g.drawLine( 0, screenY(i), XMAX,screenY(i) ); //virticals g.setColor(Color.black); g.drawLine( 0, screenY(0), XMAX,screenY(0) ); // x-axis g.drawLine( screenX(0),0,screenX(0) ,YMAX); // y=axis //draw boxes between curve and x-axis if (v!=0) { g.setColor(Color.yellow); delta=Math.abs((h-h0)/(10.0*v)); if (h < h0){ a=h/10.0; b=h0/10.0; }else{ a=h0/10.0; b=h/10.0; } boxArea=0; for(double x = a; x < b-(.5*delta); x=x+delta) { g.drawLine(screenX(x),f(x+delta),screenX(x+delta),f(x+delta) ); g.drawLine(screenX(x),f(x+delta),screenX(x),screenY(0) ); g.drawLine(screenX(x+delta),f(x+delta),screenX(x+delta),screenY(0) ); boxArea=boxArea+delta*f1(x+delta); } } //draw boundry of interval g.setColor(Color.red); g.drawLine(screenX(h/10.0), screenY(0) ,screenX(h/10.0),f(h/10.0)); g.setColor(Color.blue); g.drawLine(screenX(h0/10.0), screenY(0) ,screenX(h0/10.0),f(h0/10.0)); //Draw the curve g.setColor(Color.white); for(double x = -17; x<17; x=x+.25) g.drawLine(screenX(x),f(x),screenX(x+.25),f(x+.25) ); //Dislay info g.setColor(Color.red); g.drawString( " b = "+h/10.0, 220, 155 ); g.setColor(Color.blue); g.drawString( " a = "+h0/10.0, 220, 170 ); //g.drawString( " f(x) = "+f1(h/10.0)/100.0, 220, 170 ); if (v!=0) { g.setColor(Color.yellow); g.drawString( "# of boxes = "+v, 220, 185 ); g.drawString( "width of box = "+delta, 206, 200 ); g.drawString( "sum of box area = "+boxArea, 206, 215 ); } if(true){ g.setColor(Color.cyan); g.drawString("Integral from a to b ="+dIntegral(a,b), 206, 230); } // Plot point (beginning interval ) g.setColor(Color.blue); g.fillOval(screenX((h0/10.0))-2,f(h0/10.0)-2,5,5); //Plot point (x, f(x) ) g.setColor(Color.red); g.fillOval(screenX(h/10.0)-2,f(h/10.0)-2,5,5); //instructions g.setColor(Color.black); g.drawString("Change a with the bottom bar",20,YMAX-55); g.drawString("Change b with the top bar",20,YMAX-40); g.drawString("Change Æ with the vertical bar",20,YMAX-25); } }