Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hiekkaranta;
- import javafx.animation.AnimationTimer;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.RadioButton;
- import javafx.scene.control.ToggleGroup;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.stage.Stage;
- public class HiekkarantaSovellus extends Application {
- @Override
- public void start(Stage ikkuna) {
- Canvas piirtoalusta = new Canvas(200, 200);
- GraphicsContext piirturi = piirtoalusta.getGraphicsContext2D();
- Simulaatio simulaatio = new Simulaatio(200, 200);
- GraphicsContext pisteet = piirtoalusta.getGraphicsContext2D();
- piirturi.setFill(Color.BLACK);
- piirturi.fillRect(0, 0, piirtoalusta.getWidth(), piirtoalusta.getHeight());
- ToggleGroup group = new ToggleGroup();
- RadioButton metalli = new RadioButton("Metalli");
- RadioButton hiekka = new RadioButton("Hiekka");
- RadioButton vesi = new RadioButton("Vesi");
- metalli.setToggleGroup(group);
- hiekka.setToggleGroup(group);
- vesi.setToggleGroup(group);
- VBox nappienAsettelu = new VBox();
- nappienAsettelu.getChildren().addAll(metalli, hiekka, vesi);
- BorderPane asettelu = new BorderPane();
- asettelu.setCenter(piirtoalusta);
- asettelu.setRight(nappienAsettelu);
- new AnimationTimer() {
- long edellinen = 0;
- @Override
- public void handle(long nykyhetki) {
- if (nykyhetki - edellinen < 10000000) {
- return;
- }
- simulaatio.paivita();
- }
- }.start();
- new AnimationTimer() {
- long edellinen = 0;
- @Override
- public void handle(long nykyhetki) {
- if (nykyhetki - edellinen < 10000000) {
- return;
- }
- pisteet.setFill(Color.BLACK);
- pisteet.fillRect(0, 0, piirtoalusta.getWidth(), piirtoalusta.getHeight());
- //simulaation piirto
- for (int y = 0; y < simulaatio.getKorkeus(); y++) {
- for (int x = 0; x < simulaatio.getLeveys(); x++) {
- Tyyppi t = simulaatio.sisalto(x, y);
- if (t == Tyyppi.METALLI) {
- pisteet.setFill(Color.WHITE);
- }
- if (t == Tyyppi.HIEKKA) {
- pisteet.setFill(Color.ORANGE);
- }
- if (t == Tyyppi.VESI) {
- pisteet.setFill(Color.LIGHTBLUE);
- }
- pisteet.fillRect( x, y, 1, 1);
- }
- }
- }
- }.start();
- metalli.setOnAction((event) -> {
- piirturi.setFill(Color.WHITE);
- });
- hiekka.setOnAction((event) -> {
- piirturi.setFill(Color.ORANGE);
- });
- vesi.setOnAction((event) -> {
- piirturi.setFill(Color.LIGHTBLUE);
- });
- piirtoalusta.setOnMouseDragged((event) -> {
- double kohtaX = event.getX();
- double kohtaY = event.getY();
- piirturi.fillOval(kohtaX, kohtaY, 10, 10);
- if (piirturi.getFill() == Color.WHITE) {
- simulaatio.lisaa((int) Math.round(kohtaX), (int) Math.round(kohtaY), Tyyppi.METALLI);
- }
- if (piirturi.getFill() == Color.ORANGE) {
- simulaatio.lisaa((int) Math.round(kohtaX), (int) Math.round(kohtaY), Tyyppi.HIEKKA);
- }
- if (piirturi.getFill() == Color.LIGHTBLUE) {
- simulaatio.lisaa((int) Math.round(kohtaX), (int) Math.round(kohtaY), Tyyppi.VESI);
- }
- });
- Scene nakyma = new Scene(asettelu);
- ikkuna.setScene(nakyma);
- ikkuna.show();
- }
- public static void main(String[] args) {
- launch(HiekkarantaSovellus.class);
- }
- }
- src/hiekkaranta/Simulaatio.java
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package hiekkaranta;
- import hiekkaranta.Tyyppi;
- import java.util.Random;
- /**
- *
- * @author Pete
- */
- public class Simulaatio {
- private Tyyppi[][] taulukko;
- private int leveys;
- private int korkeus;
- public Simulaatio(int leveys, int korkeus) {
- Tyyppi[][] taulukko = new Tyyppi[korkeus][leveys];
- this.taulukko = taulukko;
- this.leveys = leveys;
- this.korkeus = korkeus;
- for (int y = 0; y < this.korkeus; y++) {
- for (int x = 0; x < this.leveys; x++) {
- this.taulukko[y][x] = Tyyppi.TYHJA;
- }
- }
- }
- public int getLeveys() {
- return this.leveys;
- }
- public int getKorkeus() {
- return this.korkeus;
- }
- public void lisaa(int x, int y, Tyyppi tyyppi) {
- if (x < 0 || y < 0 || x >= this.leveys || y >= this.korkeus) {
- } else {
- this.taulukko[y][x] = tyyppi;
- }
- }
- public Tyyppi sisalto(int x, int y) {
- if (x < 0 || x >= leveys) {
- return Tyyppi.METALLI;
- }
- if (y < 0 || y >= korkeus) {
- return Tyyppi.METALLI;
- }
- Tyyppi tyyppi = this.taulukko[y][x];
- return tyyppi;
- }
- public void paivita() {
- Random arpoja = new Random();
- for (int y = 0; y < this.korkeus; y++) {
- for (int x = 0; x < this.leveys; x++) {
- if (this.taulukko[y][x] == Tyyppi.HIEKKA) {
- this.taulukko[y][x] = Tyyppi.TYHJA;
- this.taulukko[y + 1][x] = Tyyppi.HIEKKA;
- }
- }
- }
- }
- }
- src/hiekkaranta/Tyyppi.java
- package hiekkaranta;
- public enum Tyyppi {
- TYHJA, METALLI, HIEKKA, VESI;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement