1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package minesweeperfx; 33 34 35 import javafx.application.Application; 36 import javafx.geometry.Insets; 37 import javafx.geometry.Pos; 38 import javafx.scene.Scene; 39 import javafx.scene.canvas.Canvas; 40 import javafx.scene.canvas.GraphicsContext; 41 import javafx.scene.control.Button; 42 import javafx.scene.layout.BorderPane; 43 import javafx.scene.layout.HBox; 44 import javafx.scene.text.Text; 45 import javafx.stage.Stage; 46 47 48 public class MinesweeperFX extends Application { 49 Game game; 50 51 @Override 52 public void start(Stage primaryStage) { 53 BorderPane root = new BorderPane(); 54 HBox hbox = new HBox(); 55 hbox.setPadding(new Insets(5)); 56 hbox.setSpacing(20); 57 hbox.setAlignment(Pos.CENTER_LEFT); 58 root.setTop(hbox); 59 60 Button newGameButton = new Button(); 61 newGameButton.setText("New Game"); 62 newGameButton.setOnAction((event) -> game.newGame("New Game")); 63 64 Text minesDescriptionLabel = new Text(); 65 minesDescriptionLabel.setText("Mines:"); 66 67 Text minesLabel = new Text(); 68 minesLabel.setText("0"); 69 70 hbox.getChildren().add(newGameButton); 71 hbox.getChildren().add(minesDescriptionLabel); 72 hbox.getChildren().add(minesLabel); 73 74 game = new Game(primaryStage, hbox.getHeight() + 45, minesLabel); 75 game.newGame(Game.GameDifficulty.Easy); 76 Canvas canvas = new Canvas(512, 512); 77 game.getChildren().add(canvas); 78 GraphicsContext gc = canvas.getGraphicsContext2D(); 79 game.setGraphicsContext(gc); 80 81 game.setOnMouseMoved((event) -> { 82 Point point = new Point(event.getX(), event.getY()); 83 game.draw(gc, point); 84 }); 85 86 game.setOnMousePressed((event) -> { 87 if (event.isPrimaryButtonDown() && (event.isControlDown() == false)) { 88 if (Globals.debug) { 89 System.out.println("click " + event.getX() + " " + event.getY()); 90 } 91 92 Point point = new Point(event.getX(), event.getY()); 93 game.leftClick(point); 94 } 95 else if (event.isSecondaryButtonDown() || 96 (event.isPrimaryButtonDown() && (event.isControlDown() == true))) { 97 if (Globals.debug) { 98 System.out.println("rightclick " + event.getX() + " " + event.getY()); 99 } 100 101 Point point = new Point(event.getX(), event.getY()); 102 game.rightClick(point); 103 } 104 }); 105 106 root.setCenter(game); 107 108 game.draw(gc, null); 109 Scene scene = new Scene(root); 110 111 primaryStage.setResizable(false); 112 primaryStage.setTitle("MinesweeperFX"); 113 primaryStage.setScene(scene); 114 115 primaryStage.show(); 116 } 117 118 public static void main(String[] args) { 119 launch(args); 120 } 121 }