java中怎样绘制正弦函数图象
答案:1 悬赏:40 手机版
解决时间 2021-11-25 17:31
- 提问者网友:温柔港
- 2021-11-24 19:18
java中怎样绘制正弦函数图象
最佳答案
- 五星知识达人网友:人類模型
- 2021-11-24 20:04
一下为一个代码示例
import javax.swing.*;
import java.awt.*;
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);
}
}
import javax.swing.*;
import java.awt.*;
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);
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯