a simple Java-FX Application which Simulate the Approximation of Pi https://nextn.xyz
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.7 KiB

  1. package montecarlopi;
  2. import java.util.LinkedList;
  3. import java.util.Observable;
  4. import javafx.scene.canvas.Canvas;
  5. import javafx.scene.canvas.GraphicsContext;
  6. import javafx.scene.control.ListView;
  7. import javafx.scene.layout.Pane;
  8. import javafx.scene.paint.Color;
  9. public class Model extends Observable{
  10. private Circle c;
  11. private LinkedList<Point> list;
  12. private Pane pane;
  13. private Canvas can;
  14. private int in, out, tries;
  15. private UpdateThread uT;
  16. private int count = 0;
  17. private boolean exit;
  18. public boolean isExit(){
  19. return exit;
  20. }
  21. public Model() {
  22. int size = 400;
  23. c = new Circle(size);
  24. list = new LinkedList<>();
  25. pane = new Pane();
  26. pane.setPrefSize(size, size);
  27. initPane();
  28. in = 0;
  29. out = 0;
  30. tries = 0;
  31. }
  32. public Pane getPane() {
  33. return pane;
  34. }
  35. public int getIn() {
  36. return in;
  37. }
  38. public int getOut() {
  39. return out;
  40. }
  41. public int getTries() {
  42. return tries;
  43. }
  44. private void initPane() {
  45. initCanvas();
  46. pane.getChildren().add(can);
  47. }
  48. private void initCanvas() {
  49. can = new Canvas(pane.getPrefHeight(),pane.getPrefWidth());
  50. GraphicsContext gc = can.getGraphicsContext2D();
  51. gc.clearRect(0, 0, can.getWidth(), can.getHeight());
  52. gc.fill();
  53. gc.setStroke(Color.BLACK);
  54. gc.setLineWidth(1);
  55. gc.strokeOval(0, 0, this.c.getDurchmesser() ,this.c.getDurchmesser());
  56. gc.strokeRect(0, 0, this.c.getDurchmesser() ,this.c.getDurchmesser());
  57. }
  58. public Point generatePoint() {
  59. if(exit)return null;
  60. double x = (Math.random() * c.getDurchmesser());
  61. double y = (Math.random() * c.getDurchmesser());
  62. Point p = new Point(x,y);
  63. addPoint(p);
  64. return p;
  65. }
  66. public void reset(ListView<String> list1) {
  67. list = new LinkedList<>();
  68. tries = 0;
  69. in = 0;
  70. out = 0;
  71. GraphicsContext gc = can.getGraphicsContext2D();
  72. gc.clearRect(0, 0, can.getWidth(), can.getHeight());
  73. gc.setStroke(Color.BLACK);
  74. gc.setLineWidth(1);
  75. gc.strokeOval(0, 0, c.getDurchmesser(), c.getDurchmesser());
  76. gc.strokeRect(0, 0, c.getDurchmesser(), c.getDurchmesser());
  77. setExit(true);
  78. list1.getItems().clear();
  79. setChanged();
  80. notifyObservers();
  81. }
  82. public void addPoint(Point p) {
  83. list.add(p);
  84. boolean in = inOrout(p);
  85. tries++;
  86. GraphicsContext gc = can.getGraphicsContext2D();
  87. if(in){
  88. gc.setStroke(Color.VIOLET); //inside
  89. }else{
  90. gc.setStroke(Color.BLUE); //outside
  91. }
  92. gc.setLineWidth(1);
  93. gc.strokeRect((double)p.getX(),(double)p.getY(), 1, 1);
  94. setChanged();
  95. notifyObservers();
  96. }
  97. public void generateAuto (ListView<String> list,int count){
  98. setExit(false);
  99. UpdateThread ut = new UpdateThread(this, list, count);
  100. count++;
  101. ut.start();
  102. }
  103. private boolean inOrout(Point p) {
  104. if(c.isInside(p)) {
  105. in++;
  106. return true;
  107. }else{
  108. out++;
  109. }
  110. return false;
  111. }
  112. public double getPi() {
  113. if(out != 0) {
  114. return ((double)in / ((double)out+(double)in)) * 4;
  115. }
  116. return 0;
  117. }
  118. void stop(ListView<String> list) {
  119. list.getItems().clear();
  120. exit=true;
  121. }
  122. public void setExit(boolean exit) {
  123. this.exit=exit;
  124. }
  125. }