-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
119 lines (101 loc) · 4.13 KB
/
Main.java
File metadata and controls
119 lines (101 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package your.package.name; // Replace with your package name
import java.util.List;
import java.util.function.Function;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* A template for plotting mathematical functions using Java and JFreeChart.
*/
public class FunctionPlotter {
// Static frame and panel to hold multiple charts
private static JFrame frame = null;
private static JPanel panel = null;
// Define the range for the x-axis
private static double xmin = 0;
private static double xmax = 10; // Adjust as needed
private static double xIncrement = 0.1; // Step size for x values
public static void main(String[] args){
// Example function: y = sin(x)
Function<Double, Double> sineFunction = x -> Math.sin(x);
plotFunction(sineFunction, "Sine Function");
// Example function: y = cos(x)
Function<Double, Double> cosineFunction = x -> Math.cos(x);
plotFunction(cosineFunction, "Cosine Function");
// Example function: y = x^2
Function<Double, Double> quadraticFunction = x -> Math.pow(x, 2);
plotFunction(quadraticFunction, "Quadratic Function");
}
/**
* Plots a mathematical function within the specified x-range.
*
* @param function The mathematical function to plot.
* @param seriesName The name of the function (used in the chart).
*/
public static void plotFunction(Function<Double, Double> function, String seriesName){
List<Double> yValues = new ArrayList<>();
List<Double> xValues = new ArrayList<>();
double x = xmin;
while(x <= xmax){
double yValue = function.apply(x);
xValues.add(x);
yValues.add(yValue);
x += xIncrement;
}
plotChart("X-Axis", "Y-Axis", xValues, yValues, seriesName);
}
/**
* Creates and displays a chart with the provided x and y values.
*
* @param xAxisLabel Label for the x-axis.
* @param yAxisLabel Label for the y-axis.
* @param xValues List of x-values.
* @param yValues List of y-values.
* @param seriesName Name of the data series.
*/
public static void plotChart(String xAxisLabel, String yAxisLabel, List<Double> xValues, List<Double> yValues, String seriesName){
// Create a series to hold the data
XYSeries series = new XYSeries(seriesName);
for(int i = 0; i < xValues.size(); i++){
series.add(xValues.get(i), yValues.get(i));
}
// Add the series to a dataset
XYSeriesCollection dataset = new XYSeriesCollection(series);
// Create the chart
JFreeChart chart = ChartFactory.createXYLineChart(
seriesName, // Chart title
xAxisLabel, // X-axis label
yAxisLabel, // Y-axis label
dataset, // Data
PlotOrientation.VERTICAL,
true, // Include legend
true,
false
);
// Create the ChartPanel
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 400));
// Initialize the frame and panel if they are null
if (frame == null) {
frame = new JFrame("Function Plotter");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); // Horizontal layout
frame.add(panel);
frame.setSize(800, 600); // Set initial size
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// Add the chart panel to the main panel
panel.add(chartPanel);
panel.revalidate();
panel.repaint();
}
}