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.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.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 = "";
- @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);
- Group root = new Group();
- Scene scene_game = new Scene(root, 800, 800, Color.BLACK);
- Button button_menu = new Button("Menu");
- button_menu.setOnAction(e ->{
- createMenu(primaryStage);
- });
- Double sceneHeight = scene_game.getHeight();
- Double sceneWidth = scene_game.getWidth();
- Double rectHeigth = 100.0;
- Image ship = new Image((getClass().getResourceAsStream("ship.png")));
- final Canvas canvas_coast = new Canvas(800,800);
- GraphicsContext coast = canvas_coast.getGraphicsContext2D();
- for (int i = 0; i< (sceneHeight/rectHeigth); i++){
- Double temp = random();
- Double temp1 = random();
- coast.setFill(Color.FORESTGREEN);
- coast.fillRect(0, i * rectHeigth, (0.2 + temp1/20) * sceneWidth,
- rectHeigth);
- coast.fillRect(sceneWidth - ((0.2 + temp/20) * sceneWidth),
- i * rectHeigth, (0.2 + temp/20) * sceneWidth, rectHeigth);
- coast.setFill(Color.TEAL);
- coast.fillRect((0.2 + temp1/20) * sceneWidth, i * rectHeigth,
- (1 - (0.4 + (temp + temp1)/20)) * sceneWidth, rectHeigth);
- }
- /**********************************************************************/
- DoubleProperty x = new SimpleDoubleProperty();
- DoubleProperty y = new SimpleDoubleProperty();
- Timeline timeline = new Timeline(
- new KeyFrame(Duration.seconds(0),
- new KeyValue(x, 390),
- new KeyValue(y, 50)
- ),
- new KeyFrame(Duration.seconds(1),
- new KeyValue(x, 390),
- new KeyValue(y, 150)
- )
- );
- timeline.setAutoReverse(false);
- timeline.setCycleCount(Timeline.INDEFINITE);
- final Canvas canvas_shot = new Canvas(800, 800);
- AnimationTimer timer = new AnimationTimer() {
- @Override
- public void handle(long now) {
- GraphicsContext shot = canvas_shot.getGraphicsContext2D();
- shot.setFill(Color.FORESTGREEN);
- shot.fillOval(
- x.doubleValue(),
- y.doubleValue(),
- 20,
- 20
- );
- }
- };
- scene_game.setOnKeyPressed(ke -> {
- if (ke.getCode() == KeyCode.A) {
- timer.start();
- timeline.setCycleCount(1);
- timeline.play();
- }
- });
- /**********************************************************************/
- root.getChildren().add(canvas_coast);
- root.getChildren().add(button_menu);
- root.getChildren().add(canvas_shot);
- primaryStage.setScene(scene_game);
- }
- /*EventHandler EH = new EventHandler<ActionEvent>(){
- @Override
- public void handle(ActionEvent e) {
- System.out.println(((Button)e.getSource()).getId());
- }
- };*/
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment