Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 riverpuff.v3;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import static java.lang.Math.random;
- import static java.lang.Thread.sleep;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javafx.animation.AnimationTimer;
- import javafx.animation.KeyFrame;
- import javafx.animation.KeyValue;
- import javafx.animation.Timeline;
- import javafx.application.Application;
- import javafx.beans.property.DoubleProperty;
- import javafx.beans.property.SimpleDoubleProperty;
- import javafx.event.EventHandler;
- import javafx.geometry.Pos;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.image.Image;
- import javafx.scene.input.KeyCode;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.layout.GridPane;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import javafx.util.Duration;
- /**
- *
- * @author Marek
- */
- public class RiverPuffV3 extends Application {
- public String name = "";
- public int plane_pos = 475;
- @Override
- public void start(Stage primaryStage) {
- createMenu(primaryStage);
- primaryStage.show();
- primaryStage.setResizable(false);
- primaryStage.getIcons().
- add(new Image(getClass().getResourceAsStream("Icon.png")));
- }
- private void createMenu(Stage primaryStage) {
- primaryStage.setScene(null);
- GridPane pane_menu = new GridPane();
- Button button_name = new Button("Name");
- button_name.setMaxHeight(Double.MAX_VALUE);
- button_name.setMaxWidth(Double.MAX_VALUE);
- button_name.setOnAction(e -> {
- createName(primaryStage);
- /*try{
- OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream("log.txt", true), "UTF-8");
- BufferedWriter fbw = new BufferedWriter(writer);
- fbw.newLine();
- fbw.write("append txt...");
- fbw.newLine();
- fbw.close();
- }
- catch (Exception v) {
- System.out.println("Error: " + v.getMessage());
- }*/
- });
- Button button_start = new Button("Start");
- button_start.setMaxHeight(Double.MAX_VALUE);
- button_start.setMaxWidth(Double.MAX_VALUE);
- button_start.setOnAction(e -> {
- drawGame(primaryStage);
- });
- pane_menu.setHgap(10);
- pane_menu.setVgap(10);
- pane_menu.add(button_name,0,10,10,10);
- pane_menu.add(button_start,15,10,10,10);
- try {
- String read_file = null;
- BufferedReader in = new BufferedReader(new FileReader("log.txt"));
- read_file = in.readLine();
- Text text_name = new Text("Hello " + read_file);
- pane_menu.add(text_name,5,5,10,5);
- } catch (FileNotFoundException e) {
- System.out.println("File not found!");
- //throw new RuntimeException("File not found");
- } catch (IOException e) {
- System.out.println("IO Error occured");
- //throw new RuntimeException("IO Error occured");
- }
- Scene scene_menu = new Scene(pane_menu, 300, 500);
- primaryStage.setTitle("River Puff");
- primaryStage.setScene(scene_menu);
- }
- private void createName (Stage primaryStage) {
- primaryStage.setScene(null);
- GridPane pane_name = new GridPane();
- TextField tf_name = new TextField();
- tf_name.setMaxHeight(50);
- tf_name.setMaxWidth(240);
- tf_name.setAlignment(Pos.CENTER);
- tf_name.setFont(Font.font("Verdana",25));
- tf_name.setOnKeyPressed(ke -> {
- if (ke.getCode() == KeyCode.ENTER) {
- name = tf_name.getText();
- if (!name.isEmpty()){
- MyFile myFile = new MyFile();
- myFile.writeTextFile("log.txt", name);
- }
- createMenu(primaryStage);
- }
- });
- Button button_ok = new Button("OK");
- button_ok.setMaxHeight(30);
- button_ok.setMaxWidth(80);
- button_ok.setOnAction(e -> {
- name = tf_name.getText();
- if (!name.isEmpty()){
- MyFile myFile = new MyFile();
- myFile.writeTextFile("log.txt", name);
- }
- createMenu(primaryStage);
- });
- Text text_name = new Text("What is your name?");
- text_name.setFont(Font.font("Verdana",15));
- pane_name.setHgap(10);
- pane_name.setVgap(10);
- pane_name.add(text_name,8,9,5,5);
- pane_name.add(button_ok,11,22,8,3);
- pane_name.add(tf_name,3,15,24,5);
- Scene scene_name = new Scene(pane_name, 300, 500);
- primaryStage.setTitle("River Puff - Name");
- primaryStage.setScene(scene_name);
- }
- private void drawGame (Stage primaryStage) {
- primaryStage.setScene(null);
- final int H = 700;
- final int W = 1000;
- Group root = new Group();
- Scene scene_game = new Scene(root, W, H, Color.LIGHTBLUE);
- Button button_menu = new Button("Menu");
- button_menu.setOnAction(e ->{
- createMenu(primaryStage);
- });
- Double rectHeight = 100.0;
- Image ship = new Image((getClass().getResourceAsStream("ship.png")));
- /**********************************************************************/
- DoubleProperty x = new SimpleDoubleProperty();
- DoubleProperty y = new SimpleDoubleProperty();
- Timeline timeline = new Timeline(
- new KeyFrame(Duration.seconds(0),
- new KeyValue(x, 300)
- ),
- new KeyFrame(Duration.seconds(2),
- new KeyValue(x, 500)
- )
- );
- Timeline timeline1 = new Timeline(
- new KeyFrame(Duration.seconds(0),
- new KeyValue(y, 0)
- ),
- new KeyFrame(Duration.seconds(13),
- new KeyValue(y, 800)
- )
- );
- timeline.setAutoReverse(true);
- timeline1.setAutoReverse(false);
- timeline.setCycleCount(Timeline.INDEFINITE);
- timeline1.setCycleCount(Timeline.INDEFINITE);
- /*******/
- final Canvas canvas_coast = new Canvas(W, H);
- final Canvas canvas_enemies = new Canvas(W, H);
- AnimationTimer timer = new AnimationTimer() {
- int[] j = {0,0,0,0,0,0,0,0};
- GraphicsContext b1 = canvas_coast.getGraphicsContext2D();
- @Override
- public void handle(long now) {
- if (j[0]==801) j[0]=0;
- if (j[1]==901) j[1]=100;
- if (j[2]==1001) j[2]=200;
- if (j[3]==1101) j[3]=300;
- if (j[4]==1201) j[4]=400;
- if (j[5]==1301) j[5]=500;
- if (j[6]==1401) j[6]=600;
- if (j[7]==1501) j[7]=700;
- b1.setFill(Color.FORESTGREEN);
- for (int i=1;i<9;i++) {
- b1.fillRect(0, j[i-1]-(i*100), 250+i*(i%3), rectHeight);
- b1.clearRect(0, j[i-1]-(i*100)-100, 250+i*(i%3), rectHeight);
- b1.fillRect(W-(250+i*(i%4)),j[i-1]-(i*100), 250+i*(i%4), rectHeight);
- b1.clearRect(W-(250+i*(i%4)),j[i-1]-(i*100)-100, 250+i*(i%4), rectHeight);
- }
- //b1.drawImage(ship, x.doubleValue(), j[0]-100, 170, 50);
- //b1.clearRect(x.doubleValue()-47, j[0]-101, 260, 1);
- for (int i=0;i<8;i++){
- j[i]++;
- }
- }
- };
- AnimationTimer timer1 = new AnimationTimer() {
- int j = 0;
- GraphicsContext b2 = canvas_enemies.getGraphicsContext2D();
- @Override
- public void handle(long now) {
- b2.drawImage(ship, x.doubleValue(), y.doubleValue()-100, 170, 50);
- b2.clearRect(x.doubleValue()-100, y.doubleValue()-101, 370, 1);
- }
- };
- timer.start();
- timer1.start();
- timeline.play();
- timeline1.play();
- /**********************************************************************/
- Image plane = new Image((getClass().getResourceAsStream("Icon.png")));
- Canvas canvas_plane = new Canvas(W, H);
- GraphicsContext b3 = canvas_plane.getGraphicsContext2D();
- root.setOnKeyPressed((KeyEvent ke) -> {
- if (ke.getCode() == KeyCode.LEFT) plane_pos -= 10;
- if (ke.getCode() == KeyCode.RIGHT) plane_pos += 10;
- });
- b3.drawImage(plane, plane_pos, 600, 50, 50);
- root.getChildren().add(canvas_coast);
- root.getChildren().add(button_menu);
- root.getChildren().add(canvas_enemies);
- root.getChildren().add(canvas_plane);
- primaryStage.setScene(scene_game);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment