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

  • Thread starter Thread starter jedishrfu
  • Start date Start date
AI Thread 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,449
Reaction score
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:
  • 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
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
0
Views
799
Replies
0
Views
334
Replies
4
Views
2K
Replies
1
Views
2K
Replies
4
Views
5K
Replies
1
Views
3K
Replies
12
Views
4K
Replies
4
Views
6K
Back
Top