import java.awt.*;
import javax.swing.*;
public class NumberAxis extends JPanel{
JFrame frame;
public static void main(String[] args) {
NumberAxis numberAxis = new NumberAxis();
numberAxis.go();
}
private void go() {
frame = new JFrame();
NumberAxis numberAxis = new NumberAxis();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.add(numberAxis);
frame.setTitle("刘佩伦的智障平面直角坐标系(y = " + "x * x * x" + ")");
frame.setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
g.setColor(Color.black);
int height = this.getHeight();
int width = this.getWidth();
g.drawLine(0, height / 2, width, height / 2);
g.drawLine(width / 2, 0, width / 2, height);
//在屏幕中间构建平面直角坐标系
double x, y;
g.setColor(Color.blue);
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
//遍历平面直角坐标系,如果一个点满足要求就绘制这个点
x = (i - width / 2 - 1) / 100.0;
y = (height / 2 - j - 1) / 100.0;
if (isRight(y, x * x * x, 0.02)) {
g.fillOval(i, j, 2, 2);
}
}
}
}
boolean isRight(double num1, double num2, double error) {
//判断两个数字是否在误差允许范围
if (Math.abs(num1 - num2) <= error)
return true;
return false;
}
}