您好,我正在尝试创建一个 Java 程序,该程序允许您绘制方程,并且到目前为止它运行良好,直到我希望它绘制余弦和正弦函数。当正弦和余弦返回双倍但 drawLine 只接受整数时,我对如何做到这一点感到困惑。我是java新手,所以这可能很容易,但如果你能提供帮助,我将不胜感激。
这是我的代码:
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;
import java.lang.Math;
import java.awt.geom.*;
class PlotGraph extends JFrame{
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g.drawLine(50, 300, 550, 300); //x axis
g.drawLine(300, 550, 300, 50); //y axis
//Orignin x = 300 y = 300
double xmin, xmax;
double y;
xmin =(0);
xmax = 100;
double x = xmin;
double form = Math.cos(x);
double last_y = 300-(form);
for (x = xmin+1; x<=xmax; x++){
double newForm = Math.cos(x);
y = 300-(newForm);
g2.draw(new Line2D.Double(x-1+(300), last_y, x+300, y));
last_y = y;
}
}
public static void main(String [] args) {
PlotGraph graph = new PlotGraph();
graph.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
graph.setSize(600, 600);
graph.setVisible(true);
graph.setTitle("PlotGraph");
}
}