使用Applet多媒体技术 绘制 sinx 图形
相关方法:
public abstract void fillOval(int x, int y, int width, int height);
public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle);
发现问题:sin函数周期是2PI,且是连续的。如果要显示完整的函数,即连续函数,必须进行x坐标“拉伸”,实际意义即 x/n(n为x轴拉伸倍数)。对于y 坐标,因为 sinx ∈[-1, 1]即要进行“拉伸”,需要乘一个倍数,即m*y。
例如,对y = 100sin(x/100)函数绘制
import java.applet.Applet;
import java.awt.Graphics;
public class Demo03 extends Applet{
@Override
public void paint(Graphics g) {
super.paint(g);
// 绘制窗口分辨率
g.drawString(""+getWidth()+"x"+getHeight(), 20, 20);
// 绘制简易坐标系
g.drawLine(20, getHeight()/2, getWidth()-20, getHeight()/2);
g.drawLine(getWidth()/2, 20, getWidth()/2, getHeight()-20);
// 拉伸法绘图
for(double x = 40; x<getWidth()-40.; x+=1) {
double y = 100*Math.sin(x/100);
g.fillOval((int)x, (int)y+getHeight()/2, 1, 1);
}
}
}