Graphics class draws "sine image"

Use Applet multimedia technology to draw sinx graphics

Related methods:

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);

Found the problem: the sin function cycle is 2PI and is continuous. If you want to display a complete function, that is, a continuous function, you must "stretch" the x coordinate, and the actual meaning is x/n (n is the x-axis stretch multiple). For the y coordinate, because sinx ∈[-1, 1] is to be "stretched", it needs to be multiplied by a multiple, namely m*y.

For example, plotting the function 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);
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_43341057/article/details/105581939