我正在编写一个可以在创建的窗口上绘制函数的 java 程序。一切正常,直到我开始弄乱余弦/正弦。我的程序可以编译,但由于某种原因,当我运行它时会出现一个空白窗口。在给定 x 值的情况下,它应该显示带有余弦图的 x 和 y 轴。我整天都在尝试东西,无法弄清楚我的逻辑错误是我的代码。
这是我的代码:
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");
}
}