import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class Tower extends Applet { CurvePanel mainpanel; Button restartButton; Panel buttonPanel = new Panel(); Checkbox tangentBox, secantBox; int numTanPts = 0; int numSecPts = 0; double slope,y0d,y1d; int x0,y0,x1,y1,x2,y2,x3,y3; CheckboxGroup cbg; public Point clickPoint = null; int[] tangentPt = new int[11]; int[] secantPt = new int[11]; private static final int RADIUS = 3; Color tan_col = new Color(.7f,.6f,.4f); boolean clickFlag = false; boolean tangentLast; int slopeInt,slopeFrac; String slopeString; ActionListener restartListener = new ActionListener() { public void actionPerformed (ActionEvent e) { numTanPts = 0; numSecPts = 0; tangentBox.setState(false); secantBox.setState(true); mainpanel.repaint(); } }; double f(double x) {return 320-(10-5*(x-50)*(x-50)/250/250)*30 ;} public void init() { setBackground(Color.white); int numTanPts = 0; int numSecPts = 0; mainpanel = new CurvePanel(); setLayout(new BorderLayout()); add("Center",mainpanel); mainpanel.init(); add("South",buttonPanel); buttonPanel.setLayout(new GridLayout(1,3)); cbg = new CheckboxGroup(); tangentBox = new Checkbox("Tangent Lines", cbg, false); secantBox = new Checkbox("Secant Lines", cbg, true); restartButton = new Button("restart"); restartButton.addActionListener(restartListener); buttonPanel.add(tangentBox); buttonPanel.add(secantBox); buttonPanel.add(restartButton); } public class CurvePanel extends Panel { public void init(){ addMouseListener(new TowerMouseAdapter()); } class TowerMouseAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e){ clickPoint = e.getPoint(); clickFlag = true; if ((clickPoint.x>=50) && (clickPoint.x<404)){ if (tangentBox.getState() && numTanPts<10) { numTanPts++; tangentPt[numTanPts] = clickPoint.x; tangentLast=true; } else { if (secantBox.getState() && numSecPts<10) { if ((numSecPts%2==0) || (secantPt[numSecPts]!=clickPoint.x)){ //avoid double pt. numSecPts++; secantPt[numSecPts] = clickPoint.x; if(numSecPts%2==0) tangentLast=false; } } } } repaint(); } } public void paint(Graphics g){ g.setColor(Color.black); g.drawRect(0,0,499,399); g.drawLine(0, 320, 485, 320); //t-axis g.drawLine(50, 0, 50, 340); //y-axis for (int i = 0; i<=10; i++) { //y-ticks int y=320-i*30; g.drawLine(45, y, 55, y); g.drawString(""+i,34,y); } for (int i=1; i<18; i++) { //t-ticks int x=i*25+50; g.drawLine(x,315,x,325); int tenths = i%10; int ones = i/10; g.drawString(""+ones+"."+tenths,x-7,335); } g.drawString("t",485,310); g.drawString("y",55,10); g.setColor(Color.yellow); for (int i = 1; i<=10; i++) { //y-grid int y=320-i*30; g.drawLine(55, y, 485, y); } for (int i=1; i<18; i++) { //t-grid int x=i*25+50; g.drawLine(x,315,x,0); } g.setColor(Color.blue); for (int x = 50 ; x < 404 ; x=x+5) { //curve g.drawLine(x, (int)f(x), x+5, (int)f(x+5)); g.drawLine(x, (int)f(x)+1, x+5, (int)f(x+5)+1); g.drawLine(x, (int)f(x)-1, x+5, (int)f(x+5)-1); } g.setColor(tan_col); for (int i = 1; i <= numTanPts; i++) { //tangent lines x0 = tangentPt[i]; y0 = (int) f(tangentPt[i]); g.fillOval(x0-RADIUS,y0-RADIUS,2*RADIUS,2*RADIUS); slope = 6*(x0-50.0)/1250.0; x1=50; y1=(int) (y0 + slope*(x1-x0)); x2=500;y2=(int) (y0 + slope*(x2-x0)); g.drawLine(x1,y1,x2,y2); } g.setColor(Color.red); for (int i = 1; i <= numSecPts; i++) { //secant lines g.fillOval(secantPt[i]-RADIUS,(int) f(secantPt[i])-RADIUS,2*RADIUS,2*RADIUS); if (i%2==0){x0 = secantPt[i-1]; y0d = f(secantPt[i-1]); //two pts. per secant x1 = secantPt[i]; y1d = f(secantPt[i]); slope = (y1d-y0d)/(x1-x0); x2 = 50; y2 = (int) (y0d+slope*(x2-x0)); x3=500; y3=(int) (y0d+slope*(x3-x0)); g.drawLine(x2,y2,x3,y3); } } g.setColor(Color.black); if (numTanPts>0 || numSecPts>1) { if (tangentLast) { x0 = tangentPt[numTanPts]; slope = 6*(x0-50.0)/1250.0; } else{ if (numSecPts%2==0){ x0 = secantPt[numSecPts-1]; y0d = f(secantPt[numSecPts-1]); x1 = secantPt[numSecPts]; y1d = f(secantPt[numSecPts]); slope = (y1d-y0d)/(x1-x0); } } g.drawString("Slope of last line drawn = "+String.valueOf(((int)(-slope*100*25/3))/100.0)+" ",30,375); } } }