import java.awt.*;
import java.applet.*;
public class apple0603 extends Applet{
double f(double x) { // 作成するグラフの関数
return (Math.sin(2*x)+Math.cos(3*x));
} //end f
public void paint(Graphics g) {
double x0,x1,y0,y1,xd,yd;
double max=5.0; // X,Y軸の最大値
int w,h,hw,hh;
w=getSize().width; //(a) 横の長さ
h=getSize().height; //(b) 縦の長さ
hw=w/2; hh=h/2; // グラフの中心座標
g.drawString("Sin[2x]+Cos[3x]",10,15);
g.drawString("0",hw+5,hh+12);
g.setColor(Color.red);
g.drawString(""+max,hw-20,12); // y軸の最大値
g.drawString(""+max,w-20,hh+12); // x軸の最大値
g.drawLine(0,hh,w,hh); // x軸
g.drawLine(hw,0,hw,h); // y軸
xd=2*max/w; // 刻み幅
yd=hh/max;
g.setColor(Color.blue);
for (int x=0 ; x<w-1; x++) { //(c) 関数の描画
x0=-max+x*xd; y0=f(x0)*yd;
x1=x0+xd; y1=f(x1)*yd;
g.drawLine(x,(int)(hh-y0),x+1,(int)(hh-y1)); //(d)
}
} //end paint
} //end Func1
最終更新:2010年09月16日 14:00