- 15,449
- 10,150
Here's a Java program that uses Java Swing and Java AWT to plot sine and cosine curves for the interval [0,2π].
The program creates a window with a panel where both curves are drawn:
Proposed by: @hagopbul
Coded by: @ChatGPT
Reviewed by: @jedishrfu
The program creates a window with a panel where both curves are drawn:
- Uses JFrame and JPanel for GUI rendering.
- Uses Graphics to draw the sine and cosine curves.
- Automatically scales the graph to fit within the window.
Java Code:
Java:
import javax.swing.*;
import java.awt.*;
public class SineCosinePlot extends JPanel {
private static final int WIDTH = 800; // Width of the panel
private static final int HEIGHT = 600; // Height of the panel
private static final int PADDING = 50; // Padding around the graph
private static final double SCALE_X = WIDTH / (2 * Math.PI); // Scaling factor for x-axis
private static final double SCALE_Y = HEIGHT / 3; // Scaling factor for y-axis
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawAxes(g);
drawSineCurve(g);
drawCosineCurve(g);
}
private void drawAxes(Graphics g) {
g.setColor(Color.BLACK);
int xAxisY = HEIGHT / 2;
int yAxisX = PADDING;
// X-axis
g.drawLine(PADDING, xAxisY, WIDTH - PADDING, xAxisY);
// Y-axis
g.drawLine(yAxisX, PADDING, yAxisX, HEIGHT - PADDING);
// Labels
g.drawString("0", yAxisX - 10, xAxisY + 15);
g.drawString("π", (int) (SCALE_X * Math.PI) + PADDING - 5, xAxisY + 15);
g.drawString("2π", (int) (SCALE_X * 2 * Math.PI) + PADDING - 5, xAxisY + 15);
g.drawString("1", yAxisX - 20, xAxisY - (int) (SCALE_Y));
g.drawString("-1", yAxisX - 20, xAxisY + (int) (SCALE_Y));
}
private void drawSineCurve(Graphics g) {
g.setColor(Color.RED);
for (int i = 0; i < WIDTH - PADDING * 2; i++) {
double x1 = (i / SCALE_X);
double y1 = Math.sin(x1);
double x2 = ((i + 1) / SCALE_X);
double y2 = Math.sin(x2);
int screenX1 = i + PADDING;
int screenY1 = HEIGHT / 2 - (int) (y1 * SCALE_Y);
int screenX2 = i + 1 + PADDING;
int screenY2 = HEIGHT / 2 - (int) (y2 * SCALE_Y);
g.drawLine(screenX1, screenY1, screenX2, screenY2);
}
}
private void drawCosineCurve(Graphics g) {
g.setColor(Color.BLUE);
for (int i = 0; i < WIDTH - PADDING * 2; i++) {
double x1 = (i / SCALE_X);
double y1 = Math.cos(x1);
double x2 = ((i + 1) / SCALE_X);
double y2 = Math.cos(x2);
int screenX1 = i + PADDING;
int screenY1 = HEIGHT / 2 - (int) (y1 * SCALE_Y);
int screenX2 = i + 1 + PADDING;
int screenY2 = HEIGHT / 2 - (int) (y2 * SCALE_Y);
g.drawLine(screenX1, screenY1, screenX2, screenY2);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Sine and Cosine Plot");
SineCosinePlot panel = new SineCosinePlot();
frame.add(panel);
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Explanation:
- paintComponent(Graphics g):
- This method is overridden when drawing the sine and cosine curves.
- Axes Drawing:
- Draws the x-axis and y-axis with labels for 0,𝜋, and 2𝜋.
- Sine and Cosine Curves:
- The graph is drawn by iterating over screen pixels and mapping them to the sine/cosine function.
- x values are scaled using SCALE_X, and
- y values are scaled using SCALE_Y.
- drawLine() is used to connect consecutive points smoothly.
Output:
- A red sine wave and a blue cosine wave are displayed in a window.
- Axes labeled at 0,𝜋, and 2𝜋.
- The graph scales dynamically to fit in the window.
Solution Credits
Proposed by: @hagopbul
Coded by: @ChatGPT
Reviewed by: @jedishrfu
Last edited: