用java语言绘制正弦曲线
答案:2 悬赏:50 手机版
解决时间 2021-01-23 18:48
- 提问者网友:世勋超人
- 2021-01-22 20:27
用java语言绘制正弦曲线
最佳答案
- 五星知识达人网友:空山清雨
- 2021-01-22 21:07
很简单,程序写给你,给分:
---------------------------------------------
import java.awt.*;
import javax.swing.JFrame;
import java.util.Random;
import java.text.DecimalFormat;
public class SinDemo extends JFrame {
private double cx = 1, cy = 1;
private double toCx = 1, toCy = 1;
private Random rnd = new Random();
private DecimalFormat df = new DecimalFormat("0.00");
private SinDemo () {
super("Sin-Demo");
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
run();
}
private int translateX (double x) {
return (int)(x*getWidth()/Math.PI/4 + getWidth()/2);
}
private int translateY (double y) {
return (int)(getHeight()/2 - y*getWidth()/Math.PI/4);
}
private double sin (double x) {
return (cy * Math.sin(cx * x));
}
@Override
public void paint (Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.drawString("y = " + df.format(cx) + " * sin( " + df.format(cy) + " * x)", 50, 50);
g.setColor(Color.LIGHT_GRAY);
g.drawLine(0, getHeight()/2, getWidth(), getHeight()/2);
g.drawLine(getWidth()/2, 0, getWidth()/2, getHeight());
g.setColor(Color.BLACK);
for (double i=-Math.PI/cx; i g.drawLine(translateX(i), translateY(sin(i)),
translateX(i+0.1), translateY(sin(i+0.1)));
}
public void run () {
while (true) {
if (Math.abs(cx - toCx) < 0.1) {
toCx = rnd.nextDouble()*2;
toCy = rnd.nextDouble()*2;
}
cx += (toCx - cx)/50;
cy += (toCy - cy)/50;
paint(getGraphics());
try {
Thread.sleep(80);
} catch (InterruptedException ie) {}
}
}
public static void main (String args[]) {
new SinDemo();
}
}
-------------------------------------------------
再给你一个截图:
---------------------------------------------
import java.awt.*;
import javax.swing.JFrame;
import java.util.Random;
import java.text.DecimalFormat;
public class SinDemo extends JFrame {
private double cx = 1, cy = 1;
private double toCx = 1, toCy = 1;
private Random rnd = new Random();
private DecimalFormat df = new DecimalFormat("0.00");
private SinDemo () {
super("Sin-Demo");
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
run();
}
private int translateX (double x) {
return (int)(x*getWidth()/Math.PI/4 + getWidth()/2);
}
private int translateY (double y) {
return (int)(getHeight()/2 - y*getWidth()/Math.PI/4);
}
private double sin (double x) {
return (cy * Math.sin(cx * x));
}
@Override
public void paint (Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.drawString("y = " + df.format(cx) + " * sin( " + df.format(cy) + " * x)", 50, 50);
g.setColor(Color.LIGHT_GRAY);
g.drawLine(0, getHeight()/2, getWidth(), getHeight()/2);
g.drawLine(getWidth()/2, 0, getWidth()/2, getHeight());
g.setColor(Color.BLACK);
for (double i=-Math.PI/cx; i
translateX(i+0.1), translateY(sin(i+0.1)));
}
public void run () {
while (true) {
if (Math.abs(cx - toCx) < 0.1) {
toCx = rnd.nextDouble()*2;
toCy = rnd.nextDouble()*2;
}
cx += (toCx - cx)/50;
cy += (toCy - cy)/50;
paint(getGraphics());
try {
Thread.sleep(80);
} catch (InterruptedException ie) {}
}
}
public static void main (String args[]) {
new SinDemo();
}
}
-------------------------------------------------
再给你一个截图:
全部回答
- 1楼网友:神的生死簿
- 2021-01-22 22:34
你去找找thinking in java第四版
gui部分有一个话正弦曲线的,
而且可以有slider控制曲线变换
gui部分有一个话正弦曲线的,
而且可以有slider控制曲线变换
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯