jotto

Jeszcze działa, sprawdzam ruszanie

Jan 4th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.91 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package riverpuff.v3;
  8.  
  9. import java.io.BufferedReader;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileReader;
  12. import java.io.IOException;
  13. import static java.lang.Math.random;
  14. import static java.lang.Thread.sleep;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javafx.animation.AnimationTimer;
  18. import javafx.animation.KeyFrame;
  19. import javafx.animation.KeyValue;
  20. import javafx.animation.Timeline;
  21. import javafx.application.Application;
  22. import javafx.beans.property.DoubleProperty;
  23. import javafx.beans.property.SimpleDoubleProperty;
  24. import javafx.geometry.Pos;
  25. import javafx.scene.Group;
  26. import javafx.scene.Scene;
  27. import javafx.scene.canvas.Canvas;
  28. import javafx.scene.canvas.GraphicsContext;
  29. import javafx.scene.control.Button;
  30. import javafx.scene.control.TextField;
  31. import javafx.scene.image.Image;
  32. import javafx.scene.input.KeyCode;
  33. import javafx.scene.layout.GridPane;
  34. import javafx.scene.paint.Color;
  35. import javafx.scene.text.Font;
  36. import javafx.scene.text.Text;
  37. import javafx.stage.Stage;
  38. import javafx.util.Duration;
  39.  
  40. /**
  41.  *
  42.  * @author Marek
  43.  */
  44. public class RiverPuffV3 extends Application {
  45.    
  46.     public String name = "";
  47.    
  48.     @Override
  49.     public void start(Stage primaryStage) {
  50.  
  51.         createMenu(primaryStage);
  52.                
  53.         primaryStage.show();
  54.         primaryStage.setResizable(false);
  55.         primaryStage.getIcons().
  56.                 add(new Image(getClass().getResourceAsStream("Icon.png")));
  57.     }
  58.    
  59.     private void createMenu(Stage primaryStage) {
  60.        
  61.         primaryStage.setScene(null);
  62.        
  63.         GridPane pane_menu = new GridPane();
  64.                
  65.         Button button_name = new Button("Name");
  66.         button_name.setMaxHeight(Double.MAX_VALUE);
  67.         button_name.setMaxWidth(Double.MAX_VALUE);
  68.         button_name.setOnAction(e -> {
  69.             createName(primaryStage);
  70.             /*try{        
  71.                 OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream("log.txt", true), "UTF-8");
  72.                 BufferedWriter fbw = new BufferedWriter(writer);
  73.                 fbw.newLine();
  74.                 fbw.write("append txt...");
  75.                 fbw.newLine();
  76.                 fbw.close();
  77.             }
  78.             catch (Exception v) {
  79.             System.out.println("Error: " + v.getMessage());
  80.             }*/
  81.         });
  82.        
  83.         Button button_start = new Button("Start");
  84.         button_start.setMaxHeight(Double.MAX_VALUE);
  85.         button_start.setMaxWidth(Double.MAX_VALUE);
  86.         button_start.setOnAction(e -> {
  87.             drawGame(primaryStage);
  88.         });
  89.        
  90.         pane_menu.setHgap(10);
  91.         pane_menu.setVgap(10);
  92.         pane_menu.add(button_name,0,10,10,10);
  93.         pane_menu.add(button_start,15,10,10,10);
  94.        
  95.         try {
  96.             String read_file = null;
  97.             BufferedReader in = new BufferedReader(new FileReader("log.txt"));
  98.             read_file = in.readLine();
  99.             Text text_name = new Text("Hello " + read_file);
  100.             pane_menu.add(text_name,5,5,10,5);
  101.         } catch (FileNotFoundException e) {
  102.             System.out.println("File not found!");
  103.             //throw new RuntimeException("File not found");
  104.         } catch (IOException e) {
  105.             System.out.println("IO Error occured");
  106.             //throw new RuntimeException("IO Error occured");
  107.         }
  108.        
  109.         Scene scene_menu = new Scene(pane_menu, 300, 500);
  110.        
  111.         primaryStage.setTitle("River Puff");
  112.         primaryStage.setScene(scene_menu);
  113.        
  114.     }
  115.    
  116.     private void createName (Stage primaryStage) {
  117.        
  118.         primaryStage.setScene(null);
  119.        
  120.         GridPane pane_name = new GridPane();
  121.        
  122.         TextField tf_name = new TextField();
  123.         tf_name.setMaxHeight(50);
  124.         tf_name.setMaxWidth(240);
  125.         tf_name.setAlignment(Pos.CENTER);
  126.         tf_name.setFont(Font.font("Verdana",25));
  127.         tf_name.setOnKeyPressed(ke -> {
  128.             if (ke.getCode() == KeyCode.ENTER) {
  129.                 name = tf_name.getText();
  130.                 if (!name.isEmpty()){
  131.                     MyFile myFile = new MyFile();
  132.                     myFile.writeTextFile("log.txt", name);
  133.                 }
  134.                 createMenu(primaryStage);
  135.             }
  136.         });
  137.         Button button_ok = new Button("OK");        
  138.         button_ok.setMaxHeight(30);
  139.         button_ok.setMaxWidth(80);
  140.         button_ok.setOnAction(e -> {
  141.             name = tf_name.getText();
  142.             if (!name.isEmpty()){
  143.                 MyFile myFile = new MyFile();
  144.                 myFile.writeTextFile("log.txt", name);
  145.             }
  146.             createMenu(primaryStage);
  147.         });
  148.        
  149.         Text text_name = new Text("What is your name?");
  150.         text_name.setFont(Font.font("Verdana",15));
  151.        
  152.         pane_name.setHgap(10);
  153.         pane_name.setVgap(10);
  154.        
  155.         pane_name.add(text_name,8,9,5,5);
  156.         pane_name.add(button_ok,11,22,8,3);
  157.         pane_name.add(tf_name,3,15,24,5);
  158.        
  159.         Scene scene_name = new Scene(pane_name, 300, 500);
  160.        
  161.         primaryStage.setTitle("River Puff - Name");
  162.         primaryStage.setScene(scene_name);
  163.                
  164.     }
  165.    
  166.     private void drawGame (Stage primaryStage) {
  167.        
  168.         primaryStage.setScene(null);
  169.        
  170.         Group root = new Group();
  171.         Scene scene_game = new Scene(root, 800, 800, Color.BLACK);
  172.        
  173.         Button button_menu = new Button("Menu");
  174.         button_menu.setOnAction(e ->{
  175.             createMenu(primaryStage);
  176.         });
  177.                
  178.         Double sceneHeight = scene_game.getHeight();
  179.         Double sceneWidth = scene_game.getWidth();
  180.         Double rectHeigth = 100.0;
  181.        
  182.         Image ship = new Image((getClass().getResourceAsStream("ship.png")));
  183.        
  184.         final Canvas canvas_coast = new Canvas(800,800);
  185.        
  186.         GraphicsContext coast = canvas_coast.getGraphicsContext2D();
  187.  
  188.         for (int i = 0; i< (sceneHeight/rectHeigth); i++){
  189.             Double temp = random();
  190.             Double temp1 = random();
  191.             coast.setFill(Color.FORESTGREEN);
  192.             coast.fillRect(0, i * rectHeigth, (0.2 + temp1/20) * sceneWidth,
  193.                     rectHeigth);            
  194.             coast.fillRect(sceneWidth - ((0.2 + temp/20) * sceneWidth),
  195.                     i * rectHeigth, (0.2 + temp/20) * sceneWidth, rectHeigth);
  196.             coast.setFill(Color.TEAL);
  197.             coast.fillRect((0.2 + temp1/20) * sceneWidth, i * rectHeigth,
  198.                     (1 - (0.4 + (temp + temp1)/20)) * sceneWidth, rectHeigth);
  199.         }
  200.        
  201.         /**********************************************************************/
  202.        
  203.         DoubleProperty x  = new SimpleDoubleProperty();
  204.         DoubleProperty y  = new SimpleDoubleProperty();
  205.        
  206.         Timeline timeline = new Timeline(
  207.             new KeyFrame(Duration.seconds(0),
  208.                 new KeyValue(x, 390),
  209.                 new KeyValue(y, 50)
  210.             ),
  211.             new KeyFrame(Duration.seconds(1),
  212.                 new KeyValue(x, 390),
  213.                 new KeyValue(y, 150)
  214.             )
  215.         );
  216.         timeline.setAutoReverse(false);
  217.         timeline.setCycleCount(Timeline.INDEFINITE);
  218.        
  219.         final Canvas canvas_shot = new Canvas(800, 800);
  220.        
  221.         AnimationTimer timer = new AnimationTimer() {
  222.             @Override
  223.             public void handle(long now) {
  224.                 GraphicsContext shot = canvas_shot.getGraphicsContext2D();
  225.                 shot.setFill(Color.FORESTGREEN);
  226.                 shot.fillOval(
  227.                     x.doubleValue(),
  228.                     y.doubleValue(),
  229.                     20,
  230.                     20
  231.                 );
  232.             }
  233.         };        
  234.                
  235.         scene_game.setOnKeyPressed(ke -> {
  236.             if (ke.getCode() == KeyCode.A) {
  237.                 timer.start();
  238.                 timeline.setCycleCount(1);
  239.                 timeline.play();
  240.             }
  241.         });
  242.        
  243.        
  244.                
  245.        
  246.         /**********************************************************************/
  247.  
  248.         root.getChildren().add(canvas_coast);
  249.         root.getChildren().add(button_menu);
  250.         root.getChildren().add(canvas_shot);
  251.         primaryStage.setScene(scene_game);
  252.     }
  253.    
  254.     /*EventHandler EH = new EventHandler<ActionEvent>(){
  255.  
  256.         @Override
  257.         public void handle(ActionEvent e) {
  258.             System.out.println(((Button)e.getSource()).getId());
  259.         }
  260.        
  261.     };*/
  262.  
  263.     /**
  264.      * @param args the command line arguments
  265.      */
  266.     public static void main(String[] args) {
  267.         launch(args);
  268.     }
  269.    
  270. }
Advertisement
Add Comment
Please, Sign In to add comment