编辑代码

import javax.swing.*;
import java.awt.*;

public class FunctionPlotter extends JPanel {
    private final double deltaT;

    public FunctionPlotter(double deltaT) {
        this.deltaT = deltaT;
    }

    private double computeFunction(double t) {
        return -2 * t * Math.sin(t * t);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.translate(0, getHeight() / 2);  // Move the origin to the middle of the panel
        for (double t = 0; t <= 8; t += deltaT) {
            int x = (int) (50 * t);  // Scale for better visualization
            int y = (int) (50 * computeFunction(t));
            g.drawString("*", x, -y);  // Draw the point at (x, -y) to reflect traditional Cartesian plane
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Function Plotter");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new FunctionPlotter(0.01));  // Change deltaT here to see different plots
        frame.setSize(800, 600);
        frame.setVisible(true);
    }
}