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,468
Reaction score
10,177
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
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
0
Views
846
Replies
0
Views
377
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