Advertisement
Guest User

Untitled

a guest
May 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. /**
  2. * Write a description of class MandelbrotGenerator here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7.  
  8. import javafx.animation.KeyFrame;
  9. import javafx.animation.Timeline;
  10. import javafx.application.Application;
  11. import javafx.scene.Scene;
  12. import javafx.scene.canvas.Canvas;
  13. import javafx.scene.canvas.GraphicsContext;
  14. import javafx.scene.layout.StackPane;
  15. import javafx.scene.paint.Color;
  16. import javafx.stage.Stage;
  17. import javafx.util.Duration;
  18.  
  19. import javafx.scene.input.KeyCode;
  20. import javafx.animation.Animation;
  21.  
  22. public class MandelbrotGenerator extends Application
  23. {
  24. // intstance variables
  25. static final int screenWidth = 900;
  26. static final int screenHeight = 450;
  27.  
  28. static final int scale = 0;
  29. static final int maxIterations = 255;
  30.  
  31. @Override
  32. public void start(Stage stage)
  33. {
  34. Canvas canvas = new Canvas(screenWidth, screenHeight);
  35. GraphicsContext gc = canvas.getGraphicsContext2D();
  36. Timeline tl = new Timeline(new KeyFrame(Duration.millis(100), e -> run(gc)));
  37. tl.setCycleCount(Timeline.INDEFINITE);
  38. canvas.setFocusTraversable(true);
  39.  
  40. // handle mouse and key events
  41. canvas.setOnKeyPressed(e ->
  42. {
  43. if(e.getCode() == KeyCode.W)
  44. {
  45. // do something
  46. }
  47. });
  48.  
  49. canvas.setOnMouseClicked(e ->
  50. {
  51. // do something
  52. });
  53.  
  54. stage.setTitle("Mandelbrot Set Generator");
  55. stage.setScene(new Scene(new StackPane(canvas)));
  56. stage.show();
  57. tl.play();
  58. }
  59.  
  60. private void run(GraphicsContext gc)
  61. {
  62. // color for background
  63. gc.setFill(Color.BLACK);
  64. gc.fillRect(0, 0, screenWidth, screenHeight);
  65.  
  66. for (int row = 0; row < screenWidth; row++) {
  67. for (int col = 0; col < screenHeight; col++) {
  68. checkPoint(row, col, gc);
  69. }
  70. }
  71.  
  72.  
  73.  
  74. }
  75. //determinees number of iterations, calls colorPoint if in set
  76. public static void checkPoint(int x, int y, GraphicsContext gc) {
  77. int numIterations = 0;
  78. int previousSum = 0;
  79. int c = getDistanceSquared(x, y);
  80.  
  81. while (numIterations < maxIterations && previousSum < 4) {
  82. previousSum = zFuntionOutput(c, previousSum);
  83. }
  84. System.out.println("here");
  85. if (numIterations < maxIterations)
  86. colorPoint(x, y, numIterations, gc);
  87. }
  88.  
  89. //draw and assign color
  90. public static void colorPoint(int x, int y, double iterations, GraphicsContext gc) {
  91. iterations = 1 / (iterations + 1);
  92. gc.setFill(new Color(iterations, 1 - iterations, iterations, 1));
  93. gc.fillRect(x, y, 1, 1);
  94. }
  95.  
  96. public static int zFuntionOutput(int c, int previousSum) {
  97. return previousSum * previousSum + c;
  98. }
  99.  
  100. //
  101. public static int getDistanceSquared(int x, int y) {
  102. return translateX(x) * translateX(x) + translateY(y) * translateY(y);
  103. }
  104.  
  105.  
  106.  
  107. /*
  108. @param: takes the X value fromt the top left coordinate system
  109. @postcondition: returns the X coordinate on the cartesian cooridnate system
  110. */
  111. public static int translateX(int x) {
  112. if (x < screenWidth / 2) {
  113. return x - screenWidth / 2;
  114. }
  115. else if (x > screenWidth / 2) {
  116. return x / 2;
  117. }
  118. else {
  119. return 0;
  120. }
  121. }
  122.  
  123. /*
  124. @param: takes the Y value fromt the top left coordinate system
  125. @postcondition: returns the Y coordinate on the cartesian cooridnate system
  126. */
  127. public static int translateY(int y) {
  128. if (y < screenHeight / 2) {
  129. return y + screenHeight / 2;
  130. }
  131. else if (y > screenHeight / 2) {
  132. return -y / 2;
  133. }
  134. else {
  135. return 0;
  136. }
  137. }
  138.  
  139. // run program
  140. public static void main(String[] args)
  141. {
  142. Application.launch(args);
  143. }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement