Java - Plotting Two Curves on the same Plot

  • Context: Java 
  • Thread starter Thread starter jedishrfu
  • Start date Start date
Click For Summary
SUMMARY

The discussion presents a Java program that utilizes Java Swing and AWT to plot sine and cosine curves over the interval [0, 2π]. The program employs JFrame and JPanel for graphical user interface rendering, and Graphics for drawing the curves. It features automatic scaling to fit the graph within the window, ensuring a clear display of both curves, with the sine curve in red and the cosine curve in blue.

PREREQUISITES
  • Java Swing for GUI development
  • Java AWT for graphical operations
  • Understanding of trigonometric functions (sine and cosine)
  • Basic knowledge of Java programming
NEXT STEPS
  • Explore Java Swing components for advanced GUI features
  • Learn about custom painting in Java using the paintComponent method
  • Investigate dynamic scaling techniques for graphical applications
  • Study the implementation of interactive plots using Java libraries
USEFUL FOR

Java developers, computer science students, and anyone interested in graphical programming and data visualization using Java.

Messages
15,606
Reaction score
10,369
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
Technology news on Phys.org

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
779
  • · 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