アットウィキロゴ

apple0823

import java.awt.*;
import java.awt.event.*;
class apple0823 extends Frame {
    public static void main(String [] args) {
        Frame f=new apple0823();
        f.setTitle("apple0823");
        f.setSize(600,600);
        f.setBackground(Color.white);
        f.setVisible(true);
    }
apple0823(){addWindowListener(new stopwin());}
class stopwin extends WindowAdapter{
    public void windowClosing(WindowEvent we){System.exit(0);}
    }
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));     //(i) 扇形(塗りつぶし)
  }
}
double f(double x) {     // 作成するグラフの関数
       return (Math.sin(2*x)+Math.cos(3*x));
    } //end f
}
最終更新:2010年09月14日 08:12