gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under the git version control system
summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: a70aa3a)
raw | patch | inline | side by side (parent: a70aa3a)
author | Ivan Pribela <ivanpribela@gmail.com> | |
Mon, 27 Nov 2017 14:03:38 +0000 (15:03 +0100) | ||
committer | Ivan Pribela <ivanpribela@gmail.com> | |
Mon, 27 Nov 2017 14:03:38 +0000 (15:03 +0100) |
PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Prikaz.java | [new file with mode: 0644] | patch | blob |
diff --git a/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Prikaz.java b/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Prikaz.java
--- /dev/null
@@ -0,0 +1,489 @@
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.LinkedHashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.function.Function;\r
+\r
+import javafx.application.Application;\r
+import javafx.application.Platform;\r
+import javafx.collections.FXCollections;\r
+import javafx.collections.ObservableList;\r
+import javafx.geometry.Insets;\r
+import javafx.geometry.VPos;\r
+import javafx.scene.Scene;\r
+import javafx.scene.canvas.Canvas;\r
+import javafx.scene.canvas.GraphicsContext;\r
+import javafx.scene.control.Button;\r
+import javafx.scene.control.Label;\r
+import javafx.scene.control.ListCell;\r
+import javafx.scene.control.ListView;\r
+import javafx.scene.control.Slider;\r
+import javafx.scene.layout.BorderPane;\r
+import javafx.scene.layout.ColumnConstraints;\r
+import javafx.scene.layout.GridPane;\r
+import javafx.scene.layout.Priority;\r
+import javafx.scene.layout.StackPane;\r
+import javafx.scene.layout.VBox;\r
+import javafx.scene.paint.Color;\r
+import javafx.scene.paint.Paint;\r
+import javafx.scene.shape.Circle;\r
+import javafx.scene.shape.StrokeLineCap;\r
+import javafx.scene.shape.StrokeLineJoin;\r
+import javafx.scene.text.Font;\r
+import javafx.scene.text.FontWeight;\r
+import javafx.scene.text.TextAlignment;\r
+import javafx.stage.Stage;\r
+\r
+public class Prikaz extends Application {\r
+\r
+ public static final Color STVARNO_BELA = Color.hsb(0, 0, 1.0);\r
+ public static final Color BELA = Color.hsb(0, 0, 0.95);\r
+ public static final Color SVETLO_SIVA = Color.hsb(0, 0, 0.8);\r
+ public static final Color SIVA = Color.hsb(0, 0, 0.6);\r
+ public static final Color TAMNO_SIVA = Color.hsb(0, 0, 0.4);\r
+ public static final Color CRNA = Color.hsb(0, 0, 0.2);\r
+ public static final Color STVARNO_CRNA = Color.hsb(0, 0, 0.0);\r
+\r
+ public static final Color SVETLO_CRVENA = Color.hsb( 0, 0.2, 1.0);\r
+ public static final Color SVETLO_NARANDZASTA = Color.hsb( 30, 0.2, 1.0);\r
+ public static final Color SVETLO_ZUTA = Color.hsb( 60, 0.2, 1.0);\r
+ public static final Color SVETLO_ZELENA = Color.hsb(120, 0.2, 1.0);\r
+ public static final Color SVETLO_TIRKIZNA = Color.hsb(180, 0.2, 1.0);\r
+ public static final Color SVETLO_AZURNA = Color.hsb(210, 0.2, 1.0);\r
+ public static final Color SVETLO_PLAVA = Color.hsb(240, 0.2, 1.0);\r
+ public static final Color SVETLO_PURPURNA = Color.hsb(270, 0.2, 1.0);\r
+ public static final Color SVETLO_LJUBICASTA = Color.hsb(300, 0.2, 1.0);\r
+ public static final Color SVETLO_RUZICASTA = Color.hsb(330, 0.2, 1.0);\r
+\r
+ public static final Color CRVENA = Color.hsb( 0, 0.9, 0.8);\r
+ public static final Color NARANDZASTA = Color.hsb( 30, 0.9, 0.8);\r
+ public static final Color ZUTA = Color.hsb( 60, 0.9, 0.8);\r
+ public static final Color ZELENA = Color.hsb(120, 0.9, 0.8);\r
+ public static final Color TIRKIZNA = Color.hsb(180, 0.9, 0.8);\r
+ public static final Color AZURNA = Color.hsb(210, 0.9, 0.8);\r
+ public static final Color PLAVA = Color.hsb(240, 0.9, 0.8);\r
+ public static final Color PURPURNA = Color.hsb(270, 0.9, 0.8);\r
+ public static final Color LJUBICASTA = Color.hsb(300, 0.9, 0.8);\r
+ public static final Color RUZICASTA = Color.hsb(330, 0.9, 0.8);\r
+\r
+ public static final Color TAMNO_CRVENA = Color.hsb( 0, 0.8, 0.4);\r
+ public static final Color TAMNO_NARANDZASTA = Color.hsb( 30, 0.8, 0.4);\r
+ public static final Color TAMNO_ZUTA = Color.hsb( 60, 0.8, 0.4);\r
+ public static final Color TAMNO_ZELENA = Color.hsb(120, 0.8, 0.4);\r
+ public static final Color TAMNO_TIRKIZNA = Color.hsb(180, 0.8, 0.4);\r
+ public static final Color TAMNO_AZURNA = Color.hsb(210, 0.8, 0.4);\r
+ public static final Color TAMNO_PLAVA = Color.hsb(240, 0.8, 0.4);\r
+ public static final Color TAMNO_PURPURNA = Color.hsb(270, 0.8, 0.4);\r
+ public static final Color TAMNO_LJUBICASTA = Color.hsb(300, 0.8, 0.4);\r
+ public static final Color TAMNO_RUZICASTA = Color.hsb(330, 0.8, 0.4);\r
+\r
+ ////////////////////\r
+ // Boje i stilovi //\r
+ ////////////////////\r
+\r
+ protected static Paint bojaPozadine = BELA;\r
+ protected static Paint bojaOkvira = CRNA;\r
+ protected static Map<Integer, Paint> bojaPoljaPozadina = new HashMap<>();\r
+ protected static Map<Integer, Paint> bojaPoljaTekst = new HashMap<>();\r
+ protected static Font font = Font.font("Arial", FontWeight.BOLD, 12);\r
+\r
+ public static void boja(Paint pozadina, Paint okvir) {\r
+ bojaPozadine = pozadina;\r
+ bojaOkvira = okvir;\r
+ }\r
+\r
+ public static void boja(int vrednost, Paint bojaPozadine, Paint bojaTeksta) {\r
+ if (bojaPozadine == null) {\r
+ bojaPoljaPozadina.remove(vrednost);\r
+ }\r
+ bojaPoljaPozadina.put(vrednost, bojaPozadine);\r
+ if (bojaTeksta == null) {\r
+ bojaPoljaTekst.remove(vrednost);\r
+ }\r
+ bojaPoljaTekst.put(vrednost, bojaTeksta);\r
+ }\r
+\r
+ public static void boja(int vrednostMin, int vrednostMax, Color bojaMin, Color bojaMax, Paint bojaTeksta) {\r
+ if (bojaMin == null || bojaMax == null) {\r
+ throw new IllegalArgumentException();\r
+ }\r
+ int n = vrednostMax - vrednostMin;\r
+ for (int i = 0; i <= n; i++) {\r
+ int vrednost = vrednostMin + i;\r
+ bojaPoljaPozadina.put(vrednost, bojaMin.interpolate(bojaMax, (double) (i) / n));\r
+ if (bojaTeksta == null) {\r
+ bojaPoljaTekst.remove(vrednost);\r
+ }\r
+ bojaPoljaTekst.put(vrednost, bojaTeksta);\r
+ }\r
+ }\r
+\r
+ ////////////////\r
+ // Geometrija //\r
+ ////////////////\r
+\r
+ private static double velicinaPolja = 32;\r
+ private static double velicinaRazmaka = 4;\r
+ private static int debljinaOkvira = 0;\r
+ private static double debljinaPuta = velicinaPolja / 2;\r
+\r
+ public static void velicina(double polje, double razmak, int okvir) {\r
+ velicinaPolja = polje;\r
+ velicinaRazmaka = razmak;\r
+ debljinaOkvira = okvir;\r
+ debljinaPuta = velicinaPolja / 2;\r
+ }\r
+\r
+ protected static double ukupnaDuzina(int brojPolja) {\r
+ return x(brojPolja + debljinaOkvira, 0.0);\r
+ }\r
+\r
+ protected static double x(int indeks, double delta) {\r
+ indeks = indeks + debljinaOkvira;\r
+ return indeks * (velicinaPolja + velicinaRazmaka) + velicinaRazmaka + velicinaPolja * delta;\r
+ }\r
+\r
+ protected static double x(int indeks) {\r
+ return x(indeks, 0.0);\r
+ }\r
+\r
+ protected static double k(int indeks) {\r
+ return x(indeks, 0.5);\r
+ }\r
+\r
+ protected static double w(int count, double delta) {\r
+ return (velicinaPolja + velicinaRazmaka) * count + velicinaRazmaka * (delta - 1);\r
+ }\r
+\r
+ protected static double w(int count) {\r
+ return w(count, 0);\r
+ }\r
+\r
+ protected static double r() {\r
+ return 2 * velicinaRazmaka;\r
+ }\r
+\r
+ /////////////\r
+ // Sadrzaj //\r
+ /////////////\r
+\r
+ protected static String naslovProzora;\r
+ protected static int sirinaMape;\r
+ protected static int visinaMape;\r
+ protected static FunkcijaZaMapu funkcijaZaMapu;\r
+\r
+ @FunctionalInterface\r
+ public static interface FunkcijaZaMapu {\r
+ public int vrednostNa(int x, int y);\r
+ }\r
+\r
+ public static void mapa(String naslov, int sirina, int visina, FunkcijaZaMapu funkcijaZaMapu) {\r
+ naslovProzora = naslov;\r
+ sirinaMape = sirina;\r
+ visinaMape = visina;\r
+ Prikaz.funkcijaZaMapu = funkcijaZaMapu;\r
+ pokreni();\r
+ }\r
+\r
+ protected static Map<String, FunkcijaZaPut> funkcijeZaPuteve = new LinkedHashMap<>();\r
+ protected static Map<String, FunkcijaZaVrednostPuta> funkcijeZaVrednostiPuteva = new LinkedHashMap<>();\r
+ protected static Map<String, Paint> bojeZaPuteve = new HashMap<>();\r
+\r
+ @FunctionalInterface\r
+ public static interface FunkcijaZaPut {\r
+ public KorakPuta korak(int indeks);\r
+ }\r
+\r
+ @FunctionalInterface\r
+ public static interface FunkcijaZaVrednostPuta {\r
+ public String vrednost();\r
+ }\r
+\r
+ public static class KorakPuta {\r
+ public int x, y;\r
+ }\r
+\r
+ public static KorakPuta korak(int x, int y) {\r
+ KorakPuta korak = new KorakPuta();\r
+ korak.x = x; korak.y = y;\r
+ return korak;\r
+ }\r
+\r
+ public static void put(String naziv, FunkcijaZaPut funkcijaZaPut, FunkcijaZaVrednostPuta funkcijaZaVrednostPuta, Paint boja) {\r
+ funkcijeZaPuteve.remove(naziv);\r
+ funkcijeZaVrednostiPuteva.remove(naziv);\r
+ bojeZaPuteve.remove(naziv);\r
+ odradiKasnije(() -> {\r
+ listaPuteva.remove(naziv);\r
+ Canvas platno = platnaZaPuteve.remove(naziv);\r
+ centerPane.getChildren().remove(platno);\r
+ });\r
+ if (funkcijaZaPut != null && funkcijaZaVrednostPuta != null && boja != null) {\r
+ funkcijeZaPuteve.put(naziv, funkcijaZaPut);\r
+ funkcijeZaVrednostiPuteva.put(naziv, funkcijaZaVrednostPuta);\r
+ bojeZaPuteve.put(naziv, boja);\r
+ odradiKasnije(() -> {\r
+ Canvas platno = new Canvas(Prikaz.platno.getWidth(), Prikaz.platno.getHeight());\r
+ platno.setOpacity(0.5);\r
+ platno.getGraphicsContext2D().setLineCap(StrokeLineCap.ROUND);\r
+ platno.getGraphicsContext2D().setLineJoin(StrokeLineJoin.ROUND);\r
+ platnaZaPuteve.put(naziv, platno);\r
+ centerPane.getChildren().add(1, platno);\r
+ listaPuteva.add(naziv);\r
+ crtajPut(naziv);\r
+ });\r
+ }\r
+ }\r
+\r
+ public static <T> void put(String naziv, List<T> lista, Function<T, Integer> x, Function<T, Integer> y, FunkcijaZaVrednostPuta funkcijaZaVrednostPuta, Paint boja) {\r
+ FunkcijaZaPut fja = (int indeks) -> {\r
+ try {\r
+ T element = lista.get(indeks);\r
+ return korak(x.apply(element), y.apply(element));\r
+ } catch (IndexOutOfBoundsException e) {\r
+ return null;\r
+ }\r
+ };\r
+ put(naziv, fja, funkcijaZaVrednostPuta, boja);\r
+ }\r
+\r
+ public static void osveziPut(String naziv) {\r
+ odradiKasnije(() -> {\r
+ crtajPut(naziv);\r
+ listaPuteva.set(listaPuteva.indexOf(naziv), naziv);\r
+ });\r
+ cekajAkoTreba();\r
+ }\r
+\r
+ /////////\r
+ // GUI //\r
+ /////////\r
+\r
+ protected static StackPane centerPane;\r
+ protected static ListView<String> listView;\r
+ protected static ObservableList<String> listaPuteva = FXCollections.observableArrayList();\r
+\r
+ @Override\r
+ public void start(Stage primaryStage) {\r
+ Label labelPutevi = new Label("Putevi:");\r
+ listView = new ListView<>(listaPuteva);\r
+ listView.setCellFactory(l -> new ListCell<String>() {\r
+ public void updateItem(String naziv, boolean empty) {\r
+ super.updateItem(naziv, empty);\r
+ if (empty || naziv == null) {\r
+ setText(null);\r
+ setGraphic(null);\r
+ } else {\r
+ Circle icon = new Circle(8);\r
+ icon.setFill(bojeZaPuteve.get(naziv));\r
+ setGraphic(icon);\r
+ FunkcijaZaVrednostPuta fja = funkcijeZaVrednostiPuteva.get(naziv);\r
+ try {\r
+ setText(naziv + ": " + fja.vrednost());\r
+ } catch (RuntimeException e) {\r
+ }\r
+ }\r
+ }\r
+ });\r
+\r
+ Label labelAnimacija = new Label();\r
+ Slider slider = new Slider(0, 4, 2);\r
+ Button button = new Button(">");\r
+ osveziPanelZaAnimaciju(labelAnimacija, button, slider.getValue());\r
+ slider.valueProperty().addListener((value, oldValue, newValue) -> {\r
+ osveziPanelZaAnimaciju(labelAnimacija, button, newValue.doubleValue());\r
+ });\r
+ button.setOnAction(e -> {\r
+ zavrsiCekanje();\r
+ });\r
+ button.setMaxHeight(Double.MAX_VALUE);\r
+\r
+ GridPane animationPane = new GridPane();\r
+ animationPane.setHgap(6);\r
+ animationPane.setVgap(6);\r
+ ColumnConstraints column = new ColumnConstraints();\r
+ column.setHgrow(Priority.ALWAYS);\r
+ animationPane.getColumnConstraints().add(column);\r
+ animationPane.add(labelAnimacija, 0, 0);\r
+ animationPane.add(slider, 0, 1);\r
+ animationPane.add(button, 1, 0, 1, 2);\r
+\r
+ VBox rightPane = new VBox();\r
+ rightPane.setPadding(new Insets(12, 12, 12, 12));\r
+ rightPane.setSpacing(6);\r
+ rightPane.getChildren().add(labelPutevi);\r
+ listView.setPrefHeight(0);\r
+ VBox.setVgrow(listView, Priority.ALWAYS);\r
+ rightPane.getChildren().add(listView);\r
+ VBox.setMargin(animationPane, new Insets(6, 0, 0, 0));\r
+ rightPane.getChildren().add(animationPane);\r
+\r
+ platno = new Canvas(ukupnaDuzina(sirinaMape), ukupnaDuzina(visinaMape));\r
+ osveziMapu();\r
+ centerPane = new StackPane(platno);\r
+\r
+ BorderPane rootPane = new BorderPane();\r
+ rootPane.setCenter(centerPane);\r
+ rootPane.setRight(rightPane);\r
+ Scene scene = new Scene(rootPane);\r
+\r
+ primaryStage.setTitle(naslovProzora + " - " + sirinaMape + " x " + visinaMape + "");\r
+ primaryStage.setAlwaysOnTop(true);\r
+ primaryStage.setResizable(false);\r
+ primaryStage.setOnCloseRequest(e -> podesiVreme(0));\r
+ primaryStage.setScene(scene);\r
+ primaryStage.show();\r
+\r
+ }\r
+\r
+ protected static void osveziPanelZaAnimaciju(Label labela, Button dugme, double vrednost) {\r
+ long vreme = podesiVreme(vrednost);\r
+ String tekst = "Brzina animacije: " + String.format("%.2f", vreme / 1000.0) + "s";\r
+ dugme.setVisible(vreme >= 1000);\r
+ labela.setText(tekst);\r
+ }\r
+\r
+ /////////////\r
+ // Crtanje //\r
+ /////////////\r
+\r
+ protected static Canvas platno;\r
+ protected static Map<String, Canvas> platnaZaPuteve = new LinkedHashMap<>();\r
+\r
+ protected static void osveziMapu() {\r
+ crtajMapu(platno.getGraphicsContext2D(), platno.getWidth(), platno.getHeight());\r
+ }\r
+\r
+ protected static void crtajMapu(GraphicsContext gc, double sirina, double visina) {\r
+ gc.setTextAlign(TextAlignment.CENTER);\r
+ gc.setTextBaseline(VPos.CENTER);\r
+ gc.setFont(font);\r
+ crtajOkvir(gc, sirina, visina);\r
+ for (int j = 0; j < visinaMape; j++) {\r
+ for (int i = 0; i < sirinaMape; i++) {\r
+ int sadrzaj = funkcijaZaMapu.vrednostNa(i, j);\r
+ Paint bojaPozadina = bojaPoljaPozadina.get(sadrzaj);\r
+ Paint bojaTekst = bojaPoljaTekst.get(sadrzaj);\r
+ crtajPolje(gc, i, j, bojaPozadina, bojaTekst, "" + sadrzaj);\r
+ }\r
+ }\r
+ }\r
+\r
+ protected static void crtajOkvir(GraphicsContext gc, double sirina, double visina) {\r
+ gc.setFill(bojaPozadine);\r
+ gc.fillRect(0, 0, sirina, visina);\r
+ if (debljinaOkvira <= 0) {\r
+ return;\r
+ }\r
+ gc.setFill(bojaOkvira);\r
+ gc.fillRoundRect(x(-debljinaOkvira), x(-debljinaOkvira), w(sirinaMape + 2 * debljinaOkvira), w(visinaMape + 2 * debljinaOkvira), r(), r());\r
+ gc.setFill(bojaPozadine);\r
+ gc.fillRoundRect(x(-1, 1), x(-1, 1), w(sirinaMape, 2), w(visinaMape, 2), r(), r());\r
+ }\r
+\r
+ protected static void crtajPolje(GraphicsContext gc, int x, int y, Paint bojaPozadina, Paint bojaTekst, String tekst) {\r
+ if (bojaPozadina != null) {\r
+ gc.setFill(bojaPozadina);\r
+ gc.fillRoundRect(x(x), x(y), w(1), w(1), r(), r());\r
+ }\r
+ if (tekst != null && bojaTekst != null) {\r
+ gc.setFill(bojaTekst);\r
+ gc.fillText(tekst, x(x, 0.5), x(y, 0.5));\r
+ }\r
+ }\r
+\r
+ protected static void crtajPut(String naziv) {\r
+ Canvas platno = platnaZaPuteve.get(naziv);\r
+ GraphicsContext gc = platno.getGraphicsContext2D();\r
+ gc.clearRect(0, 0, platno.getWidth(), platno.getHeight());\r
+ FunkcijaZaPut fja = funkcijeZaPuteve.get(naziv);\r
+ Paint boja = bojeZaPuteve.get(naziv);\r
+ double debljina = debljinaPuta;\r
+ List<KorakPuta> koraci = new ArrayList<>();\r
+ int i = 0; KorakPuta korak;\r
+ while ((korak = fja.korak(i++)) != null) {\r
+ koraci.add(korak);\r
+ }\r
+ crtajPut(gc, koraci, boja, debljina);\r
+ }\r
+\r
+ protected static void crtajPut(GraphicsContext gc, List<KorakPuta> koraci, Paint boja, double debljina) {\r
+ gc.setStroke(boja);\r
+ gc.setLineWidth(debljina);\r
+ if (koraci.size() == 1) {\r
+ KorakPuta k = koraci.get(0);\r
+ gc.setFill(boja);\r
+ gc.fillOval(k(k.x) - debljina / 2, k(k.y) - debljina / 2, debljina, debljina);\r
+ }\r
+ for (int j = 1; j < koraci.size(); j++) {\r
+ KorakPuta k1 = koraci.get(j - 1);\r
+ KorakPuta k2 = koraci.get(j);\r
+ gc.strokeLine(k(k1.x), k(k1.y), k(k2.x), k(k2.y));\r
+ }\r
+ }\r
+\r
+ ///////////////\r
+ // Animacija //\r
+ ///////////////\r
+\r
+ protected static boolean nekoCeka = false;\r
+ protected static long vremeCekanja = 1000;\r
+\r
+ protected static synchronized void cekajAkoTreba() {\r
+ long pocetak = System.currentTimeMillis();\r
+ if (vremeCekanja == 0) {\r
+ return;\r
+ }\r
+ nekoCeka = true;\r
+ long vreme;\r
+ boolean bioPrekid = Thread.interrupted();\r
+ while (nekoCeka && (vreme = pocetak + vremeCekanja - System.currentTimeMillis()) > 0) {\r
+ try {\r
+ Prikaz.class.wait(vreme);\r
+ } catch (InterruptedException e) {\r
+ bioPrekid = true;\r
+ }\r
+ }\r
+ if (bioPrekid) {\r
+ Thread.currentThread().interrupt();\r
+ }\r
+ nekoCeka = false;\r
+ }\r
+\r
+ protected static synchronized void podesiVreme(long novoVremeCekanja) {\r
+ vremeCekanja = novoVremeCekanja;\r
+ Prikaz.class.notify();\r
+ }\r
+\r
+ protected static synchronized void zavrsiCekanje() {\r
+ nekoCeka = false;\r
+ Prikaz.class.notify();\r
+ }\r
+\r
+ protected static long podesiVreme(double stepen) {\r
+ stepen = 5 - stepen;\r
+ if (stepen <= 1) {\r
+ podesiVreme(0);\r
+ return 0;\r
+ }\r
+ long vrednost = (long) Math.pow(10, stepen);\r
+ podesiVreme(vrednost);\r
+ return vrednost;\r
+ }\r
+\r
+ ////////////////\r
+ // Pokretanje //\r
+ ////////////////\r
+\r
+ protected static void pokreni() {\r
+ Runnable launcher = () -> {\r
+ launch(new String[] {});\r
+ };\r
+ new Thread(launcher).start();\r
+ }\r
+\r
+ protected static void odradiKasnije(Runnable zadatak) {\r
+ Platform.runLater(zadatak);\r
+ }\r
+}\r