import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class Main {
    public static void main(String[] args) {
        JFrame window = new JFrame("y = Sin(x)");
        int width = 800;
        int height = 700;
        window.setSize(width, height);
        window.setBackground(Color.WHITE);
        window.setResizable(true);
        window.setDefaultCloseOperation(EXIT_ON_CLOSE);
        window.setVisible(true);

        Canvas canvas = new Canvas();
        window.add(canvas);
        Graphics g = canvas.getGraphics();

        new Picture(g,150 ,300 ,Color.BLUE ,Color.RED ,width, height);

    }
}

class Picture extends JPanel implements Runnable {

    private final Graphics _g;
    private int _offsetFirst;
    private int _offsetSecond;

    private Color _colorFirst;
    private Color _colorSecond;
    private int _width;
    private int _height;

    public Picture(Graphics g, int _offsetFirst, int _offsetSecond,Color _colorFirst, Color _colorSecond ,int _width, int _height) {
        this._g = g;
        this._offsetFirst = _offsetFirst;
        this._offsetSecond = _offsetSecond;
        this._colorFirst = _colorFirst;
        this._colorSecond = _colorSecond;
        this._width = _width;
        this._height = _height;

        new Thread(this).start();
    }

    public void run() {
        new Thread(() -> DrawGraph(_colorFirst, _offsetFirst, 5)).start();
        new Thread(() -> DrawGraph(_colorSecond, _offsetSecond, 5)).start();
    }

    private void DrawGraph(Color color, int offset, int pointSize){
        for (int x = 0; x < _width; x++) {
            double y1 = Math.sin((x + _width) * 0.1);
            int y = (int) (y1 * 90 + offset / 2);
            synchronized (_g) {
                _g.setColor(color);
                _g.fillOval(x, y, pointSize, pointSize);
            }
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

