Untitled

                Never    
Java
       
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Window extends JFrame {
	public Window() {
		super("MonteCarloPi");
		JPanel panel = new MyPanel();

		add(panel);

		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);

	}

	

	
}


import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.geom.*;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.JPanel;

public class MyPanel extends JPanel {
	public MyPanel() {
		setPreferredSize(new Dimension(400, 400));

		final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
		executorService.scheduleAtFixedRate(new Runnable() {
			@Override
			public void run() {
				PaintingPoints(null);
			}
		}, 0, 1, TimeUnit.SECONDS);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;

		// prostokat
		Rectangle2D square = new Rectangle2D.Double(10, 10, 380, 380);
		// kolo
		Ellipse2D circle = new Ellipse2D.Double(10, 10, 380, 380);
		g2d.draw(square);
		g2d.draw(circle);
	}

	private void PaintingPoints(Graphics g) {
		Circle circl3 = new Circle(190.0);
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		double prawd;
		double pi;
		double blad;
		double x, y;
		int n = 10000;
		double hit = 0;
		Random rand = new Random();
		for (int i = 0; i < n; i++) {

			x = rand.nextDouble() * 380 + 10;
			y = rand.nextDouble() * 380 + 10;
			Ellipse2D point = new Ellipse2D.Double(x - 2, y - 2, 4, 4);
			boolean wynik = circl3.point(x - 10, y - 10);
			if (wynik == true) {
				g2d.setColor(Color.green);
				g2d.draw(point);
				hit++;

			} else {
				g2d.setColor(Color.red);
				g2d.draw(point);
			}

			try {
				// thread to sleep for 1000 milliseconds
				Thread.sleep(1000);
			} catch (Exception e) {
				System.out.println(e);
			}
		}
		prawd = hit / n;
		pi = prawd * 4;
		blad = Math.abs(pi - Math.PI);
		System.out.println("Ilość trafień to: " + hit + " prawdopodobienstwo trafienia to: " + prawd);
		System.out.println("Wyliczone pi to: " + pi + " Błąd to: " + blad);

	}

}



public class Circle {
	double r;

	public Circle(double r) {
		this.r = r;
	}
public boolean point(double x, double y) {
		boolean hit = false;
		if (Math.sqrt((x-190) * (x-190) + (y-190) * (y-190)) <= r)
			hit = true;
		else
			hit = false;
		return hit;
	}
}


import java.awt.EventQueue;

public class Test {public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {

		@Override
		public void run() {
			new Window();
		}
	});
}


}