Java Java --> Plotting Two Curves on the same Plot

  • Thread starter Thread starter jedishrfu
  • Start date Start date
Click For Summary
The Java program utilizes Java Swing and AWT to create a graphical representation of sine and cosine curves over the interval [0, 2π]. It features a JFrame and JPanel for the user interface, employing the Graphics class to draw the curves and axes. The program automatically scales the graph to fit within the specified window dimensions. The sine curve is represented in red and the cosine curve in blue, with axes labeled at 0, π, and 2π. The drawing is achieved by iterating through pixel values, mapping them to the respective sine and cosine functions, and using the drawLine() method for smooth connections between points. The output is a dynamic plot displayed in a window, showcasing the mathematical functions clearly.
Messages
15,557
Reaction score
10,306
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:
  • 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:​

  1. paintComponent(Graphics g):
    • This method is overridden when drawing the sine and cosine curves.
  2. Axes Drawing:
    • Draws the x-axis and y-axis with labels for 0,𝜋, and 2𝜋.
  3. 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.
sinecosineplot-png.png


Solution Credits​


Proposed by: @hagopbul
Coded by: @ChatGPT
Reviewed by: @jedishrfu
 
Last edited:
  • Like
Likes Greg Bernhardt
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 0 ·
Replies
0
Views
993
  • · Replies 0 ·
Replies
0
Views
538
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K