From 4dc1dcd8137ed1c4c04faa7f221ff40f7249d826 Mon Sep 17 00:00:00 2001 From: dlw-9ak-u1 <akil.alawad@stud.hs-hannover.de> Date: Thu, 8 Nov 2018 21:15:01 +0100 Subject: [PATCH] Add new file --- MainMenu.java | 218 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100644 MainMenu.java diff --git a/MainMenu.java b/MainMenu.java new file mode 100644 index 0000000..5207989 --- /dev/null +++ b/MainMenu.java @@ -0,0 +1,218 @@ +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.Random; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import javafx.animation.FadeTransition; +import javafx.animation.ParallelTransition; +import javafx.animation.TranslateTransition; +import javafx.application.Application; +import javafx.application.Platform; +import javafx.geometry.Pos; +import javafx.scene.Node; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; +import javafx.scene.input.KeyCode; +import javafx.scene.layout.HBox; +import javafx.scene.layout.Pane; +import javafx.scene.layout.VBox; +import javafx.scene.paint.Color; +import javafx.scene.text.Font; +import javafx.scene.text.FontWeight; +import javafx.scene.text.Text; +import javafx.stage.Stage; +import javafx.util.Duration; + + +public class MainMenu extends Application { + private static final Font FONT = Font.font("", FontWeight.BOLD, 18); + + private Stage primaryStage; + private Scene startScene; + private Scene menuScene; + + private VBox menuBox; + private VBox startBox; + private MenuItem startItem; + + protected int currItem=0; + private Random rand = new Random(); + private int playerNum = (int) ((Math.random()*656)+44); + private int randNumber= 0; + + private ScheduledExecutorService bgThread = Executors.newSingleThreadScheduledExecutor(); + private Duration durationInSeconds = Duration.seconds(0.4); + private Duration durationInMillis=Duration.millis(1000); + + + private int temPlayerNum; + + private Parent createMainMenuContent() throws FileNotFoundException { + Pane root = new Pane(); + root.setPrefSize(900, 600); + + final Image background = new Image(readFoto("Images/snakeBG.png")); + final ImageView background_node = new ImageView(background); + + ContentFrame frame1 = new ContentFrame(createLeftContent()); + ContentFrame frame2 = new ContentFrame(createRightContent()); + + HBox mainInfoBox = new HBox(300, frame1, frame2); + mainInfoBox.setTranslateX(100); + mainInfoBox.setTranslateY(50); + + + startItem = new MenuItem("Start"); + // Hier ist das Problem +// startItem.setOnActivate(new Runnable() { +// @Override +// public void run() { +// try { +// System.out.println(createStartContent()); +// startScene= new Scene(createStartContent()); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// } +// actions(startScene, startBox); +// primaryStage.setScene(startScene); +// primaryStage.show(); +// } +// }); + + MenuItem exitItem = new MenuItem("EXIT"); + exitItem.setOnActivate(() -> System.exit(0)); + + menuBox = new VBox(10, + startItem, + new MenuItem("Settings"), + new MenuItem("About"), + exitItem); + menuBox.setAlignment(Pos.TOP_CENTER); + menuBox.setTranslateX(360); + menuBox.setTranslateY(300); + + getMenuItem(0).setActive(true); + root.getChildren().addAll(background_node, mainInfoBox, menuBox); + return root; + } + + private Node createLeftContent() { + final Text inbox = new Text( playerNum+ " are playing this game"); + inbox.setFill(Color.WHITE); + + bgThread.scheduleAtFixedRate(() -> { + Platform.runLater(() -> { + + TranslateTransition tt = new TranslateTransition(durationInSeconds, inbox); + FadeTransition ft = new FadeTransition(durationInSeconds, inbox); + + tt.setToY(100); + ft.setToValue(0.1); + + ParallelTransition pt = new ParallelTransition(tt, ft); + + pt.setOnFinished(e -> { + inbox.setTranslateY(-100); + + inbox.setText(getPlayerNum()+ " are playing this game"); + + TranslateTransition tt2 = new TranslateTransition(durationInSeconds, inbox); + FadeTransition ft2 = new FadeTransition(durationInSeconds, inbox); + + tt2.setToY(0); + ft2.setToValue(5); + + ParallelTransition pt2 = new ParallelTransition(tt2, ft2); + pt2.play(); + }); + pt.play(); + }); + }, 2, 4, TimeUnit.SECONDS); + + return inbox; + } + + private Node createRightContent() { + String title = "!! Snake Game !! :)"; + HBox letters = new HBox(); + letters.setAlignment(Pos.CENTER); + for (int i = 0; i < title.length(); i++) { + Text letter = new Text(title.charAt(i) + ""); + letter.setFont(FONT); + letter.setFill(Color.WHITE); + letters.getChildren().add(letter); + + TranslateTransition tt3 = new TranslateTransition(durationInMillis, letter); + FadeTransition ft3 = new FadeTransition(durationInMillis, letter); + + tt3.setFromY(-25); + tt3.setToY(25); + ft3.setFromValue(0.1); + ft3.setToValue(1); + + ParallelTransition pt3 = new ParallelTransition(tt3, ft3); + pt3.setDelay(Duration.millis(i*30)); + pt3.setAutoReverse(true); + pt3.setCycleCount(TranslateTransition.INDEFINITE); + pt3.play(); + } + return letters; + } + + public FileInputStream readFoto (String source) throws FileNotFoundException { + return new FileInputStream(source); + } + + protected MenuItem getMenuItem(int index) { + return (MenuItem) menuBox.getChildren().get(index); + } + + private int getPlayerNum () { + for( int i=0; i<1000; i++ ) + { + randNumber = rand.nextInt(15) + 5; + } + temPlayerNum=playerNum + randNumber; + playerNum= temPlayerNum; + return playerNum; + } + + + @Override + public void start(Stage primaryStage) throws FileNotFoundException{ + menuScene = new Scene (createMainMenuContent()); + actions(menuScene, menuBox); + primaryStage.setScene(menuScene); + primaryStage.show(); + } + + public void actions(Scene scene, VBox box) { + scene.setOnKeyPressed(event -> { + if (event.getCode() == KeyCode.UP) { + if (currItem > 0) { + getMenuItem(currItem).setActive(false); + getMenuItem(--currItem).setActive(true); + } + } + + if (event.getCode() == KeyCode.DOWN) { + if (currItem < box.getChildren().size() -1) { + getMenuItem(currItem).setActive(false); + getMenuItem(++currItem).setActive(true); + } + } + + if (event.getCode() == KeyCode.ENTER) { + getMenuItem(currItem).activate(); + } + }); + } + + public static void main(String[] args) { + launch(args); + } +} \ No newline at end of file -- GitLab