Advertisement
Guest User

hie kak

a guest
Apr 26th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. package hiekkaranta;
  2.  
  3.  
  4. import javafx.animation.AnimationTimer;
  5. import javafx.application.Application;
  6. import javafx.scene.Scene;
  7. import javafx.scene.canvas.Canvas;
  8. import javafx.scene.canvas.GraphicsContext;
  9. import javafx.scene.control.RadioButton;
  10. import javafx.scene.control.ToggleGroup;
  11. import javafx.scene.layout.BorderPane;
  12. import javafx.scene.layout.VBox;
  13. import javafx.scene.paint.Color;
  14. import javafx.stage.Stage;
  15.  
  16.  
  17. public class HiekkarantaSovellus extends Application {
  18. private Simulaatio simulaatio;
  19.  
  20. @Override
  21. public void start(Stage ikkuna) {
  22. this.simulaatio = new Simulaatio(200, 200);
  23.  
  24. Canvas piirtoalusta = new Canvas(200, 200);
  25. GraphicsContext piirturi = piirtoalusta.getGraphicsContext2D();
  26.  
  27.  
  28. RadioButton metalli = new RadioButton("Metalli");
  29. RadioButton hiekka = new RadioButton("Hiekka");
  30. RadioButton vesi = new RadioButton("Vesi");
  31. final ToggleGroup painikkeet = new ToggleGroup();
  32. metalli.setToggleGroup(painikkeet);
  33. metalli.setSelected(true);
  34. hiekka.setToggleGroup(painikkeet);
  35. vesi.setToggleGroup(painikkeet);
  36. VBox materiaaliValikko = new VBox();
  37. materiaaliValikko.getChildren().addAll(metalli, hiekka, vesi);
  38.  
  39. BorderPane asettelu = new BorderPane();
  40. asettelu.setCenter(piirtoalusta);
  41. asettelu.setRight(materiaaliValikko);
  42.  
  43. piirtoalusta.setOnMouseDragged((event) -> {
  44. if (metalli.isSelected()) {
  45. simulaatio.lisaa((int) event.getX(), (int) event.getY(), Tyyppi.METALLI);
  46. } else if (hiekka.isSelected()) {
  47. simulaatio.lisaa((int) event.getX(), (int) event.getY(), Tyyppi.HIEKKA);
  48. } else if (vesi.isSelected()) {
  49. simulaatio.lisaa((int) event.getX(), (int) event.getY(), Tyyppi.VESI);
  50. } else {
  51. simulaatio.lisaa((int) event.getX(), (int) event.getY(), Tyyppi.TYHJA);
  52. }
  53. });
  54.  
  55. new AnimationTimer() {
  56. long edellinen = 0;
  57.  
  58. @Override
  59. public void handle(long nykyhetki) {
  60. if (nykyhetki - edellinen < 200000000) {
  61. return;
  62. }
  63. simulaatio.paivita();
  64. }
  65. }.start();
  66.  
  67. new AnimationTimer() {
  68. long edellinen = 0;
  69.  
  70. @Override
  71. public void handle(long nykyhetki) {
  72. if (nykyhetki - edellinen < 100000000) {
  73. return;
  74. }
  75. piirturi.setFill(Color.BLACK);
  76. piirturi.fillRect(0, 0, 200, 200);
  77. for (int x = 0; x < piirtoalusta.getWidth(); x++) {
  78. for (int y = 0; y < piirtoalusta.getHeight(); y++) {
  79. if (simulaatio.sisalto(x, y) == Tyyppi.METALLI) {
  80. piirturi.setFill(Color.WHITE);
  81. } else if (simulaatio.sisalto(x, y) == Tyyppi.HIEKKA) {
  82. piirturi.setFill(Color.ORANGE);
  83. } else if (simulaatio.sisalto(x, y) == Tyyppi.VESI) {
  84. piirturi.setFill(Color.LIGHTBLUE);
  85. } else {
  86. piirturi.setFill(Color.BLACK);
  87. }
  88. piirturi.fillRect(x, y, 1, 1);
  89. }
  90. }
  91. this.edellinen = nykyhetki;
  92. }
  93. }.start();
  94.  
  95. Scene nakyma = new Scene(asettelu);
  96.  
  97. ikkuna.setScene(nakyma);
  98. ikkuna.show();
  99.  
  100. }
  101.  
  102. public static void main(String[] args) {
  103. launch(HiekkarantaSovellus.class);
  104. }
  105. }
  106.  
  107.  
  108. src/hiekkaranta/Simulaatio.java
  109.  
  110. package hiekkaranta;
  111.  
  112. import java.util.ArrayList;
  113. import java.util.List;
  114. import java.util.Random;
  115.  
  116.  
  117. public class Simulaatio {
  118. private Tyyppi[][] simulaatio;
  119.  
  120. public Simulaatio(int leveys, int korkeus) {
  121. this.simulaatio = new Tyyppi[leveys][korkeus];
  122. int x = 0;
  123. while (x < simulaatio.length) {
  124. int y = 0;
  125. while (y < simulaatio[x].length) {
  126. simulaatio[x][y] = Tyyppi.TYHJA;
  127. y++;
  128. }
  129. x++;
  130. }
  131. }
  132.  
  133. public void lisaa(int x, int y, Tyyppi tyyppi) {
  134. if (x >= 0 && x < this.simulaatio.length && y >= 0 && y < this.simulaatio[x].length) {
  135. this.simulaatio[x][y] = tyyppi;
  136. }
  137. }
  138.  
  139. public Tyyppi sisalto(int x, int y) {
  140. if (x < 0 || x >= simulaatio.length || y < 0 || y >= simulaatio[x].length) {
  141. return Tyyppi.METALLI;
  142. } else if (simulaatio[x][y] == null) {
  143. return Tyyppi.TYHJA;
  144. } else {
  145. return simulaatio[x][y];
  146. }
  147. }
  148.  
  149. public void paivita() {
  150. Random arpoja = new Random();
  151. List<Integer> alapuoli = new ArrayList<>();
  152. int x = 0;
  153. while (x < simulaatio.length - 1) {
  154. int y = 0;
  155. while (y < simulaatio[x].length - 1) {
  156. if (this.sisalto(x, y) == Tyyppi.HIEKKA) {
  157. if (this.sisalto(x - 1, y + 1) == Tyyppi.TYHJA || this.sisalto(x - 1, y + 1) == Tyyppi.VESI) {
  158. alapuoli.add(x - 1);
  159. }
  160. if (this.sisalto(x, y + 1) == Tyyppi.TYHJA || this.sisalto(x, y + 1) == Tyyppi.VESI) {
  161. alapuoli.add(x);
  162. }
  163. if (this.sisalto(x + 1, y + 1) == Tyyppi.TYHJA || this.sisalto(x + 1, y + 1) == Tyyppi.VESI) {
  164. alapuoli.add(x + 1);
  165. }
  166. if (!(alapuoli.isEmpty())) {
  167. int valinta = arpoja.nextInt(alapuoli.size());
  168. this.lisaa(x, y, Tyyppi.TYHJA);
  169. this.lisaa(alapuoli.get(valinta), y + 1, Tyyppi.HIEKKA);
  170. // if (this.sisalto(alapuoli.get(valinta) * 2, y + 1) == Tyyppi.TYHJA) {
  171. // this.lisaa(alapuoli.get(valinta) * 2, y + 1, Tyyppi.VESI);
  172. // } else if (this.sisalto(alapuoli.get(valinta) * 2, y) == Tyyppi.TYHJA) {
  173. // this.lisaa(alapuoli.get(valinta) * 2, y, Tyyppi.VESI);
  174. // }
  175. alapuoli.clear();
  176. }
  177. }
  178. // if (this.sisalto(x, y) == Tyyppi.VESI) {
  179. // if (this.sisalto(x - 1, y + 1) == Tyyppi.TYHJA) {
  180. // alapuoli.add(x - 1);
  181. // }
  182. // if (this.sisalto(x, y + 1) == Tyyppi.TYHJA) {
  183. // alapuoli.add(x);
  184. // }
  185. // if (this.sisalto(x + 1, y + 1) == Tyyppi.TYHJA) {
  186. // alapuoli.add(x + 1);
  187. // }
  188. // if (!(alapuoli.isEmpty())) {
  189. // int valinta = arpoja.nextInt(alapuoli.size() - 1);
  190. // this.lisaa(x, y, Tyyppi.TYHJA);
  191. // this.lisaa(alapuoli.get(valinta), y + 1, Tyyppi.HIEKKA);
  192. // alapuoli.clear();
  193. // } else {
  194. // if (this.sisalto(x - 1, y) == Tyyppi.TYHJA) {
  195. // alapuoli.add(x - 1);
  196. // }
  197. // if (this.sisalto(x + 1, y) == Tyyppi.TYHJA) {
  198. // alapuoli.add(x + 1);
  199. // }
  200. // if (!(alapuoli.isEmpty())) {
  201. // int valinta = arpoja.nextInt(alapuoli.size());
  202. // this.lisaa(x, y, Tyyppi.TYHJA);
  203. // this.lisaa(x, alapuoli.get(valinta), Tyyppi.VESI);
  204. // alapuoli.clear();
  205. // }
  206. // }
  207. // }
  208. y++;
  209. }
  210. x++;
  211. }
  212. }
  213. }
  214.  
  215.  
  216. src/hiekkaranta/Tyyppi.java
  217. package hiekkaranta;
  218.  
  219. public enum Tyyppi {
  220. TYHJA, METALLI, HIEKKA, VESI;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement