import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.lang.Math; public class function_plot extends Applet implements ActionListener{ // Applet for function-plotting. Stefan Emet 1998 TextField TextField_parameter; Choice Choice_function; Button Button_Draw; int activefunction=0,ITERATES=1; final static int COSINE=0,NONLINEAR=1, LOGISTIC=2,BAKER=3,TENT=4; double MYY=2; public void init(){ Choice_function = new Choice(); Choice_function.addItem("cos(x)"); Choice_function.addItem(".5(x^3+x)"); Choice_function.addItem("u x (1-x)"); Choice_function.addItem("baker map"); Choice_function.addItem("tent map"); add(Choice_function); add(TextField_parameter=new TextField("",3)); add(Button_Draw= new Button("Draw")); } public void paint(Graphics g){ // The width=400 and height=300 of // the Java Applet defined in funcplot.html Choice_function.move(0,0); // place the Choice resource in the nortwest corner of the Applet if (activefunction > 1){ TextField_parameter.move(195,40); Button_Draw.move(250,40); } TextField_parameter.show(activefunction > 1); Button_Draw.show(activefunction > 1); g.setColor(Color.white); g.fillRect(0,75,399,224); g.setColor(Color.black); g.drawRect(0,75,399,224); g.drawString("f(x) = ",150,17); switch (activefunction) { case COSINE: g.drawString("cos(x)",175,17); plot_COSINE(g); break; case NONLINEAR: g.drawString("0.5 (x^3+x)",175,17); plot_NONLINEAR(g); break; case LOGISTIC: g.drawString("u = ",170,54); g.drawString("u x (1-x)",175,17); TextField_parameter.setText(""+MYY); plot_LOGISTIC(g); break; case BAKER: g.drawString("2x , for x in [0 , 0.5)",186,11); g.drawString("2x-1 , for x in [0.5 , 1]",186,23); g.drawString("The n:th iterate: f (x) , n =",60,54); g.drawString("n",147,46); TextField_parameter.setText(""+ITERATES); plot_BAKER_TENT(g,ITERATES); break; case TENT: g.drawString("2x , for x in [0 , 0.5)",186,11); g.drawString("2(1-x) , for x in [0.5 , 1]",186,23); g.drawString("The n:th iterate: f (x) , n =",60,54); g.drawString("n",147,46); TextField_parameter.setText(""+ITERATES); plot_BAKER_TENT(g,ITERATES); break; } } public void plot_COSINE(Graphics g){ // plots the cosinus-function from -1 to PI in scalars // x=[-1,1], y=[-1,3.16] double x=-1,y; int xpix,ypix0,ypix1; y=Math.cos(x); ypix0=187 - (int) (y*100); for (xpix=10;xpix<=390;xpix++){ x=-1 + ((double)(xpix-9)/91); y=Math.cos(x); ypix1=187 - (int) (y*100); g.drawLine(xpix,ypix0,xpix+1,ypix1); ypix0=ypix1; } g.drawLine(100,85,100,289); // y-axis g.drawLine(10,187,390,187); // x-axis g.drawLine(190,85,10,289); // the line y=x g.drawString("f(x)=x",176,85); // i.e. f(x)=x g.drawLine(167,189,167,185); // x-tick at g.drawString("0.739",160,200); // x=0.739 (x=cos(x)) } public void plot_NONLINEAR(Graphics g){ // plots the function: .5*(x^3+x) // x=[-1.2,1.2], y=[-2,2] double x=-1.2,y; int xpix,ypix0,ypix1; y=0.5*(Math.pow(x,3)+x); ypix0=187 - (int) (y*55); for (xpix=10;xpix<=230;xpix++){ x=-1.2 + ((double)(xpix-9)/92.5); y=0.5*(Math.pow(x,3)+x); ypix1=187 - (int) (y*55); g.drawLine(xpix,ypix0,xpix+1,ypix1); ypix0=ypix1; } g.drawLine(120,80,120,290); // y-axis g.drawLine(10,187,230,187); // x-axis g.drawLine(10,253,230,121); // the line y=x g.drawLine(118,105,122,105); // y-tick at g.drawString("1.5",100,110); // y=1.5 g.drawLine(118,269,122,269); // y-tick at g.drawString("-1.5",97,274); // y=-1.5 g.drawLine(28,185,28,189); // x-tick at g.drawString("-1",23,200); // x=-1 g.drawLine(214,185,214,189); // x-tick at g.drawString("1",212,200); // x= 1 } public void plot_LOGISTIC(Graphics g){ // plots the logistic-function // pixels for x=[-0.3643,1.3], y=[-0.3643, ] double x=-0.3643,y; int xpix,ypix0,ypix1; y=MYY*x*(1-x); ypix0=230 - (int) (y*140); for (xpix=10;xpix<=250;xpix++){ x=-0.3643 + ((double)(xpix-9)/140); y=MYY*x*(1-x); ypix1=230 - (int) (y*140); if (y >= -0.365 && y <= 1.1) { g.drawLine(xpix,ypix0,xpix+1,ypix1); } ypix0=ypix1; } g.drawLine(60,85,60,289); // y-axis g.drawLine(10,230,250,230); // x-axis g.drawLine(10,280,200,90); // the line y=x g.drawLine(58,91,200,91); // the line y=1 g.drawString("1",50,95); // y=1 g.drawLine(200,232,200,91); // the line x=1 g.drawString("1",197,247); // x=1 } public void plot_BAKER_TENT(Graphics g, int iterN){ int periods, x0, x1, half_interval; double interval; periods=(int)Math.pow(2,iterN); interval=(double)170/periods; half_interval=(int)interval/2; if (activefunction==BAKER){// plots the baker-map for (int i=1; i<=periods; i++) { g.drawLine(60+(int)((i-1)*interval),260,60+(int)(i*interval),90); } } else if (activefunction==TENT){// plots the tent-map for (int i=1; i<=periods; i++) { x0=(int)((i-1)*interval)+60; x1=(int)(i*interval)+60; g.drawLine(x0,260,x0+half_interval,90); g.drawLine(x0+half_interval,90,x1,260); } } g.drawLine(60,85,60,280); // y-axis g.drawLine(60,260,360,260); // x-axis g.drawLine(60,260,230,90); // the line y=x g.drawLine(58,90,62,90); // y=1 tickmark g.drawString("1",50,95); // y=1 g.drawLine(230,258,230,262);// x=1 tickmark g.drawString("1",227,274); // x=1 } public void actionPerformed(ActionEvent e){ Object source=e.getSource(); if (source.equals(Choice_function)){ activefunction=Choice_function.getSelectedIndex(); } // Check if you have pressed the "Draw"-button: else if(source.equals(Button_Draw)){ if (activefunction == LOGISTIC) MYY=Double.valueOf(TextField_parameter.getText()).doubleValue(); else ITERATES=Math.abs((int)Math.round(Double.valueOf(TextField_parameter.getText()).doubleValue())); } repaint(); } }// end of class function_plot