Back to index

wims  3.65+svn20090927
sinus.java
Go to the documentation of this file.
00001 /*
00002  modified source of "j.-p. Quelen"
00003  jdk1.2 - double-buffering
00004 ---------------------------------------------------------
00005 <html>
00006     <body>
00007        <script language="javascript" type="text/javascript">
00008            function sendanswer(){
00009               var data=document.applets[0].ReadApplet();
00010               var test=data.indexOf("error");
00011               if(test!=-1){alert("Your answer will not be send...\nthe next objects could not be detected in your drawing...\n\n:"+data); return;}
00012               else{
00013                  var answer=data.split(',');
00014                  alert("technical answer: \n"+answer[0]);
00015                  alert("unicode answer: \n"+answer[1]);
00016                  alert("unicode answer: \n"+answer[2]); 
00017                   <!-- confirm("Send this ?\n"+data);
00018                      if(t==true){
00019                          data=encodeURIComponent(data);
00020                          var URL = '$wims_ref_name?session=$session&+moudule=$module&+cmd=reply&+reply$n='+data ;document.location.href = URL
00021                   -->
00022                   }
00023               }
00024            }                                                                                                                                          
00025        </script>
00026        <p align=center>
00027            <applet id="sinus" codebase="build" archive="sinus.jar" code="sinus.class" WIDTH="800" HEIGHT="400" ALIGN="CENTER" MAYSCRIPT>
00028               <param name="language" value="nl">
00029               <param name="applettext" value="a string">
00030               <param name="degrees" value="rad">
00031                <param name="functiontype" value="sin">
00032                <param name="show_curve" value="yes">
00033               <param name="start" value="180">
00034                <param name="end" value="720">
00035                <param name="Xaxis_units" value="yes">
00036               No java
00037            </applet>
00038            <br> 
00039            <input type="button" onclick="javascript:sendanswer()" name="TEST" value="TEST TEST">
00040        </p>
00041     </body>
00042 </html>
00043 ---------------------------------------------------------
00044 reply string = degrees+","+radialen+"\u03C0"+","+degrees+"\u00B0";
00045 item 1 = the answer in degrees
00046 item 2 = the unicode view answer in radians
00047 item 3 = the unicode view answer in degrees
00048 ---------------------------------------------------------
00049 <param name="language" value="nl/fr/en"> default en
00050 <param name="applettext" value="a string">
00051 <param name="show_curve" value="yes/no">
00052 <param name="type" value="sin/cos"> default sin
00053 <param name="degrees" value="yes/rad/no"> default yes or radians or no
00054 <param name="Xaxis_units" value="yes/no"> default yes using degrees or radians depending on "param degrees"
00055 <param name="start" value="begin value of point P in degrees"> default 0
00056 <param name="end" value="end value of point P in degrees"> default no value
00057 
00058 attention: no "try / catch" is applied to the params ! 
00059 
00060 */
00061 
00062 
00063 
00064 import java.awt.event.* ;
00065 import java.awt.* ;
00066 import java.applet.* ;
00067 import geo.* ;
00068 
00069 public class sinus extends java.applet.Applet implements MouseListener, MouseMotionListener{
00070     Image img ;Graphics g ;Graphics g1 ;
00071     Repere R, R1 ;
00072     pt O,P,Px,Py,Pcosx,endpoint1,endpoint2,pp1,pp2;
00073     PointSurDroite O1;
00074     Cercle C, Cenr ;
00075     ArcDeCercle IM ;
00076     Droite xas, yas ,hline;
00077     PointSurCercle M ;
00078     Vecteur Endpoint;
00079     Segment straal,OPx,OPy,value,proj ;
00080     CourbeDeFonction functie ; sin sfunctie ;cos cfunctie;
00081     int aNp, periodes = 0 ;double ap = 1.0, pi2 ;int X, Y, Xret, Yret ;int modepaint = -1 ;
00082     double degrees;double absdegrees=0;int use_degrees=1;double start=0;double end=0;int type=0;
00083     int xz;int yz;double scale=7.0;double minscale=7.0;int use_end=0;int units=1;int curve=1;
00084     double yP;String rem1="Only point P [on the circle] can be rotated...";String rem2="zooming out...";String rem3="\u03C0 radians";
00085     String rem4="the angle = ";String rem5=" The unit circle";
00086     
00087     
00088     public void init(){
00089        String param;
00090        param=getParameter("language");if(param!=null && param.length()>0){
00091            if(param.equalsIgnoreCase("nl")){
00092               rem1="Alleen het punt P [op de cirkel] kan worden bewogen...";
00093               rem2="uitzoomen...";
00094               rem3="\u03C0 radialen";
00095               rem4="de hoek = ";
00096               rem5=" De eenheids cirkel";
00097            }
00098            if(param.equalsIgnoreCase("fr")){
00099               rem1="Seulement point P [sur le cercle] peut être tourné ...";
00100               rem2="zoomer en plan général...";
00101               rem3="\u03C0 radians";
00102                rem4="angle = ";
00103               rem5=" Cercle unité";
00104            }
00105        }
00106        param=getParameter("functiontype");if(param!=null && param.length()>0){if(param.equalsIgnoreCase("cos")){type=1;}}
00107        param=getParameter("show_curve");if(param!=null && param.length()>0){if(param.equalsIgnoreCase("no")){curve=0;scale=4;minscale=4;}}
00108        param=getParameter("Xaxis_units") ;if(param!=null && param.length()>0){if(param.equalsIgnoreCase("no" )){units=0;}}
00109         param=getParameter("degrees");
00110        if(param!=null && param.length()>0){
00111            if(param.equalsIgnoreCase("no")){
00112               use_degrees=0;
00113            }
00114            else
00115            {  
00116               if(param.equalsIgnoreCase("radians") || param.equalsIgnoreCase("rad") ){use_degrees=2;}
00117            }
00118        }
00119         param=getParameter("start");if(param!=null && param.length()>0){start=(double) Integer.parseInt(param, 10);  }
00120         param=getParameter("end");if(param!=null && param.length()>0){end=(double) Integer.parseInt(param, 10); use_end=1;}   
00121        setBackground(Color.white);modepaint = 1 ;
00122        periodes = 0 ;
00123        pi2 = Math.PI * 2.0;
00124        img = createImage(getSize().width, getSize().height);g = img.getGraphics();
00125        addMouseMotionListener(this);addMouseListener(this);
00126        xz=getSize().width/2;yz=getSize().height/2;
00127        R   = new Repere(xz,yz, getSize().width, getSize().height,(double)(getSize().height / 2 - 20),(double)(getSize().height / 2 - 20));
00128        R1  = new Repere(xz,yz, getSize().width, getSize().height,getSize().width/scale,getSize().width/scale);
00129        xas = new Droite(0.0, 1.0, 0.0, "", R1);
00130        yas = new Droite(1.0, 0.0, 0.0, "", R1);
00131        O1  = new PointSurDroite(xz,yz, xas, "O", R1);
00132        O   = new PointSurDroite(xz,yz, xas, "O", R1);
00133        C   = new Cercle(O1, 1.0, "", R1);
00134        Cenr= new Cercle(O1, 1.0, "", R1);
00135        M   = new PointSurCercle(start*pi2/360, C, "P", R1);
00136        straal= new Segment(O1.x,O1.y,M.x,M.y,"",R1);
00137        Px  = new point(M.x,0, "P'", R1);
00138        Py  = new point(0,M.y, "P'", R1);
00139        proj= new Segment(Px.x,Px.y,M.x,M.y,"",R1);
00140        OPx= new Segment(O1.x,O1.y,M.x,0,"cos",R1);
00141        OPy= new Segment(O1.x,O1.y,0,M.y,"sin",R1);
00142        IM = new ArcDeCercle(0.0, 0.0, 1.0, 0.0, 1.0, "", R1);
00143        if(type==0){
00144            P = new pt(O1.x+1.0,M.y, "", R1);
00145            sfunctie = new sin();
00146            functie = new CourbeDeFonction(sfunctie, "", R1);;
00147        }
00148        else
00149        {
00150            P = new pt(O1.y + 1.0, M.y, "", R1);
00151            cfunctie = new cos();
00152            functie = new CourbeDeFonction(cfunctie, "", R1);
00153        }
00154        
00155        hline=new Droite(O1.x,O1.y,M.x,M.y,"",R1);
00156        if(use_end==1){
00157            endpoint2 = new point(end*pi2/360,0.0,"",R1);
00158            endpoint1 = new point(end*pi2/360,getSize().height,"",R1);
00159            Endpoint = new Vecteur(endpoint1,endpoint2, "end",R1);
00160        }
00161        Pcosx  = new point(P.x,0, "P''", R1);
00162        value= new Segment(P.x,P.y,P.x,0,"",R1);
00163     }
00164     
00165     
00166 
00167     public void paint(Graphics g1){
00168        Font f = new Font("Arial", Font.PLAIN, 10);g.setFont(f);g.setColor(getBackground());g.fillRect(0, 0, R1.XMAX, R1.YMAX);
00169        double Ma = M.a + pi2 *(double)(periodes);degrees=Ma*(360/(pi2));double Maa = M.a ;aNp = Math.abs(periodes);
00170        if((M.a < 0.0) &&(periodes > 0)){ aNp -- ;Maa = Maa + pi2 ;} else { if((M.a > 0.0) &&(periodes < 0)){ aNp -- ;Maa = Maa - pi2 ;}}
00171        g.setColor(Color.gray);
00172        for(int i = 0 ; i < aNp ; i ++){ Cenr.Cercle(0.0, 0.0, 1.0 + R1.Abs(i + R1.X0));Cenr.trace(g); }
00173        IM.trace(g); IM.ArcDeCercle(0.0, 0.0, 1.0 + R1.Abs(aNp + R1.X0), 0.0, Maa);
00174        g.setColor(Color.gray);     xas.trace(g);yas.trace(g);
00175        g.setColor(Color.green);
00176        straal.Segment(O1,M);straal.trace(g);
00177        if(type==0){
00178            g.setColor(Color.red);
00179            Py.point(0,M.y);Py.trace(g);Py.traceNom(g);
00180            OPy.Segment(O1,Py);OPy.trace(g);
00181            proj.Segment(M,Py);proj.trace(g);
00182        }
00183        else
00184        {
00185            g.setColor(Color.red);
00186            Px.point(M.x,0);Px.trace(g);Px.traceNom(g);
00187            OPx.Segment(O1,Px);OPx.trace(g);
00188            g.setColor(Color.green);
00189            proj.Segment(M,Px);proj.trace(g);
00190        }
00191        if(curve==1){
00192            if(units==1 && aNp<10){
00193                g.setColor(Color.gray);
00194               if(use_degrees==1){
00195                   for(int i=1; i<(aNp+4);i++){
00196                      pp1=new point(i*pi2,0.0,(i*360)+"\u00B0",R1);
00197                       pp2=new point(-1.0*i*pi2,0.0,"-"+(i*360)+"\u00B0",R1);
00198                      pp1.trace(g);pp2.trace(g);      
00199                   }      
00200               }
00201               else
00202               {
00203                   for(int i=1; i<(aNp+4);i++){
00204                      pp1=new point(i*pi2,0.0,2*i+"\u03C0",R1);
00205                      pp2=new point(-1.0*i*pi2,0.0,"-"+2*i+"\u03C0",R1);
00206                      pp1.trace(g);pp2.trace(g);      
00207                   }
00208               }
00209            }
00210            g.setColor(Color.red);
00211            if(type==1){
00212               yP=(double) Math.cos(O1.x + Ma);
00213               P.point(O1.x + Ma, yP);
00214            }
00215            else
00216            {
00217               yP=(double) Math.sin(O1.x + Ma);
00218               P.point(O1.x + Ma, yP);
00219               hline.Droite(P,M);hline.trace(g);
00220            }
00221            Pcosx.point(P.x,0);Pcosx.trace(g);
00222            value.Segment(Pcosx,P);value.trace(g);
00223            if(use_end==1){g.setColor(Color.green);Endpoint.trace(g);}
00224            g.setColor(Color.orange);
00225            functie.trace(g);
00226        }
00227        g.setColor(Color.black);
00228        g.drawString(rem5,getSize().width/2,20);
00229        if(use_degrees==1){g.drawString(rem4+(double)(Math.round(10*degrees))/10 +"\u00B0",getSize().width/2, getSize().height - 30);}
00230        if(use_degrees==2){g.drawString(rem4+(double)(Math.round(1000*degrees/180))/1000 +rem3,getSize().width/2, getSize().height - 30);}
00231        if(absdegrees>180){g.drawString(rem2,getSize().width/2, getSize().height - 50);}
00232        C.trace(g);
00233        R1.cadre(g);
00234        P.trace(g);P.traceNom(g);
00235        if(modepaint==2){
00236            g.setColor(Color.red);
00237            g.drawString(rem1,getSize().width/2, getSize().height - 40);
00238        }
00239        if(modepaint==0){g.setColor(Color.red);}
00240        M.trace(g);
00241        g1.drawImage(img, 0, 0, this);
00242   }
00243 
00244   public void mousePressed(MouseEvent e){
00245     e.consume();
00246     if( M.zone(e.getX(), e.getY()) ){ 
00247        modepaint = 0 ;
00248        X = -1 ;
00249        repaint();
00250     }
00251     else
00252     {
00253        modepaint=2;
00254     }
00255   }
00256 
00257     public void update(Graphics g1){paint(g1);}
00258 
00259   public void mouseDragged(MouseEvent e)
00260   { e.consume();
00261     M.bouge(e.getX(), e.getY());
00262     if(M.a * ap < 0.0){
00263        if(ap > 2.0){
00264            periodes ++ ;
00265        }
00266        else 
00267         {
00268            if(ap < -2.0){
00269               periodes -- ;
00270            }
00271        }
00272     }
00273     ap = M.a ;
00274     if(curve==1){
00275        absdegrees=Math.abs(degrees);
00276        if(absdegrees>180){scale=(absdegrees/180)*minscale;}
00277        R1.Repere(xz,yz, getSize().width, getSize().height,getSize().width/scale,getSize().width/scale);
00278     }
00279     O1.bouge(e.getX(), e.getY());
00280     repaint();
00281   }
00282 
00283   public void mouseReleased(MouseEvent e)
00284   { e.consume();
00285     M.deplace = O1.deplace = false ; modepaint = 1 ;
00286     repaint();
00287   }
00288 
00289   public void mouseMoved(MouseEvent e){
00290     e.consume();
00291     if(modepaint == 1){ 
00292        if( M.zone(e.getX(), e.getY())){ 
00293            this.X = e.getX() + 2 ; this.Y = e.getY() + 2 ;
00294            M.deplace = false ;
00295        }
00296        Xret = e.getX(); Yret = e.getY();
00297        repaint();
00298     }
00299   }
00300 
00301   public void mouseClicked(MouseEvent e) { }
00302   public void mouseEntered(MouseEvent e) { }
00303   public void mouseExited(MouseEvent e) { }
00304 
00305   public String getAppletInfo()
00306   { return "Written by J.P. Quelen\nModified for WIMS \n J.M. Evers 3/2008" ; }
00307 
00308     protected class sin extends Fonction{
00309        public boolean defini(double x){ return true ; }
00310        public double Image(double x){ return Math.sin(x); }
00311     }
00312 
00313     protected class cos extends Fonction{
00314        public boolean defini(double x){ return true ; }
00315        public double Image(double x){ return Math.cos(x); }
00316     }
00317     
00318     // reading the degrees value of the applet by javascript
00319     public String ReadApplet(){
00320        double radialen=(Math.round(100.0*degrees/180.0))/100.0;
00321        double deg=Math.round(degrees);
00322        String reply=degrees+","+rem4+radialen+rem3+","+rem4+deg+"\u00B0";
00323        return reply;
00324     }
00325     
00326     public void start(){}
00327     public void stop(){}
00328     public void destroy(){}
00329 }