// // parabola.java // // // Created by Chris Thiel on Tue September 20, 2004. // Copyright (c) 2004 Chris Thiel. // Free for Educational Use (with permission). All rights reserved. // A simple Java applet (520 x 300) // that uses three sliders to control the three coeefs of a quadratic function // Updated 26 Sep 2005 - fixed "typos" import java.awt.*; import java.awt.event.*; import java.applet.*; import java.lang.*; public class parabola extends Applet implements MouseMotionListener { static final String message = "Hello World!"; static final int S3_LOCATION =508; static final int S2_LOCATION =450; static final int S1_LOCATION =398; static final int SLIDER_LENGTH = 200; static final int SLIDER_TOP=90; int SLIDER_BOTTOM=SLIDER_TOP+SLIDER_LENGTH; private Font font = new Font("sansserif", Font.ITALIC + Font.BOLD, 12); int ax, ay, bx, by, cx, cy,j; double a,b,c, sx, sy, fociX, fociY; double h,k,r1,r2; // parabola vars for vertex and roots public void init() { setLayout (null); this.setBackground(new Color(0xFFEEAA)); addMouseMotionListener(this); a=1.0; b=4.0; c=-5.0; ax=S1_LOCATION; ay=SLIDER_TOP+99; bx=S2_LOCATION; by=SLIDER_TOP+99; cx=S3_LOCATION; cy=SLIDER_TOP+99; } private double f(double x){ return 1.0*a*x*x+b*x+c; } private int screenX (double x){ return (int)(160+10*x+20); //converts -15=0){ g.setColor(Color.orange); r1=(-1.0*b-Math.sqrt(d2))/(2.0*a); r2=(-1.0*b+Math.sqrt(d2))/(2.0*a); g.drawString("1st root: "+r1,30,275); g.drawString("2nd root: "+r2,25,290); } if (d2<0){ // g.setColor(new Color(0x3F1212));//dark brown g.setColor(Color.pink); r1=(-1.0*b)/(2.0*a); r2=Math.sqrt(-1.0*d2)/(2.0*a); g.drawString("Complex roots: ", 30,275); g.drawString(r1+" ± "+r2+" i",30,290); } g.setFont (new Font("sansserif", Font.ITALIC+Font.BOLD, 14)); g.setColor(new Color(0x108000)); //dark green g.drawString("f(x)="+a+"(x-("+h+"))^2 + ("+k+")",360 ,45 ); // // //Draw the axis of symmetry g.setColor(Color.orange); g.drawLine(screenX(h),0 , screenX(h), 300); // // Draw the parabola g.setColor(Color.cyan); for (double i=-17.0;i<18.0;i=i+.5){ g.drawLine(screenX(i),screenY(f(i)),screenX(i+.5), screenY(f(i+.5))); } // // Draw vertex g.setColor(Color.yellow); g.drawOval(screenX(h)-1,screenY(k)-1,2,2); }//end of paraboloa drawing // Draw Controls g.setColor(Color.red); g.fillOval(ax-5,ay-5,10,10); g.setColor(Color.blue); g.fillOval(bx-5,by-5,10,10); g.setColor(Color.magenta); g.fillOval(cx-5,cy-5,10,10); g.setColor(Color.black); g.setFont (new Font("serif", Font.ITALIC + Font.BOLD, 16)); g.setColor(Color.black); g.drawString("f(x)=", 352, 28); if(a!=0)g.drawString("x", 415, 28); if (b!=0) g.drawString("x", 478, 28); g.setFont (new Font("serif", Font.BOLD, 12)); if (a!=0)g.drawString("2", 422, 21); // Draw the Equation in Standard form g.setFont (new Font("sansserif", Font.BOLD, 16)); g.setColor(Color.red); if (a!=0) g.drawString(Math.abs(a)+"", 390, 28); g.setColor(Color.blue); if (b!=0) g.drawString(Math.abs(b)+"", 445, 28); g.setColor(Color.magenta); if (c!=0) g.drawString(Math.abs(c)+" ",500, 28); g.setColor(Color.black); g.setFont (new Font("serif", Font.BOLD, 20)); if (a<0) { g.setColor(Color.red); g.drawString("-", 385, 28); g.setColor(Color.black); } if (b<0) g.drawString("-",430, 28); if (b>0) g.drawString("+", 430, 30); if (c<0) g.drawString("-",488, 28); if (c>0) g.drawString("+", 488, 30); g.setFont (new Font("serif", Font.ITALIC + Font.BOLD, 12)); g.drawString("a = "+a,S1_LOCATION-15, SLIDER_TOP-12); g.drawString("b = "+b,S2_LOCATION-15, SLIDER_TOP-12); g.drawString("c = "+c,S3_LOCATION-15, SLIDER_TOP-12); g.drawLine(S1_LOCATION,SLIDER_TOP,S1_LOCATION,SLIDER_BOTTOM); g.drawLine(S2_LOCATION,SLIDER_TOP,S2_LOCATION,SLIDER_BOTTOM); g.drawLine(S3_LOCATION,SLIDER_TOP,S3_LOCATION,SLIDER_BOTTOM); for(int j=0;j<21;j=j+5){ g.drawLine(S1_LOCATION-6,SLIDER_TOP+j*SLIDER_LENGTH/20,S1_LOCATION+6,SLIDER_TOP+j*SLIDER_LENGTH/20); g.drawLine(S2_LOCATION-6,SLIDER_TOP+j*SLIDER_LENGTH/20,S2_LOCATION+6,SLIDER_TOP+j*SLIDER_LENGTH/20); g.drawLine(S3_LOCATION-6,SLIDER_TOP+j*SLIDER_LENGTH/20,S3_LOCATION+6,SLIDER_TOP+j*SLIDER_LENGTH/20); } } public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { int mx=e.getX(); int my=e.getY(); // check if inside red or blue dot // put in approporiate //if (Math.abs(my-ry)<17 && Math.abs(mx-rx)<17){ if (my>SLIDER_TOP-17 && Math.abs(mx-S1_LOCATION)<17){ ax = S1_LOCATION; if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM; if (my=SLIDER_TOP-17 && Math.abs(mx-S2_LOCATION)<17){ bx = S2_LOCATION; if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM; if (my=SLIDER_TOP-17 && Math.abs(mx-S3_LOCATION)<17){ cx = S3_LOCATION; if (my>SLIDER_BOTTOM) my=SLIDER_BOTTOM; if (my