Back to index

wims  3.65+svn20090927
GraphWindow.java
Go to the documentation of this file.
00001 import java.applet.*;
00002 import javax.swing.*;
00003 import java.awt.*;
00004 import org.javia.arity.Symbols;                                                                                                            
00005 import org.javia.arity.SyntaxException;
00006 
00007 
00008 public class GraphWindow extends javax.swing.JFrame {  
00009 
00010     public void plotfunction(String fun) {
00011        Symbols symbols=new Symbols();
00012        int c=0;
00013        xmin=-5;
00014        xmax=5;
00015        xpix=getSize().width;
00016        ypix=getSize().height;
00017        dx=xmax-xmin;
00018        steps=(int)(10*(dx));
00019        xdata=new double[steps+10];
00020        ydata=new double[steps+10];
00021        ymax=1.0;ymin=-1.0;String Y;double y;
00022         for (double x = xmin ; x < xmax ; x=x+0.1) {
00023            Y=fun.replaceAll("x","("+x+")");
00024            try { y=symbols.eval( Y );} catch (SyntaxException e ) {y=0.0;System.out.println("error "+e); }
00025            if(y>ymax){ymax=y;}
00026            if(y<ymin){ymin=y;}
00027            xdata[c]=(double) (x*(xpix/(dx)) -  xmin*xpix/(dx));
00028            ydata[c]=y;
00029            c++;
00030         }
00031        ymax=1.2*ymax;
00032        ymin=1.2*ymin;
00033        dy=(ymax - ymin);
00034        if(dy==0){dy=100.0;}
00035        for(c=0;c<steps;c++){
00036            ydata[c]=(double) (ydata[c]*(-0.6)*(ypix)/(dy) + (0.6*ypix)*ymax/(dy)) + 0.3*ypix;
00037        }
00038        repaint();
00039     } 
00040                                                                                                                                            
00041     public void paint(Graphics g){
00042        g.setColor(bgcolor3);
00043        g.fillRect(0,0,xpix,ypix);
00044        g.setColor(Color.red);
00045        for(int c=0;c<steps-1;c++){
00046             g.drawLine((int)xdata[c], (int)ydata[c], (int)xdata[c+1], (int)ydata[c+1]);
00047        }      
00048        g.setColor(Color.blue);
00049         g.drawLine(0,(int)((0.6*ypix)*ymax/(dy) + 0.3*ypix),xpix,(int)((0.6*ypix)*ymax/(dy) + 0.3*ypix));
00050         g.drawLine((int)(-1*xmin*xpix/dx),0,(int)(-1*xmin*xpix/(dx)),ypix );
00051      } 
00052     
00053     
00054     double[] xdata;
00055     double[] ydata;
00056     int xpix;
00057     int ypix;
00058     double xmax;
00059     double xmin;
00060     double ymax;
00061     double ymin;
00062     double dy;
00063     double dx;
00064     int steps;
00065     Color bgcolor3=new Color(164,235,60);                                                                                              
00066 
00067 }