Advertisement
Guest User

hiekkaratnta2

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