java中怎样绘制正弦函数图象
1个回答
展开全部
一下为一个代码示例
import javax.swing.*;
import java.awt.*;
/**
* @author <a href="mailto:ge.y.yang@hotmail.com">杨舸</a>
* @version 1.0.0
* @since jdk 1.8
*/
class Diagram extends JPanel {
static final int SCALEFACTOR = 200;
int cycles;
int points;
double[] sines;
int[] pts;
Diagram() {
}
public void setCycles(int newCycles) {
cycles = newCycles;
points = SCALEFACTOR * cycles * 2;
sines = new double[points];
for (int i = 0; i < points; i++) {
double radians = (Math.PI / SCALEFACTOR) * i;
sines[i] = Math.sin(radians);
}
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int maxWidth = getWidth();
double hstep = (double) maxWidth / (double) points;
int maxHeight = getHeight();
pts = new int[points];
for (int i = 0; i < points; i++)
pts[i] = (int) (sines[i] * maxHeight / 2 * .95 + maxHeight / 2);
g.setColor(Color.BLUE);
for (int i = 1; i < points; i++) {
int x1 = (int) ((i - 1) * hstep);
int x2 = (int) (i * hstep);
int y1 = pts[i - 1];
int y2 = pts[i];
g.drawLine(x1, y1, x2, y2);
}
}
}
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
Diagram sines = new Diagram();
sines.setCycles(5);
frame.getContentPane().add(sines);
frame.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |