jotto

Untitled

Jan 4th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.74 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.         int H = 700;
  171.         int W = 1000;
  172.        
  173.         Group root = new Group();
  174.         Scene scene_game = new Scene(root, W, H, Color.BLACK);
  175.        
  176.         Button button_menu = new Button("Menu");
  177.         button_menu.setOnAction(e ->{
  178.             createMenu(primaryStage);
  179.         });
  180.                
  181.         Double sceneHeight = scene_game.getHeight();
  182.         Double sceneWidth = scene_game.getWidth();
  183.         Double rectHeigth = 100.0;
  184.        
  185.         Image ship = new Image((getClass().getResourceAsStream("ship.png")));
  186.        
  187.         /**********************************************************************/                
  188.         final Canvas canvas_shot = new Canvas(W, H);
  189.         final Canvas canvas_coast = new Canvas(W, H);
  190.        
  191.         AnimationTimer timer = new AnimationTimer() {
  192.             int[] j = {0,0,0,0,0,0,0,0};
  193.             GraphicsContext b1 = canvas_coast.getGraphicsContext2D();
  194.            
  195.             @Override
  196.             public void handle(long now) {
  197.                
  198.                 if (j[0]==801) j[0]=0;
  199.                 if (j[1]==901) j[1]=100;
  200.                 if (j[2]==1001) j[2]=200;
  201.                 if (j[3]==1101) j[3]=300;
  202.                 if (j[4]==1201) j[4]=400;
  203.                 if (j[5]==1301) j[5]=500;
  204.                 if (j[6]==1401) j[6]=600;
  205.                 if (j[7]==1501) j[7]=700;
  206.  
  207.                 b1.setFill(Color.FORESTGREEN);
  208.                
  209.                 for (int i=1;i<9;i++) {
  210.                     b1.fillRect(0, j[i-1]-(i*100), 300+i*10, 100);
  211.                     b1.clearRect(0, j[i-1]-(i*100)-100, 300+i*10, 100);
  212.                 }
  213.                
  214.                 for (int i=0;i<8;i++){
  215.                     j[i]+=1;
  216.                 }
  217.             }
  218.         };
  219.                 timer.start();                
  220.         /**********************************************************************/
  221.  
  222.         root.getChildren().add(canvas_coast);
  223.         root.getChildren().add(button_menu);
  224.         root.getChildren().add(canvas_shot);
  225.         primaryStage.setScene(scene_game);
  226.     }
  227.  
  228.     /**
  229.      * @param args the command line arguments
  230.      */
  231.     public static void main(String[] args) {
  232.         launch(args);
  233.     }
  234.    
  235. }
Advertisement
Add Comment
Please, Sign In to add comment