画余弦正弦函数图像

先画坐标轴

package start;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.geom.GeneralPath;

import javax.swing.*;
public class sincos extends JFrame{
public sincos(){
super("正弦和余弦曲线");
getContentPane().setBackground(Color.black);
setSize(500,500);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.drawLine(0, 250, 500, 250);
g.setColor(Color.red);
g.drawLine(250, 0, 250, 500);
Graphics2D g2d=(Graphics2D)g;
//Graphics2D的上下文
//
GeneralPath sin=new GeneralPath();
sin.moveTo(0, 250);
for(int count =0;count<500;count++){
int x=count;
int y=(int)(50*Math.sin(2*x*Math.PI/200))+250;
sin.lineTo(x,y);
}
sin.moveTo(500, 250);
sin.closePath();
g2d.setColor(Color.blue);
//
g2d.draw(sin);
GeneralPath cos=new GeneralPath();
cos.moveTo(0.,300.);
for(int count =0;count<500;count++){
double x=count;
double y=(double)(50*Math.cos(2*x*Math.PI/200))+300;
cos.lineTo(x,y);
}
cos.moveTo(500, 300);
cos.closePath();
g2d.setColor(Color.ORANGE);
g2d.draw(cos);

}
public static void main(String args[]){
sincos application=new sincos();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

 

posted @ 2021-05-05 12:55  梦醒如赦  阅读(593)  评论(0)    收藏  举报