Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. import javafx.animation.AnimationTimer;
  2. import javafx.application.Application;
  3. import javafx.scene.Group;
  4. import javafx.scene.Scene;
  5. import javafx.scene.canvas.Canvas;
  6. import javafx.scene.canvas.GraphicsContext;
  7. import javafx.scene.paint.Color;
  8. import javafx.stage.Stage;
  9.  
  10. import java.util.Arrays;
  11. import java.util.Random;
  12.  
  13. import static java.lang.Math.round;
  14. import static java.lang.Math.sqrt;
  15. import static java.lang.System.*;
  16.  
  17. /*
  18. * Program to simulate segregation.
  19. * See : http://nifty.stanford.edu/2014/mccown-schelling-model-segregation/
  20. *
  21. * NOTE:
  22. * - JavaFX first calls method init() and then method start() far below.
  23. * - To test uncomment call to test() first in init() method!
  24. *
  25. */
  26. // Extends Application because of JavaFX (just accept for now)
  27. public class Neighbours extends Application {
  28.  
  29. // Enumeration type for the state of an Actor
  30. enum State {
  31. UNSATISFIED,
  32. SATISFIED
  33. }
  34.  
  35. class Actor {
  36. final Color color; // Color an existing JavaFX class
  37. State state;
  38.  
  39. Actor(Color color) {
  40. this.color = color;
  41. } // Constructor
  42. }
  43.  
  44. // Below is the *only* accepted instance variable (i.e. variables outside any method)
  45. // This variable may *only* be used in methods init() and updateWorld()
  46. Actor[][] world; // The world is a square matrix of Actors
  47.  
  48. // This is the method called by the timer to update the world
  49. // (i.e move unsatisfied) approx each 1/60 sec.
  50. void updateWorld() {
  51. // % of surrounding neighbours that are like me
  52. double threshold = 0.7;
  53.  
  54. // TODO update the world
  55. }
  56.  
  57. // This method initializes the world variable with a random distribution of Actors
  58. // Method automatically called by JavaFX runtime
  59. // That's why we must have "@Override" and "public" (just accept for now)
  60. @Override
  61. public void init() {
  62. //test(); // <---------------- Uncomment to TEST!
  63.  
  64. // %-distribution of RED, BLUE and NONE
  65. double[] dist = {0.25, 0.25, 0.50};
  66. // Number of locations (places) in world (must be a square)
  67. int nLocations = 900; // Should also try 90 000
  68.  
  69. // TODO create initialize the world
  70.  
  71. // Should be last
  72. fixScreenSize(nLocations);
  73. }
  74.  
  75. // -------------- Methods -----------------------
  76.  
  77. // TODO Add methods to use
  78.  
  79. // Check if inside world
  80. boolean isValidLocation(int size, int row, int col) {
  81. return 0 <= row && row < size && 0 <= col && col < size;
  82. }
  83.  
  84.  
  85. // ------- Testing -------------------------------------
  86.  
  87. // Here you run your tests i.e. call your logic methods
  88. // to see that they really work. Important!!!!
  89. void test() {
  90. // A small hard coded world for testing
  91. Actor[][] testWorld = new Actor[][]{
  92. {new Actor(Color.RED), new Actor(Color.RED), null},
  93. {null, new Actor(Color.BLUE), null},
  94. {new Actor(Color.RED), null, new Actor(Color.BLUE)}
  95. };
  96. double th = 0.5; // Simple threshold used for testing
  97.  
  98. int size = testWorld.length;
  99. out.println(isValidLocation(size, 0, 0));
  100. out.println(!isValidLocation(size, -1, 0));
  101. out.println(!isValidLocation(size, 0, 3));
  102.  
  103. // TODO Add tests here
  104.  
  105. exit(0);
  106. }
  107.  
  108. // ******************** NOTHING to do below this row, it's JavaFX stuff **************
  109.  
  110. double width = 400; // Size for window
  111. double height = 400;
  112. final double margin = 50;
  113. double dotSize;
  114.  
  115. void fixScreenSize(int nLocations) {
  116. // Adjust screen window
  117. dotSize = 9000 / nLocations;
  118. if (dotSize < 1) {
  119. dotSize = 2;
  120. }
  121. width = sqrt(nLocations) * dotSize + 2 * margin;
  122. height = width;
  123. }
  124.  
  125. long lastUpdateTime;
  126. final long INTERVAL = 450_000_000;
  127.  
  128.  
  129. @Override
  130. public void start(Stage primaryStage) throws Exception {
  131.  
  132. // Build a scene graph
  133. Group root = new Group();
  134. Canvas canvas = new Canvas(width, height);
  135. root.getChildren().addAll(canvas);
  136. GraphicsContext gc = canvas.getGraphicsContext2D();
  137.  
  138. // Create a timer
  139. AnimationTimer timer = new AnimationTimer() {
  140. // This method called by FX, parameter is the current time
  141. public void handle(long now) {
  142. long elapsedNanos = now - lastUpdateTime;
  143. if (elapsedNanos > INTERVAL) {
  144. updateWorld();
  145. renderWorld(gc);
  146. lastUpdateTime = now;
  147. }
  148. }
  149. };
  150.  
  151. Scene scene = new Scene(root);
  152. primaryStage.setScene(scene);
  153. primaryStage.setTitle("Simulation");
  154. primaryStage.show();
  155.  
  156. timer.start(); // Start simulation
  157. }
  158.  
  159.  
  160. // Render the state of the world to the screen
  161. public void renderWorld(GraphicsContext g) {
  162. g.clearRect(0, 0, width, height);
  163. int size = world.length;
  164. for (int row = 0; row < size; row++) {
  165. for (int col = 0; col < size; col++) {
  166. int x = (int) (dotSize * col + margin);
  167. int y = (int) (dotSize * row + margin);
  168. if (world[row][col] != null) {
  169. g.setFill(world[row][col].color);
  170. g.fillOval(x, y, dotSize, dotSize);
  171. }
  172. }
  173. }
  174. }
  175.  
  176. public static void main(String[] args) {
  177. launch(args);
  178. }
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement