import javax.swing.*;
import java.awt.*;
publicclassFunctionPlotterextendsJPanel{
privatefinaldouble deltaT;
publicFunctionPlotter(double deltaT){
this.deltaT = deltaT;
}
privatedoublecomputeFunction(double t){
return -2 * t * Math.sin(t * t);
}
@OverrideprotectedvoidpaintComponent(Graphics g){
super.paintComponent(g);
g.translate(0, getHeight() / 2); // Move the origin to the middle of the panelfor (double t = 0; t <= 8; t += deltaT) {
int x = (int) (50 * t); // Scale for better visualizationint y = (int) (50 * computeFunction(t));
g.drawString("*", x, -y); // Draw the point at (x, -y) to reflect traditional Cartesian plane
}
}
publicstaticvoidmain(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);
}
}