jotto

tak winno być chyba

Jan 6th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.07 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.event.EventHandler;
  25. import javafx.geometry.Pos;
  26. import javafx.scene.Group;
  27. import javafx.scene.Scene;
  28. import javafx.scene.canvas.Canvas;
  29. import javafx.scene.canvas.GraphicsContext;
  30. import javafx.scene.control.Button;
  31. import javafx.scene.control.TextField;
  32. import javafx.scene.image.Image;
  33. import javafx.scene.input.KeyCode;
  34. import javafx.scene.input.KeyEvent;
  35. import javafx.scene.layout.GridPane;
  36. import javafx.scene.paint.Color;
  37. import javafx.scene.text.Font;
  38. import javafx.scene.text.Text;
  39. import javafx.stage.Stage;
  40. import javafx.util.Duration;
  41.  
  42. /**
  43.  *
  44.  * @author Marek
  45.  */
  46. public class RiverPuffV3 extends Application {
  47.    
  48.     public String name = "";
  49.     public int plane_pos = 475;
  50.    
  51.     @Override
  52.     public void start(Stage primaryStage) {
  53.  
  54.         createMenu(primaryStage);
  55.                
  56.         primaryStage.show();
  57.         primaryStage.setResizable(false);
  58.         primaryStage.getIcons().
  59.                 add(new Image(getClass().getResourceAsStream("Icon.png")));
  60.     }
  61.    
  62.     private void createMenu(Stage primaryStage) {
  63.        
  64.         primaryStage.setScene(null);
  65.        
  66.         GridPane pane_menu = new GridPane();
  67.                
  68.         Button button_name = new Button("Name");
  69.         button_name.setMaxHeight(Double.MAX_VALUE);
  70.         button_name.setMaxWidth(Double.MAX_VALUE);
  71.         button_name.setOnAction(e -> {
  72.             createName(primaryStage);
  73.             /*try{        
  74.                 OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream("log.txt", true), "UTF-8");
  75.                 BufferedWriter fbw = new BufferedWriter(writer);
  76.                 fbw.newLine();
  77.                 fbw.write("append txt...");
  78.                 fbw.newLine();
  79.                 fbw.close();
  80.             }
  81.             catch (Exception v) {
  82.             System.out.println("Error: " + v.getMessage());
  83.             }*/
  84.         });
  85.        
  86.         Button button_start = new Button("Start");
  87.         button_start.setMaxHeight(Double.MAX_VALUE);
  88.         button_start.setMaxWidth(Double.MAX_VALUE);
  89.         button_start.setOnAction(e -> {
  90.             drawGame(primaryStage);
  91.         });
  92.        
  93.         pane_menu.setHgap(10);
  94.         pane_menu.setVgap(10);
  95.         pane_menu.add(button_name,0,10,10,10);
  96.         pane_menu.add(button_start,15,10,10,10);
  97.        
  98.         try {
  99.             String read_file = null;
  100.             BufferedReader in = new BufferedReader(new FileReader("log.txt"));
  101.             read_file = in.readLine();
  102.             Text text_name = new Text("Hello " + read_file);
  103.             pane_menu.add(text_name,5,5,10,5);
  104.         } catch (FileNotFoundException e) {
  105.             System.out.println("File not found!");
  106.             //throw new RuntimeException("File not found");
  107.         } catch (IOException e) {
  108.             System.out.println("IO Error occured");
  109.             //throw new RuntimeException("IO Error occured");
  110.         }
  111.        
  112.         Scene scene_menu = new Scene(pane_menu, 300, 500);
  113.        
  114.         primaryStage.setTitle("River Puff");
  115.         primaryStage.setScene(scene_menu);
  116.        
  117.     }
  118.    
  119.     private void createName (Stage primaryStage) {
  120.        
  121.         primaryStage.setScene(null);
  122.        
  123.         GridPane pane_name = new GridPane();
  124.        
  125.         TextField tf_name = new TextField();
  126.         tf_name.setMaxHeight(50);
  127.         tf_name.setMaxWidth(240);
  128.         tf_name.setAlignment(Pos.CENTER);
  129.         tf_name.setFont(Font.font("Verdana",25));
  130.         tf_name.setOnKeyPressed(ke -> {
  131.             if (ke.getCode() == KeyCode.ENTER) {
  132.                 name = tf_name.getText();
  133.                 if (!name.isEmpty()){
  134.                     MyFile myFile = new MyFile();
  135.                     myFile.writeTextFile("log.txt", name);
  136.                 }
  137.                 createMenu(primaryStage);
  138.             }
  139.         });
  140.         Button button_ok = new Button("OK");        
  141.         button_ok.setMaxHeight(30);
  142.         button_ok.setMaxWidth(80);
  143.         button_ok.setOnAction(e -> {
  144.             name = tf_name.getText();
  145.             if (!name.isEmpty()){
  146.                 MyFile myFile = new MyFile();
  147.                 myFile.writeTextFile("log.txt", name);
  148.             }
  149.             createMenu(primaryStage);
  150.         });
  151.        
  152.         Text text_name = new Text("What is your name?");
  153.         text_name.setFont(Font.font("Verdana",15));
  154.        
  155.         pane_name.setHgap(10);
  156.         pane_name.setVgap(10);
  157.        
  158.         pane_name.add(text_name,8,9,5,5);
  159.         pane_name.add(button_ok,11,22,8,3);
  160.         pane_name.add(tf_name,3,15,24,5);
  161.        
  162.         Scene scene_name = new Scene(pane_name, 300, 500);
  163.        
  164.         primaryStage.setTitle("River Puff - Name");
  165.         primaryStage.setScene(scene_name);
  166.                
  167.     }
  168.    
  169.     private void drawGame (Stage primaryStage) {
  170.        
  171.         primaryStage.setScene(null);
  172.        
  173.         final int H = 700;
  174.         final int W = 1000;
  175.        
  176.         Group root = new Group();
  177.         Scene scene_game = new Scene(root, W, H, Color.LIGHTBLUE);
  178.        
  179.         Button button_menu = new Button("Menu");
  180.         button_menu.setOnAction(e ->{
  181.             createMenu(primaryStage);
  182.         });
  183.        
  184.         Double rectHeight = 100.0;
  185.        
  186.         Image ship = new Image((getClass().getResourceAsStream("ship.png")));
  187.        
  188.         /**********************************************************************/
  189.         DoubleProperty x  = new SimpleDoubleProperty();
  190.         DoubleProperty y  = new SimpleDoubleProperty();
  191.        
  192.         Timeline timeline = new Timeline(
  193.             new KeyFrame(Duration.seconds(0),
  194.                 new KeyValue(x, 300)
  195.             ),
  196.             new KeyFrame(Duration.seconds(2),
  197.                 new KeyValue(x, 500)
  198.             )
  199.         );
  200.        
  201.         Timeline timeline1 = new Timeline(
  202.             new KeyFrame(Duration.seconds(0),
  203.                 new KeyValue(y, 0)
  204.             ),
  205.             new KeyFrame(Duration.seconds(13),
  206.                 new KeyValue(y, 800)
  207.             )
  208.         );        
  209.         timeline.setAutoReverse(true);
  210.         timeline1.setAutoReverse(false);
  211.         timeline.setCycleCount(Timeline.INDEFINITE);        
  212.         timeline1.setCycleCount(Timeline.INDEFINITE);
  213.        
  214.         /*******/
  215.        
  216.         final Canvas canvas_coast = new Canvas(W, H);
  217.         final Canvas canvas_enemies = new Canvas(W, H);
  218.        
  219.         AnimationTimer timer = new AnimationTimer() {
  220.             int[] j = {0,0,0,0,0,0,0,0};
  221.             GraphicsContext b1 = canvas_coast.getGraphicsContext2D();
  222.            
  223.             @Override
  224.             public void handle(long now) {
  225.                
  226.                 if (j[0]==801) j[0]=0;
  227.                 if (j[1]==901) j[1]=100;
  228.                 if (j[2]==1001) j[2]=200;
  229.                 if (j[3]==1101) j[3]=300;
  230.                 if (j[4]==1201) j[4]=400;
  231.                 if (j[5]==1301) j[5]=500;
  232.                 if (j[6]==1401) j[6]=600;
  233.                 if (j[7]==1501) j[7]=700;
  234.  
  235.                 b1.setFill(Color.FORESTGREEN);
  236.                
  237.                 for (int i=1;i<9;i++) {
  238.                     b1.fillRect(0, j[i-1]-(i*100), 250+i*(i%3), rectHeight);
  239.                     b1.clearRect(0, j[i-1]-(i*100)-100, 250+i*(i%3), rectHeight);
  240.                    
  241.                     b1.fillRect(W-(250+i*(i%4)),j[i-1]-(i*100), 250+i*(i%4), rectHeight);
  242.                     b1.clearRect(W-(250+i*(i%4)),j[i-1]-(i*100)-100, 250+i*(i%4), rectHeight);
  243.                 }                
  244.                
  245.                 //b1.drawImage(ship, x.doubleValue(), j[0]-100, 170, 50);
  246.                 //b1.clearRect(x.doubleValue()-47, j[0]-101, 260, 1);
  247.                
  248.                 for (int i=0;i<8;i++){
  249.                     j[i]++;
  250.                 }
  251.             }
  252.         };
  253.        
  254.         AnimationTimer timer1 = new AnimationTimer() {            
  255.             int j = 0;
  256.             GraphicsContext b2 = canvas_enemies.getGraphicsContext2D();
  257.            
  258.             @Override
  259.             public void handle(long now) {
  260.                 b2.drawImage(ship, x.doubleValue(), y.doubleValue()-100, 170, 50);
  261.                 b2.clearRect(x.doubleValue()-100, y.doubleValue()-101, 370, 1);
  262.             }
  263.            
  264.         };
  265.        
  266.         timer.start();  
  267.         timer1.start();
  268.         timeline.play();
  269.         timeline1.play();
  270.         /**********************************************************************/
  271.        
  272.         Image plane = new Image((getClass().getResourceAsStream("Icon.png")));
  273.         Canvas canvas_plane = new Canvas(W, H);
  274.         GraphicsContext b3 = canvas_plane.getGraphicsContext2D();
  275.        
  276.         root.setOnKeyPressed((KeyEvent ke) -> {
  277.             if (ke.getCode() == KeyCode.LEFT) plane_pos -= 10;
  278.             if (ke.getCode() == KeyCode.RIGHT) plane_pos += 10;
  279.         });
  280.        
  281.         b3.drawImage(plane, plane_pos, 600, 50, 50);
  282.        
  283.                
  284.                
  285.         root.getChildren().add(canvas_coast);
  286.         root.getChildren().add(button_menu);
  287.         root.getChildren().add(canvas_enemies);
  288.         root.getChildren().add(canvas_plane);
  289.         primaryStage.setScene(scene_game);
  290.     }
  291.  
  292.     /**
  293.      * @param args the command line arguments
  294.      */
  295.     public static void main(String[] args) {
  296.         launch(args);
  297.     }
  298.    
  299. }
Advertisement
Add Comment
Please, Sign In to add comment